在SQL中使用子查询时获取单个员工的多行

时间:2015-05-27 12:01:19

标签: sql oracle

我正在编写查询以获取员工的福利详情。我需要获取不同计划和选项的扣除金额。对于扣除金额,表格中有一个字段。我为了不同的计划和选项多次获取该字段。我使用子查询做到了这一点。但我的查询是返回单个员工的多行,并在多行中显示金额。我希望单个员工的所有扣减金额都在一行中。

这就是我编写查询的方式: -

select papf.person_number, csv.salary_amount, (select bprv.rt_val from ben_prtt_rt_val bprv,
     ben_opt_f bof, 
     ben_pl_f bpf, 
     ben_pl_typ_f bpt where bprv.prtt_enrt_rslt_id=bper.prtt_enrt_rslt_id and bper.pl_typ_id=bpt.pl_typ_id and bpf.pl_typ_id=bpt.pl_typ_id and bper.pl_id=bpf.pl_id and bper.opt_id=bof.opt_id and bpt.name ='abcd' and bpf.name='abcd' and bof.name='Participant'
and (sysdate between bof.effective_start_Date and  bof.effective_end_Date)  
    and  (sysdate between bpt.effective_start_Date and  bpt.effective_end_Date)
    and (sysdate between bpf.effective_start_Date and  bpf.effective_end_Date)) as "abcd Amount",
(select bprv1.rt_val from ben_prtt_rt_val bprv1,ben_opt_f bof1, 
     ben_pl_f bpf1, 
     ben_pl_typ_f bpt1 where bprv1.prtt_enrt_rslt_id=bper.prtt_enrt_rslt_id and bper.pl_typ_id=bpt1.pl_typ_id and bpf1.pl_typ_id=bpt1.pl_typ_id and bper.opt_id=bof1.opt_id and bper.pl_id=bpf1.pl_id and bpt1.name ='efgh' and bpf1.name='efgh' and bof1.name='Participant'
and (sysdate between bof1.effective_start_Date and  bof1.effective_end_Date)  
    and  (sysdate between bpt1.effective_start_Date and  bpt1.effective_end_Date)
    and (sysdate between bpf1.effective_start_Date and  bpf1.effective_end_Date)) as "efgh Amount",
(select bprv2.rt_val from ben_prtt_rt_val bprv2,ben_opt_f bof2, 
     ben_pl_f bpf2, 
     ben_pl_typ_f bpt2 where bprv2.prtt_enrt_rslt_id=bper.prtt_enrt_rslt_id and bper.pl_typ_id=bpt2.pl_typ_id and bpf2.pl_typ_id=bpt2.pl_typ_id and bper.opt_id=bof2.opt_id and bper.pl_id=bpf2.pl_id and bpt2.name ='ijkl' and bpf2.name='ijkl' and bof2.name='Participant'
and (sysdate between bof2.effective_start_Date and  bof2.effective_end_Date)  
    and  (sysdate between bpt2.effective_start_Date and  bpt2.effective_end_Date)
    and (sysdate between bpf2.effective_start_Date and  bpf2.effective_end_Date)) as "ijkl Amount"
from
     ben_prtt_enrt_rslt bper,
     per_all_people_f papf  ,
     cmp_salary csv
where 
    bper.assignment_id=csv.assignment_id    
    and bper.person_id=papf.person_id    
    and  (sysdate between papf.effective_start_Date and  papf.effective_end_Date) 
    AND CSV.DATE_TO=(SELECT MAX(CSV1.DATE_TO) FROM CMP_SALARY CSV1 WHERE CSV.ASSIGNMENT_ID=CSV1.ASSIGNMENT_ID)

我想要这样的结果: -

Employee     Salary    abcd_amt    efgh_amt     ijkl_amt 
1234              10000       200         300         400

但是使用查询我得到的结果如下: -

Employee  Salary  abcd_amt         efgh_amt           ijkl_amt
1234       10000    200             
1234       10000                       300         
1234       10000                                         400

并且还会重复行。

请帮帮我。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

只需在查询中添加group by和Max,就像这样:

select papf.person_number as Employee, csv.salary_amount as Salary
max(-query of abcd_amt-) as abcd Amount, 
max(-query of efgh_amt-)as efgh Amount, 
max(-query of ijkl_amt-) as abcd Amount
from  --tables names
where -- where condition
group by papf.person_number, csv.salary_amount;