1

我需要帮助。我需要从这 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_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

待定金额

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

2 回答 2

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

于 2013-06-29T17:36:24.657 回答
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
于 2013-06-29T17:48:55.450 回答