| Author |
Topic |
|
vijaynet
Starting Member
7 Posts |
Posted - 2008-08-13 : 19:06:47
|
| I have two tables.Table name - Profit Id PropertyId Profit---- ------- ------------------ 1 P1 1000.002 P1 2000.00Table name - LossId PropertyId Loss----------- ---------------- ---------------------------------- 1 P1 1000.00My queryselect P.PropertyId,L.PropertyId,isnull(Sum(P.Profit),0) As 'Profit', isnull(sum(L.Loss),0) As 'Loss'from Profit P inner join Loss L On P.PropertyId=L.PropertyIdwhere P.PropertyId='P1'Group by P.PropertyId,L.PropertyId ResultPropertyId PropertyId Profit Loss------ ----------- ------------ --------------------- P1 P1 3000.00 2000.00But i need the below result PropertyId PropertyId Profit Loss--------- -------- ------------- -------------------- P1 P1 3000.00 1000.00How can i get this result..... |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-08-13 : 19:11:34
|
Try this:select P.PropertyId, L.PropertyId, dt.Profit, L.Lossfrom Profit P inner join Loss L on P.PropertyId=L.PropertyIdinner join( select PropertyId, isnull(Sum(Profit),0) As 'Profit' from Profit where PropertyId='P1' Group by PropertyId) dton P.PropertyId = dt.PropertyId Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-08-13 : 19:12:58
|
Actually, this can be simplified to:select P.PropertyId, L.PropertyId, P.Profit, L.Lossfrom Loss L inner join( select PropertyId, isnull(Sum(Profit),0) As 'Profit' from Profit where PropertyId='P1' Group by PropertyId) Pon L.PropertyId = P.PropertyId Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog |
 |
|
|
vijaynet
Starting Member
7 Posts |
Posted - 2008-08-13 : 19:22:25
|
| thank u for ur reply If i included the one more record to the both tables. The result was not group.Plz give me a solution. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
vijaynet
Starting Member
7 Posts |
Posted - 2008-08-13 : 19:43:00
|
| I have included the one more record to Profit and Loss tableTable name - ProfitId PropertyId Profit---- ------- ------------------1 P1 1000.002 P1 2000.003 P1 3000.00Table name - LossId PropertyId Loss----------- ---------------- ----------------------------------1 P1 1000.001 P1 2000.00Result for ur queryPropertyId PropertyId Profit Loss------ ----------- ------------ ---------------------P1 P1 6000.00 1000.00P1 P1 6000.00 2000.00So, the result is not group.I need the below result.PropertyId PropertyId Profit Loss------ ----------- ------------ ---------------------P1 P1 6000.00 3000.00Table records will grow in future.It must shows with group. |
 |
|
|
vijaynet
Starting Member
7 Posts |
Posted - 2008-08-13 : 20:00:59
|
| Dear tKizer,Can u understand my probs. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-08-13 : 23:58:02
|
[code]DECLARE @Profit TABLE( Id int, PropertyId varchar(2), Profit decimal(10,2))INSERT INTO @ProfitSELECT 1, 'P1', 1000.00 UNION ALLSELECT 2, 'P1', 2000.00 UNION ALLSELECT 3, 'P1', 3000.00DECLARE @Loss TABLE( Id int, PropertyId varchar(2), Loss decimal(10,2))INSERT INTO @LossSELECT 1, 'P1', 1000.00 UNION ALLSELECT 1, 'P1', 2000.00SELECT p.PropertyId, p.Profit, l.LossFROM( SELECT PropertyId, Profit = SUM(Profit) FROM @Profit GROUP BY PropertyId) pINNER JOIN( SELECT PropertyId, Loss = SUM(Loss) FROM @Loss GROUP BY PropertyId) l ON p.PropertyId = l.PropertyId/*PropertyId Profit Loss ---------- ------------ ------------ P1 6000.00 3000.00 (1 row(s) affected)*/[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
vijaynet
Starting Member
7 Posts |
Posted - 2008-08-14 : 13:08:33
|
| Thank u very much Khtan,Its working well. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-08-14 : 17:18:28
|
There is a slight problem with all suggestions above.Do they show the propertyid's where there are only profit and no loss?Or when there is a propertyid with only loss and no profit?Try this codeDECLARE @Profit TABLE (ID INT, PropertyID CHAR(2), Profit MONEY)INSERT @ProfitSELECT 1, 'P1', 1000.00 UNION ALLSELECT 2, 'P1', 2000.00 UNION ALLSELECT 3, 'P1', 3000.00 UNION ALLSELECT 4, 'P2', 2000.00DECLARE @Loss TABLE (ID INT, PropertyID CHAR(2), Loss MONEY)INSERT @LossSELECT 1, 'P1', 1000.00 UNION ALLSELECT 2, 'P1', 2000.00 UNION ALLSELECT 3, 'P3', 500.00-- PesoSELECT PropertyID, SUM(Profit) AS Profit, SUM(Loss) AS LossFROM ( SELECT PropertyID, Profit, 0 AS Loss FROM @Profit UNION ALL SELECT PropertyID, 0, Loss FROM @Loss ) AS dGROUP BY PropertyIDORDER BY PropertyID E 12°55'05.25"N 56°04'39.16" |
 |
|
|
vijaynet
Starting Member
7 Posts |
Posted - 2008-08-14 : 17:43:35
|
| No peso,Its working well.I have one more doubtUniqueCode Labour Parts Asset---------- ------ ----- ------1 50.00 0 0 1 0 50.00 01 0 0 50.00I need the below resultUniqueCode Labour Parts Asset---------- ------ ----- ------1 50.00 50.00 50.00Help me..... |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-08-14 : 17:48:58
|
quote: Originally posted by vijaynet No peso,Its working well.
What?See this sample codeDECLARE @Profit TABLE (ID INT, PropertyID CHAR(2), Profit MONEY)INSERT @ProfitSELECT 1, 'P1', 1000.00 UNION ALLSELECT 2, 'P1', 2000.00 UNION ALLSELECT 3, 'P1', 3000.00 UNION ALLSELECT 4, 'P2', 2000.00DECLARE @Loss TABLE (ID INT, PropertyID CHAR(2), Loss MONEY)INSERT @LossSELECT 1, 'P1', 1000.00 UNION ALLSELECT 2, 'P1', 2000.00 UNION ALLSELECT 3, 'P3', 500.00-- PesoSELECT PropertyID, SUM(Profit) AS Profit, SUM(Loss) AS LossFROM ( SELECT PropertyID, Profit, 0 AS Loss FROM @Profit UNION ALL SELECT PropertyID, 0, Loss FROM @Loss ) AS dGROUP BY PropertyIDORDER BY PropertyID-- OtherSELECT p.PropertyId, p.Profit, l.LossFROM ( SELECT PropertyId, SUM(Profit) AS Profit FROM @Profit GROUP BY PropertyId ) AS pINNER JOIN ( SELECT PropertyId, SUM(Loss) AS Loss FROM @Loss GROUP BY PropertyId ) as l ON l.PropertyId = p.PropertyId My suggestion returnsPropertyID Profit LossP1 6000,00 3000,00P2 2000,00 0,00P3 0,00 500,00 Whereas other code returnsPropertyId Profit LossP1 6000,00 3000,00 What happened to PropertyID P2 and P3? E 12°55'05.25"N 56°04'39.16" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-08-14 : 17:50:03
|
quote: Originally posted by vijaynet I need the below resultUniqueCode Labour Parts Asset---------- ------ ----- ------1 50.00 50.00 50.00Help me.....
I have already showed you the proper way to do this.Copy and paste my suggestion and alter as needed. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
vijaynet
Starting Member
7 Posts |
Posted - 2008-08-14 : 18:06:41
|
| Thanks peso...I need one more help from u.my table name - tblPcodePcode Parts Asset1 50.00 01 0 100.00I need below result.Pcode Parts Asset1 50.00 100.00 |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-08-14 : 18:13:57
|
See my first and only suggestion. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-14 : 22:47:12
|
quote: Originally posted by vijaynet Thanks peso...I need one more help from u.my table name - tblPcodePcode Parts Asset1 50.00 01 0 100.00I need below result.Pcode Parts Asset1 50.00 100.00
GROUP BY Pcode and take the SUM() of other fields |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-08-15 : 01:35:33
|
Peso,Could you help me with this:My table name - tblPcodePcode Parts Asset1 52.55 01 0 666.33I need below result.Pcode Parts Asset1 52.55 666.33 Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-08-15 : 02:33:30
|
I know Tara, I know... E 12°55'05.25"N 56°04'39.16" |
 |
|
|
|