Results 1 to 2 of 2

Thread: SQL - Report

  1. #1
    Join Date
    May 2002
    Posts
    30

    Unanswered: SQL - Report

    I have a table tab1

    part_make char(3)
    part_model char(3)
    age int
    count int

    Eg:

    Part_make Part_model age count

    1 2 4 5
    1 2 9 8
    1 2 3 7


    I want a quey which will give the output as

    part_make part_model age0to2 age3to5 age6to10 age10plus
    1 2 0 12 8 0


    What is the most efficient way to do this ?

    Currently I do

    select t1.part_make,t2.part_model,(select sum(count) form tab1 t2 where t1.part_make=t2.part_make and t1.part_model=t2.part_model and age between 0 and 2)etc from tab1 t1 group by t1.part_make,t2.part_model

    (A null for 0 does not matter)

    Cheers

    Soundh

  2. #2
    Join Date
    Jan 2003
    Posts
    4,292
    Provided Answers: 5
    Soundh,

    I believe that something like the following would work better:

    Select part_make, part_model,
    case when age between 0 and 2 then sum(count) else 0 end as age0to2,
    etc
    from tab1
    group by ...

    Andy

Posting Permissions

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