gpt4 book ai didi

sql - T-SQL 选择每日总计但每日截止时间不是午夜

转载 作者:行者123 更新时间:2023-12-01 11:50:14 26 4
gpt4 key购买 nike

我需要计算销售数量,并按日期计算销售总额,这很简单。但曲线间距是——我需要“截止”时间是下午 6 点,而不是午夜。

6pm the day before until 6pm day of.

让我感到困惑的是“分组”。我的计数只提取真实日期,而不是“下午 6 点”信息。

Sort30   Day30      Total                 Counter
-------- ---------- --------------------- -----------
20120810 08/10/2012 675.32 9
20120809 08/09/2012 1314.68 16

查询:

SELECT top 30 CONVERT(VARCHAR(8), chickendate, 112) AS varSort30, 
CONVERT(VARCHAR(10), chickendate, 101) AS varDay30,
SUM(CAST(transAmount AS money)) AS varTotal,
Count(chickendate) AS varCounter
FROM CHICKEN
WHERE
(chickendate >= dateadd(hour, 18, dateadd(day, datediff(day, 0, chickendate), -1)) AND
chickendate < dateadd(hour, 18, dateadd(day, datediff(day, 0, chickendate), 0)))
GROUP BY CONVERT(VARCHAR(8), chickendate, 112),
CONVERT(VARCHAR(10), chickendate, 101)
ORDER BY CONVERT(VARCHAR(8), chickendate, 112) DESC

转了一圈又一圈,我觉得有什么东西在盯着我的脸。谢谢。

最佳答案

如果我的逻辑是正确的,这应该会给你正确的结果:

SELECT TOP 30
CONVERT(VARCHAR(8), modifiedChickenDate, 112) AS varSort30,
CONVERT(VARCHAR(10), modifiedChickenDate, 101) AS varDay30,
SUM(CAST(transAmount AS money)) AS varTotal,
COUNT(modifiedChickenDate) AS varCounter
FROM (
SELECT
transAmount,
DATEADD(HOUR, 6, chickendate) AS modifiedChickenDate
FROM CHICKEN
) sub
GROUP BY
CONVERT(VARCHAR(8), modifiedChickenDate, 112) AS varSort30,
CONVERT(VARCHAR(10), modifiedChickenDate, 101) AS varDay30,
ORDER BY
CONVERT(VARCHAR(8), modifiedChickenDate, 112) AS varSort30

关于sql - T-SQL 选择每日总计但每日截止时间不是午夜,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11905950/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com