|
-
Aug 23rd, 2004, 02:24 PM
#1
Thread Starter
Giants World Champs!!!!
Counting Records with Case
I have a table that has a date field and an item field. I want to compare the number of items sold this to the amount sold last year. I know I have to use the Case statement but I can't remember how to do it
Data in Table1:
Date Item
1/1/2004 Apples
3/5/2004 Oranges
2/5/2003 Grapes
5/15/2003 Oranges
The output:
Item 2004 2003
Apples 1 0
Grapes 0 1
Oranges 1 1
Thank in advance!!!!
Regards,
Mark
Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."
-
Aug 23rd, 2004, 02:38 PM
#2
I am assuming SQL Server but try this query.
Code:
Select
Item,
Sum(Case When Year(DateField) = 2004 Then 1 Else 0),
Sum(Case When Year(DateField) = 2003 Then 1 Else 0)
From Table1
Group By Item
-
Aug 23rd, 2004, 03:10 PM
#3
Fanatic Member
To build on brucevde's example(if you're using SQL Server), you could use the Year() and GetDate() functions so that you don't have to change the Year settings:
Code:
Select
Item,
Sum(Case When Year(DateField) = Year(GetDate()) Then 1 Else 0),
Sum(Case When Year(DateField) = (Year(GetDate()) - 1) Then 1 Else 0)
From Table1
Group By Item
Chris
Master Of My Domain
Got A Question? Look Here First
-
Aug 23rd, 2004, 03:15 PM
#4
Thread Starter
Giants World Champs!!!!
Bruce,
Thanks for the quick post. I had to add 'END' in each of the Case Statements. Otherwise it worked great!!
VB Code:
SELECT INCIDENT_TYPE,
COUNT(CASE WHEN Year(Received_Dt) = 2004 THEN 1 ELSE 0 [b]END[/b]) AS C2004,
COUNT(CASE WHEN Year(Received_Dt) = 2003 THEN 1 ELSE 0 [b]END[/b]) AS C2003
FROM dbo.INCIDENTS
GROUP BY INCIDENT_TYPE
I had to add 'END' in each of the Case Statments. Otherwise it worked great!!
Thanks
Regards,
Mark
Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."
-
Aug 23rd, 2004, 03:17 PM
#5
Thread Starter
Giants World Champs!!!!
Thanks vb_dba
But I am going to dynamically create the SQL statements using VB-Script. I like your solution, I never would have thought of that.
Mark
Regards,
Mark
Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|