SQL Server:获取1到多个记录集并为每个id创建1条记录

时间:2013-06-29 17:10:23

标签: sql sql-server-2005

我需要一些帮助。我需要从这3个表中获取数据并创建如下所示的输出。导出plan_name_xpending_tallyx列,以便为每个声明ID生成一行。每个索赔ID最多可以与3个计划关联,我希望在一个记录中显示每个计划和计数金额。做这个的最好方式是什么?

感谢您的任何想法。 :)

需要输出结果集:

claim_id    ac_name     plan_name_1   pending_tally1 plan_name_2     Pending_tally2 plan_name_3    pending_tally3
--------    -------     -----------   -------------- -----------     -------------- -----------    --------------
1234        abc cooks   delux_prime   22             prime_express   23             standard_prime 2
2341        zzz bakers  delpux_prime  22             standard_prime  2              NULL           NULL
3412        azb pasta's prime_express 23             NULL            NULL           NULL           NULL

用于上述结果集的SQL Server 2005表:

company_claims

claim_id    ac_name     
1234        abc cooks
2341        zzz bakers
3412        azb pasta's

claim_plans

claim_id    plan_id     plan_name
1234        101     delux_prime
1234        102     Prime_express
1234        103     standard_prime
2341        101     delux_prime
2341        103     standard_prime
3412        102     Prime_express

Pending_amounts

claim_id    plan_id     Pending_tally
1234        101     22
1234        102     23
1234        103     2
2341        101     22
2341        103     2
3412        102     23

2 个答案:

答案 0 :(得分:0)

我会先加入您的所有数据,以便获得相关列:claim_id, ac_name, plan_name, pending tally

然后我会添加转换来获取计划名称并计划在不同行上的计数,并将标签绑定在一起。

然后它应该很容易转动。

我会将这些与常用表格表达式结合在一起。

以下是查询:

with X as (
  select cc.*, cp.plan_name, pa.pending_tally,
    rank() over (partition by cc.claim_id order by plan_name) as r
  from company_claims cc
  join claim_plans cp on cp.claim_id = cc.claim_id
  join pending_amounts pa on pa.claim_id = cp.claim_id
    and pa.plan_id = cp.plan_id
), P as (
    select
      X.claim_id, 
      x.ac_name, 
      x.plan_name as value,
      'plan_name_' + cast(r as varchar(max)) as label
    from x
  union all 
    select
      X.claim_id, 
      x.ac_name, 
      cast(x.pending_tally as varchar(max)) as value,
      'pending_tally' + cast(r as varchar(max)) as label
    from x
)
select claim_id, ac_name, [plan_name_1], [pending_tally1],[plan_name_2], [pending_tally2],[plan_name_3], [pending_tally3]
from (select * from P) p
pivot (
  max(value)
  for label in ([plan_name_1], [pending_tally1],[plan_name_2], [pending_tally2],[plan_name_3], [pending_tally3])
) as pvt
order by pvt.claim_id, ac_name

这是一个小提琴,展示了它的实际效果:http://sqlfiddle.com/#!3/68f62/10

答案 1 :(得分:0)

如果您知道3总是最大的计划数量,那么一些左连接将正常工作:

select c.claim_id, c.ac_name, 
       cp1.plan_name as plan_name_1, pa1.pending_tally as pending_tally1,
       cp2.plan_name as plan_name_2, pa2.pending_tally as pending_tally2,
       cp3.plan_name as plan_name_3, pa3.pending_tally as pending_tally3,
from company_claims c
left join claim_plans cp1 on c.claim_id = cp1.claim_id and cp1.planid = 101
left join claim_plans cp2 on c.claim_id = cp2.claim_id and cp2.planid = 102
left join claim_plans cp3 on c.claim_id = cp3.claim_id and cp3.planid = 103
left join pending_amounts pa1 on cp1.claim_id = pa1.claimid and cp1.planid = pa1.plainid
left join pending_amounts pa2 on cp2.claim_id = pa2.claimid and cp2.planid = pa2.plainid
left join pending_amounts pa3 on cp3.claim_id = pa3.claimid and cp3.planid = pa3.plainid