2012 年の売上がまだ終わっておらず、比較が間違っているという理由だけで、salesQty2012 の期間が SalesQty2011 と同じであることを確認する必要があるクエリで問題が発生しました。私の質問は、現在の期間に従って 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 年の売上数量が同じ期間であることを確認するクエリを作成する必要があります。
ありがとうアリ