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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Select

Author  Topic 

satheesh
Posting Yak Master

152 Posts

Posted - 2014-02-12 : 10:38:59
Hi

I have policy and commission table. There are up to 3 levels of commission for each policy and i want to select all commission in a row by policyid

for eg :
select policy.policyid,commission.amount,commission.level
from policy
inner join commission
on commission.policyid = policy.policyid

Result

Policyid Commission level
50470 9.056839 2
50470 31.011067 3
50470 7.185093 0
23355 6.8347 0
23355 76.989 2

But i want to display like below

policyid level0 level2 level3
50470 7.185093 9.056839 31.011067
23355 6.8347 76.989

Any help will be highly appreciated.


Thanks

Regards,
SG

alexsts
Starting Member

13 Posts

Posted - 2014-02-12 : 11:53:13
quote:
Originally posted by satheesh

Hi

I have policy and commission table. There are up to 3 levels of commission for each policy and i want to select all commission in a row by policyid

for eg :
select policy.policyid,commission.amount,commission.level
from policy
inner join commission
on commission.policyid = policy.policyid

Result

Policyid Commission level
50470 9.056839 2
50470 31.011067 3
50470 7.185093 0
23355 6.8347 0
23355 76.989 2

But i want to display like below

policyid level0 level2 level3
50470 7.185093 9.056839 31.011067
23355 6.8347 76.989

Any help will be highly appreciated.


Thanks

Regards,
SG




There are few ways to do it.
Most efficient, but not easiest, is to use pivotal tables.
Other is to use bunch of temp tables to insert for example to table 1 all what is related to level 1, temp 2 to level 2 etc.
Then create join between those temp tables based on your criteria and select what you need.
Cheers.


Alex
Go to Top of Page

Ifor
Aged Yak Warrior

700 Posts

Posted - 2014-02-12 : 12:39:02
eg:

-- *** Test Data ***
CREATE TABLE #Commission
(
Policyid int NOT NULL
,Commission decimal(8,6) NOT NULL
,[level] tinyint NOT NULL
);
INSERT INTO #Commission
SELECT 50470, 9.056839, 2
UNION ALL SELECT 50470, 31.011067, 3
UNION ALL SELECT 50470, 7.185093, 0
UNION ALL SELECT 23355, 6.8347, 0
UNION ALL SELECT 23355, 76.989, 2
-- *** End Test Data ***

SELECT Policyid
,COALESCE([0], 0) AS Level0
,COALESCE([2], 0) AS Level2
,COALESCE([3], 0) AS Level3
FROM
(
SELECT Policyid, Commission, [level]
FROM #Commission
) S
PIVOT
(
MIN(Commission)
FOR [level] IN ([0], [2], [3])
) P

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-02-13 : 07:55:08
you can extend IFors suggestion tusing dynamic sql if your level values cant be determined before hand
http://beyondrelational.com/modules/2/blogs/70/posts/10840/dynamic-pivot-in-sql-server-2005.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -