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 |
|
R.Prabu
Starting Member
33 Posts |
Posted - 2008-04-07 : 14:03:33
|
| i have two tables Table 1 - AccountRentFields areRentAmount RentYear 100 2004 200 2004 300 2004 400 2005 500 2006Table 2 - InsuranceFields areInsuranceAmount ReceivedYear 100 2002 200 2002 300 2003 400 2003 500 2005 600 2005I Need to sum these two tables 'Amount' based on the Year The Result forIncomeAmount Year 300 2002 700 2003 600 2004 1500 2005 500 2006Regards,Prabu R |
|
|
jhocutt
Constraint Violating Yak Guru
385 Posts |
Posted - 2008-04-07 : 14:15:01
|
| [code]declare @AccountRent table (RentAmount int, RentYear int)insert into @AccountRentselect 100, 2004 union allselect 200, 2004 union allselect 300, 2004 union allselect 400, 2005 union allselect 500, 2006declare @Insurance table (InsuranceAmount int, ReceivedYear int)insert into @Insurance select 100, 2002 union allselect 200, 2002 union allselect 300, 2003 union allselect 400, 2003 union allselect 500, 2005 union allselect 600, 2005select Yr, sum(Amt) as Amt from ( select RentAmount as Amt, RentYear as Yr from @AccountRent union all select InsuranceAmount as Amt, ReceivedYear as Yr from @Insurance ) agroup by Yr[/code]"God does not play dice" -- Albert Einstein"Not only does God play dice, but he sometimes throws them where they cannot be seen." -- Stephen Hawking |
 |
|
|
R.Prabu
Starting Member
33 Posts |
Posted - 2008-04-07 : 14:56:01
|
| thanks for your replay,Here i changed the query but its showing error, just i changed the Table namei don't want the insert statementError: "Invalid object name 'Tbl_AccountRent'" select Yr, sum(Amt) as Amt from ( select RentAmount as Amt, RentYear as Yr from Tbl_AccountRent union all select InsuranceAmount as Amt, ReceivedYear as Yr from Tbl_AccountInsurance ) agroup by YrRegards,Prabu R |
 |
|
|
jhocutt
Constraint Violating Yak Guru
385 Posts |
Posted - 2008-04-07 : 15:00:53
|
| Are you in the correct databaseIE does "select RentAmount as Amt, RentYear as Yr from Tbl_AccountRent "return data if not prepend the databse and ownerselect RentAmount as Amt, RentYear as Yr from DATABASE.OWNER.Tbl_AccountRent"God does not play dice" -- Albert Einstein"Not only does God play dice, but he sometimes throws them where they cannot be seen." -- Stephen Hawking |
 |
|
|
R.Prabu
Starting Member
33 Posts |
Posted - 2008-04-07 : 15:20:49
|
| Yes you are right, your query executed correclty butI applied you query in my requirement, it showing error tell me the solution my requirement isthe "propertyId" field having the three tables the two tables results should displayed based "HSSPMS_Tbl_PropertyMaster" table OwnerId='LL564547' and the two tables should displayed based on the yearIt displaying Error,select Yr, sum(Amt) as Amt from ( SELECT SUM(ISNULL(AR.PaidAmount, 0)) AS Income, AR.RentYear FROM HSSPMS_Tbl_AccountRent AS AR GROUP BY AR.RentYear Union All SELECT SUM(ISNULL(AI.ActualCost, 0)) AS Income , AI.ReceivedYear FROM HSSPMS_Tbl_AccountInsurance As AI GROUP BY AI.ReceivedYear) WHERE AR.PropertyId In (SELECT PropertyId FROM HSSPMS_Tbl_PropertyMaster WHERE OwnerId='LL564547') GROUP BY AR.RentYearRegards,Prabu R |
 |
|
|
jhocutt
Constraint Violating Yak Guru
385 Posts |
Posted - 2008-04-07 : 15:40:13
|
Next time post what you are looking for not part of the problem.select Yr, sum(Amt) as Amt from ( SELECT SUM(ISNULL(AR.PaidAmount, 0)) AS Amt, AR.RentYear as yr FROM HSSPMS_Tbl_AccountRent AS AR WHERE AR.PropertyId In (SELECT PropertyId FROM HSSPMS_Tbl_PropertyMaster WHERE OwnerId='LL564547') GROUP BY AR.RentYear Union All SELECT SUM(ISNULL(AI.ActualCost, 0)) AS Amt, AI.ReceivedYear as yr FROM HSSPMS_Tbl_AccountInsurance As AI GROUP BY AI.ReceivedYear) AGROUP BY A.Yr "God does not play dice" -- Albert Einstein"Not only does God play dice, but he sometimes throws them where they cannot be seen." -- Stephen Hawking |
 |
|
|
R.Prabu
Starting Member
33 Posts |
Posted - 2008-04-07 : 16:00:40
|
| Hi, This query is executed but the Result is Wrong. give me the solutionRegards,Prabu R |
 |
|
|
jhocutt
Constraint Violating Yak Guru
385 Posts |
Posted - 2008-04-07 : 16:09:23
|
| What makes you think the result it wrong??"God does not play dice" -- Albert Einstein"Not only does God play dice, but he sometimes throws them where they cannot be seen." -- Stephen Hawking |
 |
|
|
R.Prabu
Starting Member
33 Posts |
Posted - 2008-04-07 : 16:20:04
|
| WHERE AR.PropertyId In (SELECT PropertyId FROM HSSPMS_Tbl_PropertyMaster WHERE OwnerId='LL564547') GROUP BY AR.RentYear This Condition Is not applied for below select statement. The above condition should apply for both select statement and also it display the all the amount and yearSELECT SUM(ISNULL(AI.ActualCost, 0)) AS Amt, AI.ReceivedYear as yr FROM HSSPMS_Tbl_AccountInsurance As AI GROUP BY AI.ReceivedYearRegards,Prabu R |
 |
|
|
jhocutt
Constraint Violating Yak Guru
385 Posts |
Posted - 2008-04-07 : 16:22:22
|
| Well since you will not post your table structure, sample data and expected output, I guessed based on your previous query.Since you know what is wrong now correct it.Or post your table structure sample data and the expected output."God does not play dice" -- Albert Einstein"Not only does God play dice, but he sometimes throws them where they cannot be seen." -- Stephen Hawking |
 |
|
|
R.Prabu
Starting Member
33 Posts |
Posted - 2008-04-07 : 16:40:41
|
| I have 3 TablesTable 1 - AccountRentFields areRentAmount RentYear PropertyId100 2004 PT0101 200 2004 PT0102300 2004 PT0103400 2005 PT0104500 2006 PT0105Table 2 - InsuranceFields areInsuranceAmount ReceivedYear PropertyId100 2002 PT0101200 2002 PT0102300 2003 PT0103400 2003 PT0104500 2005 PT0105 600 2005 PT0106Table3 - MasterOwnerId PropertyIdLL564547 PT0101LL564547 PT0102LL564547 PT0103LL564547 PT0104LL564547 PT0105LL564547 PT0106LL564548 PT0107LL564548 PT0108LL564548 PT0109LL564548 PT01010...The Table 1 and 2 results display based on the table3 OwnerId='LL564547' Which all propertyId matches the table3 OwnerId. Now i want Sum Table1 RentAmount and Table 2 InsuranceAmount These two Amount should group by year. The Table3 Condition OwnerId='LL564547' Should applicable for Tabl1 and Table 2Expected ResultIncomeAmount Year300 2002700 2003600 20041500 2005500 2006I think now you are understand my requirementRegards,Prabu R |
 |
|
|
jhocutt
Constraint Violating Yak Guru
385 Posts |
Posted - 2008-04-07 : 17:09:18
|
First This is not your table structure you have table names like HSSPMS_Tbl_AccountRent etc.Second please post your data as I have here.That way we do not have to spend time getting it set up so we can insert it.declare @AccountRent table (RentAmount int, RentYear int, PropertyId varchar(20))insert into @AccountRent select 100, 2004, 'PT0101' union allselect 200, 2004, 'PT0102' union allselect 300, 2004, 'PT0103' union allselect 400, 2005, 'PT0104' union allselect 500, 2006, 'PT0105'declare @Insurance table (InsuranceAmount int, ReceivedYear int, PropertyId varchar(20))insert into @Insurance select 100, 2002, 'PT0101' union allselect 200, 2002, 'PT0102' union allselect 300, 2003, 'PT0103' union allselect 400, 2003, 'PT0104' union allselect 500, 2005, 'PT0105 ' union allselect 600, 2005, 'PT0106' declare @Master table (OwnerId varchar(20), PropertyId varchar(20))insert into @Masterselect 'LL564547', 'PT0101' union allselect 'LL564547', 'PT0102' union allselect 'LL564547', 'PT0103' union allselect 'LL564547', 'PT0104' union allselect 'LL564547', 'PT0105' union allselect 'LL564547', 'PT0106' union allselect 'LL564548', 'PT0107' union allselect 'LL564548', 'PT0108' union allselect 'LL564548', 'PT0109' union allselect 'LL564548', 'PT01010' -- Table 1 - AccountRent-- RentAmount RentYear PropertyId-- -- Table 2 - Insurance-- InsuranceAmount ReceivedYear PropertyId-- -- Table 3 - Master-- OwnerId PropertyIdselect sum(amt) as IncomeAmount, yr as 'Year'from ( select IsNull(a.RentAmount,0) as amt, a.RentYear as yr from @AccountRent a join @Master b on a.PropertyId = b.PropertyId AND b. OwnerID = 'LL564547' union all select IsNull(a.InsuranceAmount,0) as amt, a.ReceivedYear as yr from @Insurance a join @master b on a.PropertyId = b.PropertyId AND b. OwnerID = 'LL564547') agroup by yr "God does not play dice" -- Albert Einstein"Not only does God play dice, but he sometimes throws them where they cannot be seen." -- Stephen Hawking |
 |
|
|
R.Prabu
Starting Member
33 Posts |
Posted - 2008-04-07 : 17:59:52
|
| Thanks You,I got the Expected result, its Working Fine, I gave all the table structure, just i changed the table name for easy to understandThank for spending time to here.Regards,Prabu R |
 |
|
|
|
|
|
|
|