如何在SQL中按同一列过滤两次?

时间:2018-05-30 15:55:12

标签: sql sql-server hue

假设我有一列日期,类别和值要求和。我希望按类别进行分组并对值进行求和,使其满足特定日期的某些条件,以及另一组日期的另一个条件。

类似的东西:

<Button VerticalAlignment = "Bottom"
                 Margin = "10,0,0,10" Command = "{x: Bind local: MainPage.Pause (mp)}"
                 HorizontalAlignment = "Left" Height = "30" Width = "100"> Pause </ Button>

谢谢!

2 个答案:

答案 0 :(得分:3)

我认为你想要条件聚合:

 select categories, sum(values) 
 from table 
 group by categories
 having sum(case when dates between w and x then values else 0 end) = 0 and
        sum(case when dates between y and z then values else 0 end) > 10000 ; 

答案 1 :(得分:1)

您可以执行以下操作(SUM仅将尊重条件的值转换为IIF子句:

SELECT categories, SUM(values) 
FROM table 
GROUP BY categories
HAVING SUM(IIF(dates BETWEEN w AND x, values, 0)) = 0 
AND SUM(IIF(dates BETWEEN y AND z, values, 0)) > 10000