2

我有下表称为 CustomerDetails

*Status Div Code     CustomerName                    SalesQty2012   SalesQty2011*
ACTIVE  1   CA830   Assadi SuperMarket                      0   643.75
ACTIVE  1   CA825   Askanani SuperMarket                    517.25  0
ACTIVE  4   CA822   Ashraf SuperMarket                     0    127.2083
ACTIVE  4   CA739   Al Zaqazeeq Grocery                     50  211.9167
ACTIVE  1   CA736   Al Zahra SuperMarket                    0   178
ACTIVE  1   CA835   Atlas Al Khaleej Grocery                293 452.4583
ACTIVE  1   CA846   Al Jeel Al Saad Grocery                 51  0
ACTIVE  4   CA927   Al Fazeel SuperMarket - Khorfakkan      127.375 168.1667
ACTIVE  2   CS082   Sea Side Grocery                       28.5 105
ACTIVE  2   CS111   Shairal Firdous Grocery                0    125.25

我正在尝试编写一个查询,该查询为具有以下条件的客户趋势提供一个额外的列

Case When *SalesQty2012* is higher than *SalesQty2011* THEN "Increasing"
Case When *SalesQty2011* is higher than *SalesQty2012* THEN "Decreasing"
Case When *SalesQty2011* is equal 0 (No Sales) AND  *SalesQty2012* grater than 0 THEN "NEW"
Case When *SalesQty2012* is equal 0 (No Sales) AND  *SalesQty2011* grater than 0 THEN "Lost"

这是我写的最后两个条件没有正确应用的地方

    Select top 10 [Status],DivisionCode,CustomerCode,CustomerName,
SalesQty2012=ISNULL(SalesQty2012,0),
SalesQty2011=ISNULL(SalesQty2011,0),
CustomerTrend= ( CASE WHEN SalesQty2012>SalesQty2011 THEN 'Increasing'
WHEN SalesQty2012<=SalesQty2011 THEN 'Decreasing'
WHEN SalesQty2011=0 and SalesQty2012>0 THEN 'NEW'
WHEN (SalesQty2011>0 and SalesQty2012 =0) THEN 'Lost'  else 'Nothing' END)
from dbo.CustomerMasterSalesView

order by customerTrend

我需要您的帮助来告诉我如何编写查询来处理 New and Lost 场景的案例。

非常感谢阿里


我在上面的查询中遇到了一个问题,我需要确保 salesQty2012 期间与 SalesQty2011 相同,因为 2012 年的销售仍未结束,我的比较将是错误的。我的问题是如何编写查询以根据当前时间段带来 salesqty 2011。我的原始查询以获取上述数据:

    Status,CustomerGroupCode,CustomerGroup,CustomerCode,CustomerName,DivisionCode,DivisionName,SalesLocation,RouteManagerCode,RouteManager,ChannelCode,Channel,CustomerCategoryCode,CustomerCategory,InvRoute,CustomerPeymentTermsCode,CustomerPeymentTerms,CreditDays,CreditLimit,LastInvoiceDate,SalesQty2012,SalesQty2011
ACTIVE,NULL,NO GROUP                           ,CA001  ,A L S Super Market                           ,1,DUBAI               ,Deria Dubai                        ,SDD11,SAJI M ABRAHAM,SDD11,IMPULSE CUSTOMERS             ,G    ,Groceries                          ,36,CS,CASH,0,0,27/10/2012 0:00,160,187
ACTIVE,NULL,NO GROUP                           ,CA003  ,A.M.M Super Market.  0                       ,2,ABU DHABI           ,Abu Dhabi                          ,SDD11,SAJI M ABRAHAM,SDD11,IMPULSE CUSTOMERS             ,G    ,Groceries                          ,4,CS,CASH,0,0,06/11/2012 0:00,69.625,96.4583333
ACTIVE,NULL,NO GROUP                           ,CA005  ,Aalian Super Market                          ,2,ABU DHABI           ,Abu Dhabi                          ,SDD11,SAJI M ABRAHAM,SDD11,IMPULSE CUSTOMERS             ,G    ,Groceries                          ,12,CS,CASH,0,0,27/08/2012 0:00,118.125,259.25
ACTIVE,NULL,NO GROUP                           ,CA006  ,Aawan S/M.                                   ,2,ABU DHABI           ,Abu Dhabi                          ,SDD20,NULL,SDD11,IMPULSE CUSTOMERS             ,G    ,Groceries                          ,99,CS,CASH,0,0,NULL,NULL,NULL
ACTIVE,NULL,NO GROUP                           ,CA008  ,Abbas Bin Moideen Super Market               ,2,ABU DHABI           ,Abu Dhabi                          ,SDD11,SAJI M ABRAHAM,SDD11,IMPULSE CUSTOMERS             ,G    ,Groceries                          ,4,CS,CASH,0,0,07/11/2012 0:00,185.625,188
ACTIVE,NULL,NO GROUP                           ,CA013  ,Abdu Grocery   0                             ,1,DUBAI               ,Bur Dubai                          ,SDD11,SAJI M ABRAHAM,SDD11,IMPULSE CUSTOMERS             ,G    ,Groceries                          ,44,CS,CASH,0,0,24/04/2011 0:00,NULL,10
ACTIVE,NULL,NO GROUP                           ,CA016  ,Abdu Rahiman Al Haj                          ,1,DUBAI               ,Dubai                              ,SDD11,SAJI M ABRAHAM,SDD11,IMPULSE CUSTOMERS             ,G    ,Groceries                          ,28,CS,CASH,0,0,29/10/2012 0:00,306.625,351.875
ACTIVE,NULL,NO GROUP                           ,CA017  ,Abdu Rahman  Khaleefa0                       ,1,DUBAI               ,Deria Dubai                        ,SDD11,SAJI M ABRAHAM,SDD11,IMPULSE CUSTOMERS             ,G    ,Groceries                          ,36,CS,CASH,0,0,14/10/2012 0:00,60,133.2499999
ACTIVE,NULL,NO GROUP                           ,CA018  ,Abdul  Khalfan Mohd Grocery   0              ,1,DUBAI               ,Sharjah                            ,SDD11,SAJI M ABRAHAM,SDD11,IMPULSE CUSTOMERS             ,G    ,Groceries                          ,37,CS,CASH,0,0,25/08/2011 0:00,NULL,21
ACTIVE,NULL,NO GROUP                           ,CA019  ,Abdul Aziz Super Market                      ,2,ABU DHABI           ,Abu Dhabi                          ,SDD11,SAJI M ABRAHAM,SDD11,IMPULSE CUSTOMERS             ,G    ,Groceries                          ,4,CS,CASH,0,0,08/10/2012 0:00,179.25,134.875
ACTIVE,NULL,NO GROUP                           ,CA020  ,Abdul Aziz Mohd.Sadiq Food Stuff             ,1,DUBAI               ,Sharjah                            ,SDD11,SAJI M ABRAHAM,SDD11,IMPULSE CUSTOMERS             ,G    ,Groceries                          ,41,CS,CASH,0,0,27/08/2012 0:00,10,2
ACTIVE,NULL,NO GROUP                           ,CA021  ,Abdul Gafoor Bava   0                        ,2,ABU DHABI           ,Abu Dhabi                          ,SDD11,SAJI M ABRAHAM,SDD11,IMPULSE CUSTOMERS             ,G    ,Groceries                          ,12,CS,CASH,0,0,24/09/2012 0:00,40,87.1666666
ACTIVE,NULL,NO GROUP                           ,CA022  ,Happy Down  Gr                               ,1,DUBAI               ,Dubai                              ,SDD11,SAJI M ABRAHAM,SDD11,IMPULSE CUSTOMERS             ,G    ,Groceries                          ,36,CS,CASH,0,0,31/10/2012 0:00,80,162.2083332
ACTIVE,NULL,NO GROUP                           ,CA025  ,Abdul Hamid Abdul Karim Grocery  0           ,1,DUBAI               ,Dubai                              ,SDD20,NULL,SDD11,IMPULSE CUSTOMERS             ,G    ,Groceries                          ,99,CS,CASH,0,0,12/10/2008 0:00,NULL,NULL
ACTIVE,NULL,NO GROUP                           ,CA027  ,Abdul Majid S/M - Baniyas                    ,2,ABU DHABI           ,Abu Dhabi                          ,SDD11,SAJI M ABRAHAM,SDD11,IMPULSE CUSTOMERS             ,G    ,Groceries                          ,5,CS,CASH,0,0,30/10/2012 0:00,750,824.125
ACTIVE,NULL,NO GROUP                           ,CA029  ,Yasin Ali Reza                               ,1,DUBAI               ,Deria Dubai                        ,SDD11,SAJI M ABRAHAM,SDD11,IMPULSE CUSTOMERS             ,G    ,Groceries                          ,22,CS,CASH,0,0,29/10/2012 0:00,264.125,373.9999999
ACTIVE,NULL,NO GROUP                           ,CA030  ,Abdul Rahman Abdulla Al Haj                  ,1,DUBAI               ,Dubai                              ,SDD20,NULL,SDD11,IMPULSE CUSTOMERS             ,GR   ,Group                              ,99,CS,CASH,0,0,NULL,NULL,NULL
ACTIVE,NULL,NO GROUP                           ,CA032  ,Al Zajal Gr#2                                ,3,Al Ain              ,Al Ain                             ,SDD11,SAJI M ABRAHAM,SDD11,IMPULSE CUSTOMERS             ,G    ,Groceries                          ,10,CS,CASH,0,0,25/10/2012 0:00,98,112.2083333
ACTIVE,NULL,NO GROUP                           ,CA034  ,Abdul Rasool Waswasi Grocery                 ,1,DUBAI               ,Dubai                              ,SDD20,NULL,SDD11,IMPULSE CUSTOMERS             ,GR   ,Group                              ,99,CS,CASH,0,0,NULL,NULL,NULL
ACTIVE,NULL,NO GROUP                           ,CA036  ,Abdul Raza Ghanim Grocery0                   ,1,DUBAI               ,Deria Dubai                        ,SDD11,SAJI M ABRAHAM,SDD11,IMPULSE CUSTOMERS             ,G    ,Groceries                          ,22,CS,CASH,0,0,04/03/2008 0:00,NULL,NULL

这里是查询

    Select top 20 Status=(CASE WHEN a.LOCK=0 THEN 'ACTIVE'
WHEN a.LOCK=1 THEN 'INACTIVE' END), 
CustomerGroupCode=ISNULL((SELECT TOP 1 ITcode FROM BCGMAST WHERE ITTAG='U' AND ITCODE=a.CGPCODE),null), 
CustomerGroup=ISNULL((SELECT TOP 1 ITDESC FROM BCGMAST WHERE ITTAG='U' AND ITCODE=a.CGPCODE),'NO GROUP'),                               
a.accode as CustomerCode,
a.name as CustomerName,
DivisionCode=ISNULL((SELECT ccode FROM CCMAST WHERE CCMAST.CCODE=a.CCODE),NULL),    
DivisionName=ISNULL((SELECT TOP 1 CCDES FROM CCMAST WHERE CCMAST.CCODE=a.CCODE),'NO DIVISION'),    
SalesLocation=ISNULL((SELECT TOP 1 ITDESC FROM BCGMAST WHERE BCGMAST.ITTAG='L' AND BCGMAST.ITCODE=a.SLLOC),'NO SALES LOCATION'),                              
RouteManagerCode=ISNULL(a.ROUTEMANAGER,NULL),
RouteManager=(CASE WHEN a.ROUTEMANAGER='SDD11'  THEN 'SAJI M ABRAHAM'   
WHEN a.ROUTEMANAGER='SDD01' THEN 'EHSAAN JAMEEL' 
WHEN a.ROUTEMANAGER='SDD12' THEN 'DANISH KHAN'  END), 
ChannelCode=ISNULL((SELECT TOP 1 GRPCD FROM ACGROUP WHERE ACGROUP.GRPCD=a.GRPCD),NULL),
Channel=ISNULL((SELECT TOP 1 GPSNM FROM ACGROUP WHERE ACGROUP.GRPCD=a.GRPCD),'NO CHANNEL'),
CustomerCategoryCode=ISNULL((SELECT TOP 1 ITcode FROM BCGMAST WHERE BCGMAST.ITTAG='Z' AND BCGMAST.ITCODE=a.custcate),null), 
CustomerCategory=ISNULL((SELECT TOP 1 ITDESC FROM BCGMAST WHERE BCGMAST.ITTAG='Z' AND BCGMAST.ITCODE=a.custcate),'NO CATEGORY'), 
InvRoute=a.Route,
CustomerPeymentTermsCode=a.SEQ1,
CustomerPeymentTerms=(CASE WHEN a.SEQ1='CS' THEN 'CASH'    
WHEN a.SEQ1='CR' THEN 'CREDIT'    
WHEN a.SEQ1='CB' THEN 'BILL TO BILL' END),    
a.crdays as CreditDays,
a.crlimit as CreditLimit,
LastInvoiceDate=(SELECT max(S.InvoiceDate)from Sales_Data08 S where S.PartyCode= a.ACCODE),
SalesQty2012=(SELECT Sum(S.SALESQTY)from Sales_Data S where S.PartyCode = a.ACCODE and InvoiceDate BETWEEN '2012-01-01' AND '2012-12-31'),
SalesQty2011=(SELECT Sum(S.SALESQTY)from [GRP022011].[dbo].[Sales_Data]  S where S.PartyCode = a.ACCODE and InvoiceDate BETWEEN '2011-01-01' AND '2011-12-31')
--TotalSalesAMT= (SELECT max(STHEAD.BILLDATE)from sthead where STHEAD.CLSUP = a.ACCODE),

From ACCMST a full join accsub b on    a.accode=b.accode where  a.GRPCD LIKE 'SDD%' and a.LOCK=0

从上面的视图我使用数据从下面的查询中显示客户销售趋势

SELECT
       [PartyCode]
      ,[PartyName]
      ,[DivisionName]
      ,[SalesLocation]
      ,[InvRoute]
      ,[RouteManager]
      ,[ChannelName]
      ,[CustomerGroupName]
      ,[CustomerCategory]
      ,[InvoiceDate]
      ,[BrandName]
      ,[CATEGORY]
      ,[ItemCode]
      ,[ItemName]
      ,[SALESQTY]
      ,[SALESAMT]
      ,[NetSales]
  CustomerTrend= 
    (
    CASE
      WHEN SQ.SalesQty2011 = 0 and SQ.SalesQty2012 > 0 THEN 'NEW'
      WHEN SQ.SalesQty2011 > 0 and SQ.SalesQty2012 <= 0 THEN 'Lost'
      WHEN SQ.SalesQty2011 = 0 and SQ.SalesQty2012 = 0 THEN 'Inactive'
      WHEN SQ.SalesQty2012 > SQ.SalesQty2011 THEN 'Increasing'
      WHEN SQ.SalesQty2012 <= SQ.SalesQty2011 THEN 'Decreasing'

      ELSE 'Nothing' 
    END
from [Sales_Data10] WHERE InvoiceDate BETWEEN '2011-01-01' AND '2012-12-31'

  CROSS APPLY (
              SELECT ISNULL(SV.SalesQty2011, 0) AS SalesQty2011, 
                     ISNULL(SV.SalesQty2012, 0) AS SalesQty2012
              ) SQ

总结一下,我需要编写查询以确保 2012 年和 2011 年的销售数量在同一时间段内。

谢谢阿里

4

1 回答 1

1

该案例从左到右进行评估,当有匹配时,它会采用它并且不再查找。

尝试将最后两个when子句移到顶部。

Select top 10
  [Status],
  DivisionCode,
  CustomerCode,
  CustomerName,
  SalesQty2012=ISNULL(SalesQty2012,0),
  SalesQty2011=ISNULL(SalesQty2011,0),
  CustomerTrend= 
    (
     CASE
      WHEN SalesQty2011 = 0 and SalesQty2012 > 0 THEN 'NEW'
      WHEN SalesQty2011 > 0 and SalesQty2012 = 0 THEN 'Lost'
      WHEN SalesQty2012 > SalesQty2011 THEN 'Increasing'
      WHEN SalesQty2012 <= SalesQty2011 THEN 'Decreasing'
      ELSE 'Nothing' 
     END
    )
from dbo.CustomerMasterSalesView
order by customerTrend

更新

对于列NULL中的值,您SalesQty*也需要ISNULLCASE表达式中使用。
这是一个CROSS APPLY用于获取0而不是NULL.

SELECT TOP 10
  SV.[Status],
  SV.DivisionCode,
  SV.CustomerCode,
  SV.CustomerName,
  SQ.SalesQty2012,
  SQ.SalesQty2011,
  CustomerTrend= 
    (
    CASE
      WHEN SQ.SalesQty2011 = 0 and SQ.SalesQty2012 > 0 THEN 'NEW'
      WHEN SQ.SalesQty2011 > 0 and SQ.SalesQty2012 = 0 THEN 'Lost'
      WHEN SQ.SalesQty2012 > SQ.SalesQty2011 THEN 'Increasing'
      WHEN SQ.SalesQty2012 <= SQ.SalesQty2011 THEN 'Decreasing'
      ELSE 'Nothing' 
    END
    )
FROM CustomerMasterSalesView SV
  CROSS APPLY (
              SELECT ISNULL(SV.SalesQty2011, 0) AS SalesQty2011, 
                     ISNULL(SV.SalesQty2012, 0) AS SalesQty2012
              ) SQ
ORDER BY customerTrend
于 2012-11-13T06:38:51.200 回答