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 |
tonywig
Starting Member
2 Posts |
Posted - 2009-02-16 : 03:57:00
|
Hi allI have a number of tables each of which has a column called "legacy_pol".Each of the tables will contain a varying number of rows.I also have a control table which contains a small selection of legacy_pol values against which I need to hold a count from each of the other tables.I'm looking for a report that looks a built like ...Legacy_Pol Table_A Table_B Table_C etc12345 41 38 9767890 12 16 11etcI am (to say the least) no expert.I've created the control table.I've inserted the legacy_pol values I want to collect counts for.Now I am stuck on how to collect values from the other tables.Any advice welcome.ThankstonywigTony |
|
sridhar.dbe
Starting Member
34 Posts |
Posted - 2009-02-16 : 05:01:55
|
Is this what you want?declare @t1 table(id varchar(10))insert into @t1select 'L1' as 'id' union allselect 'L2' as 'id' union allselect 'L2' as 'id' union allselect 'L2' as 'id' union allselect 'L3' as 'id' union allselect 'L3' as 'id' declare @t2 table(id varchar(10))insert into @t2select 'L1' as 'id' union allselect 'L1' as 'id' union allselect 'L2' as 'id' union allselect 'L2' as 'id' union allselect 'L3' as 'id' declare @t3 table(id varchar(10))insert into @t3select 'L1' as 'id' union allselect 'L1' as 'id' union allselect 'L1' as 'id' union allselect 'L2' as 'id' union allselect 'L3' as 'id' union allselect 'L3' as 'id' union allselect 'L3' as 'id' declare @t4 table(id varchar(20))insert into @t4select 'L1' as 'id' union allselect 'L2' as 'id' union allselect 'L3' as 'id' select t4.id,t1.cn,t2.cn,t3.cn from @t4 as t4,(select id,count(id) as 'cn' from @t1 group by id) as t1,(select id,count(id)as 'cn' from @t2 group by id) as t2,(select id,count(id)as 'cn' from @t3 group by id) as t3wheret4.id=t1.idandt4.id=t2.idandt4.id=t3.id |
|
|
tonywig
Starting Member
2 Posts |
Posted - 2009-02-16 : 05:43:29
|
Thanks for the help.Technically a bit beyond me though i am happy to say I have resolved this using temp tables along the way.I will look at your suggestion though as it is likely to be far more efficient than mine.ThanksTonyTony |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-02-16 : 05:47:22
|
hi tony,use join than the cross joinselect t4.id,t1.cn,t2.cn,t3.cn from @t4 as t4 inner join (select id,count(id) as 'cn' from @t1 group by id) as t1 on t4.id=t1.idinner join (select id,count(id)as 'cn' from @t2 group by id) as t2 on t4.id=t2.idinner join (select id,count(id)as 'cn' from @t3 group by id) as t3 on t4.id=t3.id |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-16 : 06:19:03
|
quote: Originally posted by bklr hi tony,use join than the cross joinselect t4.id,t1.cn,t2.cn,t3.cn from @t4 as t4 inner join (select id,count(id) as 'cn' from @t1 group by id) as t1 on t4.id=t1.idinner join (select id,count(id)as 'cn' from @t2 group by id) as t2 on t4.id=t2.idinner join (select id,count(id)as 'cn' from @t3 group by id) as t3 on t4.id=t3.id
you need to use left join rather than inner join as there may be values which will not exists in one or more tables. the above code wont list them at all. |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-02-16 : 06:23:11
|
quote: Originally posted by visakh16
quote: Originally posted by bklr hi tony,use join than the cross joinselect t4.id,t1.cn,t2.cn,t3.cn from @t4 as t4 inner join (select id,count(id) as 'cn' from @t1 group by id) as t1 on t4.id=t1.idinner join (select id,count(id)as 'cn' from @t2 group by id) as t2 on t4.id=t2.idinner join (select id,count(id)as 'cn' from @t3 group by id) as t3 on t4.id=t3.id
you need to use left join rather than inner join as there may be values which will not exists in one or more tables. the above code wont list them at all.
yes visakh,we should use left join than inner joinjust i saw the above data and written the inner joinit is better to use left join than inner join tony........ |
|
|
|
|
|
|
|