-3

我有一个代码可以查看我的销售员工。他最近受聘为已灭绝的客户创造增长。因此,在我们在 dbase 中拥有的 1,000 多个客户中,他只会影响 100 个。因此,在他的值所在的位置生成了很多行NULL。我希望报告排除这些客户行,以便更有效地了解我的管理层。我将如何做到这一点?

    select  T1.CardCode 'BP Code',
        T1.CardName 'BP Name',
        COUNT(CASE WHEN T1.SlpCode<>'37' THEN T0.DocNum END) '# of Orders',
        SUM(CASE WHEN T1.SlpCode<>'37' THEN T0.DocTotal END) 'Total Orders Amt',
        SUM(CASE WHEN T1.SlpCode<>'37' THEN T0.DOCTOTAL END)/
        COUNT(CASE WHEN T1.SlpCode<>'37' THEN T0.DocNum END) 'Avg Order Size',
        COUNT(CASE WHEN T1.SlpCode='37' THEN T0.DocNum END) '# of Orders',
        SUM(CASE WHEN T1.SlpCode='37' THEN T0.DocTotal END) 'Total Orders Amt',
        SUM(CASE WHEN T1.SlpCode='37' THEN T0.DOCTOTAL END)/
        COUNT(CASE WHEN T1.SlpCode='37' THEN T0.DocNum END) 'Avg Order Size'

from    ORDR T0 INNER JOIN OCRD T1 ON T0.CardCode=T1.CardCode

where   T0.DocStatus = 'C'
        AND T0.CANCELED <> 'Y'
        AND T0.DocDate >= '2015-05-26'

GROUP BY    T1.CardCode,
            T1.CardName

Order By    t1.CardName
4

2 回答 2

0

尝试包括

AND T1.SlpCode is NOT NULL

于 2015-07-21T20:54:04.113 回答
0
select tnot.*, tyes.*
from ( select OCRD.CardCode 'BP Code',
              OCRD.CardName 'BP Name',
              COUNT(ORDR.DocNum) '# of Orders',
              SUM(ORDR.DocTotal) 'Total Orders Amt'
         FROM OCRD
         LEFT JOIN ORDR 
           ON ORDR.CardCode= OCRD.CardCode 
          AND ORDR.CANCELED <> 'Y'
          AND ORDR.DocDate >= '2015-05-26'
          AND OCRD.SlpCode <> '37' 
     GROUP BY OCRD.CardCode,
              OCRD.CardName
     ) tnot    
join ( select OCRD.CardCode 'BP Code',
              OCRD.CardName 'BP Name',
              COUNT(ORDR.DocNum) '# of Orders',
              SUM(ORDR.DocTotal) 'Total Orders Amt'
         FROM OCRD
         JOIN ORDR 
           ON ORDR.CardCode= OCRD.CardCode 
          AND ORDR.CANCELED <> 'Y'
          AND ORDR.DocDate >= '2015-05-26'
          AND OCRD.SlpCode  = '37' 
     GROUP BY OCRD.CardCode,
              OCRD.CardName
     ) tyes 
    on tnot.[BP Code] = tyes.[BP Code]
   and tnot.[BP Name] = tyes.[BP Name]
 order by tyes.[BP Name]
于 2015-07-21T22:05:25.193 回答