Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8817601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:53:20+00:00 2026-06-14T04:53:20+00:00

I Have the following table called CustomerDetails *Status Div Code CustomerName SalesQty2012 SalesQty2011* ACTIVE

  • 0

I Have the following table called 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

I am trying to write a query that a an extra column for customer Trend with follwoing condition

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"

This what I wrote where the last two condition is not applied correctly

    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

I need your help to tell me how I can write the query to handle the case for New and Lost scenario.

Many Thanks
ALi


I got a issue on above query where I need to make sure the salesQty2012 period is same as SalesQty2011 simply because 2012 sales still not over and my comparison will be wrong.
My question is how I can write a query to bring salesqty 2011 as per current period of time . my orignal query to fetch above data :

    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

here the query

    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

from above view I use data to show customer sales trend from below query

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

To Summarize I need to write query that ensures sales Qty for 2012 and 2011 are in same period of time.

Thanks
Ali

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T04:53:21+00:00Added an answer on June 14, 2026 at 4:53 am

    The case is evaluated left to right and when there is a match it takes that and looks no further.

    Try to move your two last when clauses to the top.

    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
    

    Update

    With NULL values in your SalesQty* columns you need to use ISNULL in your CASE expressions as well.
    Here is a version that uses CROSS APPLY to get 0 instead of 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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have the following code.My table is called 'tableu'.I want replace 'tableu' with a
I have following PHP code for inserting session data into a table called ds_session:
I have something like following code inside [XMLValue] column of a table called AlgorithmLog:
suppose I have the following source table (called S): name gender code Bob 0
I have the following table called stores id store zip 1 Market 1 01569
If I have the following fields in a table called DailyLeaveLedger LeaveDate LeaveType AmountPaid
I have a table called LOGS with the following columns: id - user -
I have a table called tblRentalRates with the following columns: rate_id (int) rate_startdate (datetime)
I have a table called Employee with the following fields: EmpID Salary Name I
I have a table called 'prices with the following structure: 'id', 'price, 'availability_id' and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.