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 |
latiful
Starting Member
10 Posts |
Posted - 2007-03-13 : 07:18:45
|
-- declare table declare @Table1 table(ID int, Value int )-- Insert sample data provided into tableinsert into @Table1select 1 , 2 union allselect 2 , 1 union allselect 7 , 2 -- declare table declare @Table2 table(ID int, Value int )-- Insert sample data provided into tableinsert into @Table2select 1 , 2 union allselect 5 , 1 union allselect 3 , 2 -- declare table declare @Table3 table(ID int, Value int )-- Insert sample data provided into tableinsert into @Table3select 1 , 2 union allselect 2 , 1 union allselect 3 , 2 -- declare table declare @Table4 table(ID int, Value int )-- Insert sample data provided into tableinsert into @Table4select 5 , 2 union allselect 2 , 1 union allselect 10 , 2 /*Is there anyway I can write one sql query which will give me the following result:IDs = All unique ID from all tablesSumOfAllValueFromAllTables =for the same ID, value from Table1+for the same ID, value from Table2 + so on ...IDs - SumOfAllValueFromAllTables===============================1 - 62 - 33 - 45 - 37 - 210 - 2*/--I have tried by the following way, but for more tables, it is becoming complicated, because I have 12 tables.--Is there any better way to do it? Thanks for the help.select coalesce(t1.ID,t2.ID) as IDs, coalesce(sum(t1.value),0) +coalesce(sum(t2.value),0) as SumOfAllValueFromAllTablesfrom @Table1 t1full join @Table2 t2 on t1.id=t2.idgroup by t1.ID,t2.IDorder by IDs |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-03-13 : 07:57:44
|
[code]select ID, sum(Value)from( select ID, Value from @Table1 union all select ID, Value from @Table2 union all select ID, Value from @Table3 union all select ID, Value from @Table4) agroup by ID[/code] KH |
 |
|
latiful
Starting Member
10 Posts |
Posted - 2007-03-13 : 19:53:42
|
Thanks for your VERY GOOD reply.I have one question about performance. For example, if I have 12 tables and each of them contain 10000 rows, then do you think, by doing the following two queries will create some performance problem?Thanks for your all VERY GOOD reply.-- All Test Tables below ------------------Query 1:select ID, sum(Value) as 'Value' from ( select ID, Value from @Table1 union all select ID, Value from @Table2 union all select ID, Value from @Table3 union all select ID, Value from @Table4 union all select ID, Value from @TableAll ) a group by ID order by IDQuery 2:select 'Total' as Total, sum(V) as 'Total Value'from ( select ID, sum(Value) as 'V' from ( select ID, Value from @Table1 union all select ID, Value from @Table2 union all select ID, Value from @Table3 union all select ID, Value from @Table4 union all select ID, Value from @TableAll ) a group by ID) b-- Test data -------------------- declare table declare @TableAll table(ID int, Value int )-- Insert sample data provided into tableinsert into @TableAllselect 1 , 0 union allselect 2 , 0 union allselect 3 , 0 union allselect 4 , 0 union allselect 5 , 0 union allselect 6 , 0 union allselect 7 , 0 union allselect 8 , 0 union allselect 9 , 0 union allselect 10 , 0 -- declare table declare @Table1 table(ID int, Value int )-- Insert sample data provided into tableinsert into @Table1select 1 , 2 union allselect 2 , 1 union allselect 7 , 2 -- declare table declare @Table2 table(ID int, Value int )-- Insert sample data provided into tableinsert into @Table2select 1 , 2 union allselect 5 , 1 union allselect 3 , 2 -- declare table declare @Table3 table(ID int, Value int )-- Insert sample data provided into tableinsert into @Table3select 1 , 2 union allselect 2 , 1 union allselect 3 , 2 -- declare table declare @Table4 table(ID int, Value int )-- Insert sample data provided into tableinsert into @Table4select 5 , 2 union allselect 2 , 1 union allselect 10 , 2 |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-14 : 02:01:27
|
select ID, sum(Value) as [Value]from(select ID, Value from @Table1union allselect ID, Value from @Table2union allselect ID, Value from @Table3union allselect ID, Value from @Table4union allselect ID, Value from @TableAll) agroup by IDwith rolluporder by IDPeter LarssonHelsingborg, Sweden |
 |
|
|
|
|
|
|