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 |
vip
Starting Member
4 Posts |
Posted - 2011-06-01 : 14:48:32
|
hello, My question is as follows I have 2 tables table1 containing (tid,name )and table2 containing (id,tid,cost,quantity) and table1 is related to table2 by tid for example Table1 Table2tid name id tid cost quantity1 X 1 1 10 12 X 2 3 20 23 Y 3 4 5 54 Yin the above I have 2 tables if I go to table1 and check all the values whose tid<=2 are of name X and tid>=3 have name Y so what I need is to make a query to get the total sum of cost and quantity for those having tid>=3 and tid <=2the final result should be as follows:Total Quantity of type X | Total Cost of X| total Quantity of type Y| Total Cost of Yhere is my query but it doesn't give the result as required:select sum(quantity),sum(cost*quantity) from table1 where tid<=2 groupby tidselect sum(quantity),sum(cost*quantity) from table1 where tid>=3 groupby tid |
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2011-06-01 : 15:10:01
|
Could you post what your expected result should be?JimEveryday I learn something that somebody else already knew |
 |
|
latch
Yak Posting Veteran
62 Posts |
Posted - 2011-06-01 : 15:22:16
|
These query are providing results as these:select sum(quantity),sum(cost*quantity) from table2 where tid<=2 group by tid(No column name) (No column name)1 10 select sum(quantity),sum(cost*quantity) from table2 where tid>=3 group by tid(No column name) (No column name)2 405 25i think those values are right. if you want all the results at same time then use UNION.provide a sample of your result to better guide you. |
 |
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2011-06-01 : 15:40:49
|
Here's my guess based on little or no information and the fact that I'm leaving in 5 minutesDECLARE @Table1 TABLE (tid int identity (1,1), name char(1))DECLARE @Table2 TABLE (id int identity(1,1),tid int,cost int,qty int)INSERT INTO @Table1SELECT 'X' UNION ALLSELECT 'X' UNION ALLSELECT 'Y' UNION ALLSELECT 'Y'INSERT INTO @Table2SELECT 1,10,1 UNION SELECT 3,20,2 UNIONSELECT 4,5,5 ;with cteAS(SELECT NewT.[Name],NewT.Total,NewQty.QtyFROM( select (t1.tid +1)/2 as NewTID ,[name] ,count(*) as Total from @table1 t1 group by (t1.tid +1)/2 ,[name]) NewTINNER JOIN (select (t2.tid +1)/2 as NewTID , sum(t2.Cost*t2.qty) as QTY from @table2 t2 group by (t2.tid +1)/2 )NewQtyON Newt.NewTid = NewQty.NewTid)select MAX(CASE WHEN [Name] = 'X' THEN Total END) as TotalX,MAX(CASE WHEN [Name] = 'X' THEN qty END) as TotalXCost ,MAX(CASE WHEN [Name] = 'Y' THEN Total END) as TotalY,MAX(CASE WHEN [Name] = 'Y' THEN qty END) as TotalYCost from cteJimEveryday I learn something that somebody else already knew |
 |
|
vip
Starting Member
4 Posts |
Posted - 2011-06-01 : 15:41:48
|
sample Output: Table1:tid Name1 X2 X3 Y4 YTable2:id tid cost quantity1 1 10 52 3 11 23 4 50 14 2 9 3Output:TotalCostX TotalQuantityX TotalCOSTY TOTALQUANTITYY(10*5)+(9*3) 5+3 (11*2)+(50*1) 2+1 |
 |
|
vip
Starting Member
4 Posts |
Posted - 2011-06-01 : 23:02:44
|
If I make the following query it won't return the result in the specified output select sum(quantity),sum(cost*quantity) from table1 where tid<=2 groupby tidUNIONselect sum(quantity),sum(cost*quantity) from table1 where tid>=3 groupby tid |
 |
|
|
|
|
|
|