Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
jparker
Posting Yak Master
118 Posts |
Posted - 2006-07-25 : 09:21:36
|
| I have the following code which joins two temp tablescreate table #temptable (folder_id varchar(100), orders smallint)create table #temptable1 (folder_id varchar(100), actual smallint)insert into #temptable (folder_id, orders) select distinct d.pack_id, count(distinct order_id) as 'orders' from tblDocuments dgroup by d.pack_id order by d.pack_idinsert into #temptable1 (folder_id, actual) select distinct d.folder, count(distinct order_id) as 'actual' from tblDocuments dleft join tblFilenames f on f.code = convert(varchar(20), d.pack_id)group by d.pack_id, f.mydesc, d.folder order by d.folderselect * from #temptableselect * from #temptable1drop table #temptabledrop table #temptable1This outputs for #temptablefolder_id | orders ------------------------10 | 416 | 52 | 121 | 44 | 15 | 650 | 169 | 17 | 18 | 4This outputs for #temptable1folder_id | orders ------------------------10 | 316 | 52 | 121 | 44 | 15 | 650 | 169 | 17-8-10 | 18 | 3I would like to join the tables so I get the following output which is based mainly on the second table but should an entry from the first not be there then to output original count and then actual as zero.The join in the code excludes the 7 folder_id as it doesn't have any actual packs now. See belowfolder_id | orders | actuals------------------------------10 | 4 | 316 | 5 | 52 | 1 | 121 | 4 | 44 | 1 | 15 | 6 | 650 | 1 | 169 | 1 | 17-8-10 | 0 | 18 | 4 | 3 | 27 | 26Can you help please |
|
|
jparker
Posting Yak Master
118 Posts |
Posted - 2006-07-25 : 09:32:58
|
| Apologies I forgot to include the joining of the temp tables code which is as follows : - select t1.folder_id, IsNull(t.orders, 0) as orders, t1.actual from #temptable1 t1left join #temptable t on t.folder_id = t1.folder_idunion allselect ' ', sum(t.orders), sum(t1.actual)from #temptable1 t1left join #temptable t on t.folder_id = t1.folder_id |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-07-25 : 10:28:03
|
| Erm, what is the question? The way I read it, you already have everything you need.Do you want folder_id 7 to be included? If so, what should that row look like? If not, what?Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
jparker
Posting Yak Master
118 Posts |
Posted - 2006-07-25 : 10:36:09
|
| Your correct I don't have folder_id 7The correct results would look something like thisolder_id | orders | actuals------------------------------10 | 4 | 316 | 5 | 52 | 1 | 121 | 4 | 44 | 1 | 15 | 6 | 650 | 1 | 169 | 1 | 17 | 1 | 07-8-10 | 0 | 18 | 4 | 3| 27 | 27Where 7 | 1 | 0 is the new line. The problem being is that pack_id does equal folder_id which as there isn't a match doesn't bring in the valueIts too hot and my brain is failing to work so would appreciate how to get this working |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-07-25 : 11:10:17
|
You probably just need to use full join rather than left join, and then handle the nulls. Something like this...select isnull(t1.folder_id, t.folder_id), IsNull(t.orders, 0) as orders, isnull(t1.actual, 0) as actualfrom #temptable1 t1full join #temptable t on t.folder_id = t1.folder_idunion allselect ' ', sum(t.orders), sum(t1.actual)from #temptable1 t1full join #temptable t on t.folder_id = t1.folder_id Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
jparker
Posting Yak Master
118 Posts |
Posted - 2006-07-25 : 11:21:08
|
| Thanks that will probably be the best solution so that I can move on to something new :) ThanksJ |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-07-25 : 11:30:00
|
Incidentally, it may be more efficient to use a group by and rollup to get your totals (rather than a union).select case when grouping(folder_id) = 1 then '' else folder_id end as folder_id, sum(orders) as orders, sum(actual) as actualfrom ( select isnull(a.folder_id, b.folder_id) as folder_id, isnull(b.orders, 0) as orders, isnull(a.actual, 0) as actual from #temptable1 a full outer join #temptable b on a.folder_id = b.folder_id) cgroup by folder_idwith rolluporder by grouping(folder_id), folder_id Of course, it may be even more efficient if you get your totals from your front-end (if applicable).Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
|
|
|
|
|