| Author |
Topic |
|
jparker
Posting Yak Master
118 Posts |
Posted - 2006-05-18 : 10:00:19
|
I having a bit of a bad day today and have forgotten how best to handle this problem. I know I can handle it in a temp table but dont want to.The scenario is that I have the following 2 sql statementsselect order_id,person_id, sum(pages) from tbldocuments where [primary] <> 2 and folder = '10' group by order_id, person_id select order_id,person_id, sum(pages)+2 from tbldocuments where folder = '10' group by order_id, person_id Which produces the output17612 161520651 1817631 235868283 17and17612 161520651 2617631 235868283 24but I prefer to have it as 17612 161520651 18 2617631 235868283 17 24Can you please put me out of my misery pleaseThanks  |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
jparker
Posting Yak Master
118 Posts |
Posted - 2006-05-18 : 10:09:22
|
| I don't want to return in a single field I want its own field which perhaps wasn't clear in my example.Sorry but is there another approach? |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-05-18 : 10:34:25
|
| What happens when you have 100 different values in sum(pages)? Do you want to have 100 columns?If you want to generate cross-tab reports look for cross-tab reports in sql server help file and also this link http://weblogs.sqlteam.com/jeffs/archive/2005/05.aspxMadhivananFailing to plan is Planning to fail |
 |
|
|
jparker
Posting Yak Master
118 Posts |
Posted - 2006-05-18 : 10:40:16
|
| There will only ever be two columns of sums and a total of 4 columns in all |
 |
|
|
jparker
Posting Yak Master
118 Posts |
Posted - 2006-05-18 : 10:50:16
|
| This is basically what I want to do but don't believe it is the best way to do itcreate table #temptable (order_id varchar(30), person_id varchar(30), original int)insert into #temptable select order_id,person_id, sum(pages) from tbldocuments where [primary] <> 2 and folder = '10' group by order_id, person_id select t.*, sum(d.pages)+2 from #temptable tjoin tbldocuments d on d.order_id = t.order_idwhere d.folder = '10' group by d.order_id, d.person_id, t.order_id, t.person_id, t.originaldrop table #temptable |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-05-18 : 10:51:19
|
Try thisSelect order_id,person_id, Sum(case when [primary] <> 2 then pages else 0 end) as Sum1, Sum(pages)+2 as Sum2,from tbldocuments where folder = '10' group by order_id, person_id MadhivananFailing to plan is Planning to fail |
 |
|
|
jparker
Posting Yak Master
118 Posts |
Posted - 2006-05-18 : 10:58:08
|
| As always brilliant madhivanan. I knew there was a simplier way than my messy code.Cheers mate |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-05-18 : 11:04:01
|
Thats why I asked you to look for cross-tab reports in sql server help file MadhivananFailing to plan is Planning to fail |
 |
|
|
|