条件窗口函数

时间:2014-09-06 07:37:09

标签: sql-server function tsql windowing partition-by

假设此查询,

SELECT  
    SUM (Quantity) OVER (Partition By Symbol ORDER BY PositionID ASC ROWS UNBOUNDED PRECEDING) as RemainedQty

是否可以在窗口函数中定义条件? 在这个例子中,我需要在每一行的不同情况下得到SUM(Quantity)。 某事(假设)如:

SELECT
SUM (Quantity) OVER (Partition By ...) Where SomeColumn='X' as RemainedQty_X,
SUM (Quantity) OVER (Partition By ...) Where SomeColumn='Y' as RemainedQty_Y ...

1 个答案:

答案 0 :(得分:1)

是的,这是可能的。

选择SUM(SomeColumn ='X',然后是Quantity else 0结束时的情况)作为sumx,SUM(SomeColumn ='y',然后是Quantity else 0 end的情况)作为表中的sumy