SQL新手。如何让以下查询给出每日结果?

时间:2017-04-03 13:34:55

标签: sql-server

好的,所以我有以下问题,但是我试图围绕如何在给定的日期范围内给我每日结果。截至目前,这基本上是我的查询:

SELECT Source = CASE WHEN Project = 'CODE' OR Code = '1' THEN 'SOURCE1'
                       WHEN Project <> 'CODE' AND Code = '2' THEN 'SOURCE2'
                       WHEN Project <> 'CODE' AND Code = '3' THEN 'SOURCE3'
                       WHEN Project <> 'CODE' AND Code = '4' THEN 'SOURCE4'
                       ELSE Code END, Count(Document) as TotalCount, SUM(Amount) as TotalAmount                       

-- Joins here to gather and refine data collected

WHERE Date >= '03/20/2017'
       AND Date < '03/23/2017'
GROUP BY CASE WHEN Project = 'CODE' OR Code = '1' THEN 'SOURCE1'
                           WHEN Project <> 'CODE' AND Code = '2' THEN 'SOURCE2'
                           WHEN Project <> 'CODE' AND Code = '3' THEN 'SOURCE3'
                           WHEN Project <> 'CODE' AND Code = '4' THEN 'SOURCE4'
                           ELSE Code END
ORDER BY TotalCount DESC

给出了:

SOURCE1 123 1225
SOURCE2 23  126
SOURCE3 17  78
SOURCE4 12  56
MISCSOURCE1 6   78

理想情况下,它会给我:

Date         Source   TotalCount  TotalAmount
2017-03-20   SOURCE1  60          625
2017-03-20   SOURCE2  13          60
-- etc
2017-03-21   SOURCE1  63          600
2017-03-21   SOURCE2  10          66
-- etc

它还必须提供单独的MISCSOURCE(即除了重新标记的主要4之外的1 / +额外来源)总计数和金额。我该怎么做呢?在此先感谢帮助新手!

3 个答案:

答案 0 :(得分:1)

听起来你正想在日期字段上聚合。所以将你的逻辑调整为这样的东西可能对你有用:

SELECT [Date]
, 'SOURCE' + Code AS Source
, COUNT(Document) as TotalCount
, SUM(Amount) as TotalAmount
--FROM...                       
GROUP BY [Date], 'SOURCE' + Code
ORDER BY COUNT(Document) DESC

这应该会为您提供与您请求的输出更相似的输出。如果确实存在“Code”有时不等于Source1的实例,则可能需要添加一些额外的逻辑。进行以下调整可能有效:

SELECT [Date]
, 'SOURCE' + CASE WHEN Project = 'CODE' OR Code = '1' THEN '1' ELSE Code END AS Source
, COUNT(Document) as TotalCount
, SUM(Amount) as TotalAmount     
--FROM...                  
GROUP BY [Date]
, 'SOURCE' + CASE WHEN Project = 'CODE' OR Code = '1' THEN '1' ELSE Code END
ORDER BY COUNT(Document) DESC

应该这样做。

答案 1 :(得分:0)

如果您只是将日期添加到选择和组中,您应该得到您想要的内容。像这样:

SELECT Date,Source = CASE WHEN Project = 'CODE' OR Code = '1' THEN 'SOURCE1'
                       WHEN Project <> 'CODE' AND Code = '2' THEN 'SOURCE2'
                       WHEN Project <> 'CODE' AND Code = '3' THEN 'SOURCE3'
                       WHEN Project <> 'CODE' AND Code = '4' THEN 'SOURCE4'
                       ELSE Code END, Count(Document) as TotalCount, SUM(Amount) as TotalAmount                       

-- Joins here to gather and refine data collected

WHERE Date >= '03/20/2017'
       AND Date < '03/23/2017'
GROUP BY CASE WHEN Project = 'CODE' OR Code = '1' THEN 'SOURCE1'
                           WHEN Project <> 'CODE' AND Code = '2' THEN 'SOURCE2'
                           WHEN Project <> 'CODE' AND Code = '3' THEN 'SOURCE3'
                           WHEN Project <> 'CODE' AND Code = '4' THEN 'SOURCE4'
                           ELSE Code END
ORDER BY date,TotalCount DESC

答案 2 :(得分:0)

@ChadPortman很接近,只是在Group By部分中缺少Date。最终查询如下所示:

SELECT Date, Source = CASE WHEN Project = 'CODE' OR Code = '1' THEN 'SOURCE1'
                   WHEN Project <> 'CODE' AND Code = '2' THEN 'SOURCE2'
                   WHEN Project <> 'CODE' AND Code = '3' THEN 'SOURCE3'
                   WHEN Project <> 'CODE' AND Code = '4' THEN 'SOURCE4'
                   ELSE Code END, Count(Document) as TotalCount, SUM(Amount) as TotalAmount                       

-- Joins here to gather and refine data collected

WHERE Date >= '03/20/2017'
       AND Date < '03/23/2017'
GROUP BY Date, CASE WHEN Project = 'CODE' OR Code = '1' THEN 'SOURCE1'
                           WHEN Project <> 'CODE' AND Code = '2' THEN 'SOURCE2'
                           WHEN Project <> 'CODE' AND Code = '3' THEN 'SOURCE3'
                           WHEN Project <> 'CODE' AND Code = '4' THEN 'SOURCE4'
                           ELSE Code END
ORDER BY Date, TotalCount DESC

这正是我所需要的。谢谢大家的回复!