Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2004
    Posts
    10

    Unanswered: select statement

    I have a table with account name,date of sale and value fields I would like to know how I can construct a select statement that would give me
    the following output:-

    account |sales value yr 1 |sales value yr 2 |sales value yr 3


    Any help is much appreciated!

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    PHP Code:
    select account
         
    sum(case when year(salesdate) = 2002 
               then salesamt 
    else 0 end) as sales2002
         
    sum(case when year(salesdate) = 2003 
               then salesamt 
    else 0 end) as sales2003
         
    sum(case when year(salesdate) = 2004 
               then salesamt 
    else 0 end) as sales2004
      from salestable
    group by account 
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Feb 2004
    Posts
    10

    select statment

    Originally posted by r937
    PHP Code:
    select account
         
    sum(case when year(salesdate) = 2002 
               then salesamt 
    else 0 end) as sales2002
         
    sum(case when year(salesdate) = 2003 
               then salesamt 
    else 0 end) as sales2003
         
    sum(case when year(salesdate) = 2004 
               then salesamt 
    else 0 end) as sales2004
      from salestable
    group by account 
    Thanks this worked!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •