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 |
|
satheesh
Posting Yak Master
152 Posts |
Posted - 2012-10-24 : 09:40:10
|
| Dear All,I have a temp.table where i have lists of policyid.I need to sum all the price including the 'linkedpolicyid' price from policy table.Temp table dosen't have linkedpolicyid.I need to find this and sum up together.Eg:PolicyiD price, linkedpolicyid(policy table)1001 100.00 null1003 50.00 57895789 20.20 null1004 75.00 nullPolicyid (#Temp table)10011003I wrote the below query select sum(price) from policyinner join #tempon #temp.policyid = policy.policyid Result 150.00How to include/join the linkedpolicy here so that i can get the result as 170.20.Any help will be highly appreciated!ThanksSG |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-10-24 : 10:06:42
|
if its just one level of linking you can use a logic likeselect sum(p1.price)+ SUM(p2.price) from policy p1left join policy p2ON p2.PolicyiD - p1.LinkpolicyIDinner join #tempon #temp.policyid = p1.policyid if it spand multiple levels, you need a recursive CTE based logic------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
satheesh
Posting Yak Master
152 Posts |
Posted - 2012-10-24 : 10:43:55
|
| Just one level of linking.Many Thanks Visakh.Its working. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-10-24 : 11:34:34
|
| welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|