Click here to Skip to main content
15,894,410 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,

I am using below query to count a particular column data based on single condition.

SQL
SELECT COUNT(DISTINCT(S.INVOICENO)) AS LESSTWO , YEAR(S.invoicedate) YER,Month(S.invoicedate) MNTH
  FROM SALESDATA S where  S.BILLINGTYPE='INVOICE' AND (S.invoicedate >='4-1-2009' and S.invoicedate <='4-30-2010')
                          AND S.TAXABLEAMT <=2000
                   GROUP BY YEAR(S.invoicedate) ,Month(S.invoicedate) ORDER BY YEAR(S.invoicedate) ,Month(S.invoicedate)



But I have to calculate for 3 more conditions. Is it possible to count based on 3 conditions(i.e. 2001 - 3000,3001 - 4000, 4001 - 5000,>5000) and I want result set for all the conditions?


Regards,

N.SRIRAM
Posted

I can not provide you a exact solution you want but answer given in THIS[^] of getting group in range with SQL will help you towards.
 
Share this answer
 
Dear Friend
I think
SELECT distinct COUNT(S.INVOICENO) AS LESSTWO , YEAR(S.invoicedate)As YER,Month(S.invoicedate)as MNTH
  FROM SALESDATA S where  S.BILLINGTYPE='INVOICE' AND (S.invoicedate between '4-1-2009' and  '4-30-2010')
                          AND S.TAXABLEAMT <=2000
                   GROUP BY YEAR(S.invoicedate) ,Month(S.invoicedate) ORDER BY YEAR(S.invoicedate) ,Month(S.invoicedate)


this will solve the issue


If there is still causing the error then feel free to contact along with the table exact structure and the error you got along with the output needed

regards
Vipin Kumar Mallaya G
 
Share this answer
 
v2
If it were me, I'd do it in multiple queries, and store each result in its own variable. That would also make the sql more maintainable, and maybe even faster than trying to wrangle all that into a single query.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900