Results 1 to 5 of 5

Thread: Counting Records with Case

  1. #1

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    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."


  2. #2
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    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

  3. #3
    Fanatic Member vb_dba's Avatar
    Join Date
    Jun 2001
    Location
    Somewhere aloft between the real world and insanity
    Posts
    1,016
    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

  4. #4

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965
    Bruce,
    Thanks for the quick post. I had to add 'END' in each of the Case Statements. Otherwise it worked great!!


    VB Code:
    1. SELECT INCIDENT_TYPE,
    2.    COUNT(CASE WHEN Year(Received_Dt) = 2004 THEN 1 ELSE 0 [b]END[/b]) AS C2004,
    3.    COUNT(CASE WHEN Year(Received_Dt) = 2003 THEN 1 ELSE 0 [b]END[/b]) AS C2003
    4. FROM dbo.INCIDENTS
    5. 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."


  5. #5

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965
    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
  •  



Click Here to Expand Forum to Full Width