我需要计算销售数量,并按日期总计销售总额,这很容易。但曲线间距是 - 我需要"切断"到下午6点而不是午夜。
前一天下午6点至下午6点。
扔我的是"分组"。我的计数只是拉动真实的日期而不是" 6 pm"资讯
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
四处转转,我觉得它的某些东西在我脸上盯着我。感谢。
答案 0 :(得分:4)
如果我的逻辑是正确的,这应该会给你正确的结果:
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
答案 1 :(得分:1)
如果chickendate是DATETIME而不是DATE,那么您可以使用
GROUP BY CONVERT(VARCHAR(10), DATEADD(hh, 6, chickendate), 101)
将日期提前6小时(截止时间比午夜提前6小时,或下午6点),然后在一年中分组。这仅适用于您存储时间信息的情况,我不确定您是谁。为必要的表发布一些模式。但我认为你正在寻找......
SELECT TOP 30 CONVERT(VARCHAR(10), DATEADD(hh, 6, chickendate), 101) as Date
, SUM(CAST(transAmount as money)) AS Total
, Count(*) as Counter
FROM Chicken
GROUP BY CONVERT(VARCHAR(10), DATEADD(hh, 6, chickendate), 101)
ORDER BY CONVERT(VARCHAR(10), DATEADD(hh, 6, chickendate), 101) DESC
答案 2 :(得分:0)
怎么样:
GROUP BY CONVERT(VARCHAR(8), dateadd(hour, 6, chickendate), 112)
所以'2012-08-09 18:00:00'
按20120810
分组,'2012-08-09 17:59:59'
按20120809
分组。