Table 1
RID Name
---------------
1 Bob
2 Tom
3 Sally
4 Fran
Table2
RID Plan Percent
--------------------
3 1 13
2 4 10
3 2 20
1 5 15
3 4 5
3 5 28
I need to de-normalize this in my result set... Ordered from Largest to smallest Percent. So for RID=3 I would want back
RID Plan1 Pct1 Plan2 Pct2 Plan3 Pct3
-----------------------------------------------
3 2 30 5 28 1 13
My first thought was a temp table and doing something like this:
Code:
insert into #Temp (Plan01, Pct01)
(SELECT TOP 1 PlanID, PctShare
from hcpmhcplan
where RID = 10100001
order by PctShare desc, PlanID desc
)
insert into #Temp (Plan02, Pct02)
(SELECT TOP 1 PlanID, PctShare
from hcpmhcplan
where RID = 10100001 and PlanID not in (
select PlanID from #Temp where RID = 10100001)
order by PctShare desc, PlanID desc
) where RID = 10100001
insert into #Temp (Plan03, Pct03)
(SELECT TOP 1 PlanID, PctShare
from hcpmhcplan
where RID = 10100001 and PlanID not in (
Select PlanID from #Temp where RID = 10100001)
order by PctShare desc, PlanID desc
) where RID = 10100001
Unfortunately- the order by doesn't seem to work in this case- SQL Server doesn't like it... I know all the nesting and in's are sloppy.. I want to get it to work before I worked on cleaning it up...
Anyone have any ideas?
Hal Levy
I am here to help you, not do it for you.
That is, unless you hire me. I am looking for work.