数据透视表如何不允许多列中的空值

时间:2016-09-23 21:28:53

标签: sql sql-server tsql filter pivot

如果[2015][2016]为NULL,我想设置条件然后隐藏行, 但我不能把它拉下来。

如果billname没有数据,我对它没兴趣。

SELECT 
    IssuedByUserGroup,
    BILLNAME, 
    SUM([2016]) AS [2016],
    SUM([2015])AS [2015]
FROM Sum_Orders 
PIVOT
(
    SUM(Sum_SellPrice)
        FOR OrderperiodYear IN ([2016],[2015]) 
) AS pvt
WHERE  ( (MONTH(OrderDate) = MONTH(GETDATE()) 
    AND day(OrderDate) <= DAY(GETDATE())) OR MONTH(OrderDate) < MONTH(GETDATE())) 
--This part above is just for getting the data since january 1 to sep 23(to the date)
    AND OrderStatus in ('Complete','invoiced') 
    AND OrderPrefix IN ('LAX6') 
GROUP BY BILLNAME,IssuedByUserGroup
ORDER BY IssuedByUserGroup desc

按预期返回以下内容:

enter image description here

谢谢!

2 个答案:

答案 0 :(得分:2)

GROUP BY部分

下添加COALESCE
HAVING COALESCE(SUM([2016]), SUM([2015])) IS NOT NULL

答案 1 :(得分:0)

您应该能够在PIVOT下面使用WHERE子句,如下所示:

WHERE [2015] IS NOT NULL