-2

我正在尝试生成每个销售人员每周的数据,同时将我的结果与上周进行比较,并从本周中减去上周。有时本周没有任何价值。我想通过将其替换为“0”来处理这种情况。我不是很擅长这个,所以我正在寻求帮助。

SELECT t2.emp_name, 
       t2.pipelinetarget, 
       Sum(t0.maxsumloc)               AS SaleOpp, 
       Datepart(ww, t0.opendate)       AS [Week #], 
       Isnull ((SELECT Sum(maxsumloc) AS Expr1 
                FROM   dbo.oopr AS A 
                WHERE  ( slpcode = t1.slpcode ) 
                       AND ( Datepart(ww, opendate) = 
                             Datepart(ww, Getdate()) - 1 ) 
                       AND ( Datepart(yy, opendate) = Datepart(yy, Getdate()) ) 
                GROUP  BY slpcode), 0) AS LastWeek, 
       Isnull ((SELECT Sum(maxsumloc) AS Expr1 
                FROM   dbo.oopr AS A 
                WHERE  ( Datepart(ww, opendate) = Datepart(ww, Getdate()) ) 
                       AND ( Datepart(yy, opendate) = Datepart(yy, Getdate()) ) 
                       AND ( maxsumloc >= 10000.00 ) 
                       AND ( slpcode = t1.slpcode ) 
                GROUP  BY slpcode), 0) AS [SumOf-Opp-obove-10k], 
       Isnull ((SELECT Count(opprid) AS Expr1 
                FROM   dbo.oopr AS A 
                WHERE  ( Datepart(ww, opendate) = Datepart(ww, Getdate()) ) 
                       AND ( Datepart(yy, opendate) = Datepart(yy, Getdate()) ) 
                       AND ( maxsumloc >= 10000.00 ) 
                       AND ( slpcode = t1.slpcode ) 
                GROUP  BY slpcode), 0) AS [#-Opp-obove-10k], 
       Isnull ((SELECT Sum(maxsumloc) AS Expr1 
                FROM   dbo.oopr AS A 
                WHERE  ( Datepart(ww, opendate) = Datepart(ww, Getdate()) ) 
                       AND ( Datepart(yy, opendate) = Datepart(yy, Getdate()) ) 
                       AND ( maxsumloc < 10000.00 ) 
                       AND ( slpcode = t1.slpcode ) 
                GROUP  BY slpcode), 0) AS [SumOf-Opp-below-10k], 
       Isnull ((SELECT Count(opprid) AS Expr1 
                FROM   dbo.oopr AS A 
                WHERE  ( Datepart(ww, opendate) = Datepart(ww, Getdate()) ) 
                       AND ( Datepart(yy, opendate) = Datepart(yy, Getdate()) ) 
                       AND ( maxsumloc < 10000.00 ) 
                       AND ( slpcode = t1.slpcode ) 
                GROUP  BY slpcode), 0) AS [#-Opp-below-10k] 
FROM   dbo.oslp AS t1 
       LEFT OUTER JOIN dbo.oopr AS t0 
                    ON t1.slpcode = t0.slpcode 
       LEFT OUTER JOIN dbo.[user_qsalestarget(salesrep)] AS t2 
                    ON t2.emp_name = t1.slpname 
       LEFT OUTER JOIN dbo.ocrd AS t4 
                    ON t4.cardcode = t0.cardcode 
       LEFT OUTER JOIN dbo.[user_clusters(sp)] AS t5 
                    ON t5.emp_name = t1.slpname 
WHERE  ( Datepart(ww, t0.opendate) = Datepart(ww, Getdate()) ) 
       AND ( Datepart(yy, t0.opendate) = Datepart(yy, Getdate()) ) 
       AND ( t5.title = 'Outside Sales Rep' ) 
GROUP  BY t2.emp_name, 
          t1.slpcode, 
          Datepart(ww, t0.opendate), 
          t2.pipelinetarget 
4

1 回答 1

1

查看合并函数,例如“SELECT COALESCE(thePossibleNullValue, 0) FROM mytable”,它返回第一个非空值,即您的值或在本例中为空时为零

于 2013-10-28T15:42:29.360 回答