这里需要SQL Query帮助

时间:2017-08-18 22:08:16

标签: sql

我有一个包含4列的查询,如下所示

(Select PAPROJNUMBER, PACOSTCATID,EMPLOYID, PADT,  PABase_Qty from PTE10001 
where padt between '2017-04-09' and '2017-04-11'
order by PADT) 

我得到的结果如下:

PAPROJNUMBER    PACOSTCATID    EMPLOYID        PADT                  PABase_Qty
HOTELEDGER      CONSULTING      ACKE0001        2017-04-09 00:00:00.000 1.00000
HOTELEDGER      CONSULTING      ACKE0001        2017-04-09 00:00:00.000 2.00000
HOTELEDGER      CONSULTING      ACKE0001        2017-04-09 00:00:00.000 2.00000
HOTELEDGER      CONSULTING      ACKE0001        2017-04-09 00:00:00.000 4.00000
HOTELEDGER      CONSULTING      ACKE0001        2017-04-09 00:00:00.000 6.00000
HOTELEDGER      CONSULTING      ACKE0001        2017-04-10 00:00:00.000 3.00000
HOTELEDGER      CONSULTING      ACKE0001        2017-04-10 00:00:00.000 4.00000
HOTELEDGER      CONSULTING      ACKE0001        2017-04-10 00:00:00.000 2.00000
HOTELEDGER      CONSULTING      ACKE0001        2017-04-11 00:00:00.000 0.00000
HOTELEDGER      CONSULTING      ACKE0001        2017-04-11 00:00:00.000 0.00000
HOTELEDGER      CONSULTING      ACKE0001        2017-04-11 00:00:00.000 3.00000

我正在寻找像这样的输出

PAPROJNUMBER    PACOSTCATID    EMPLOYID      PABase_Qty 4/9/17 4/10/17  4/11/17 
HOTELEDGER      CONSULTING      ACKE0001         1.00    1.00    3.00   0.00 
HOTELEDGER      CONSULTING      ACKE0001         2.00    2.00    4.00   0.00 
HOTELEDGER      CONSULTING      ACKE0001         2.00    2.00    2.00   3.00 
HOTELEDGER      CONSULTING      ACKE0001         4.00    4.00    0.00   0.00 
HOTELEDGER      CONSULTING      ACKE0001         6.00    6.00   0.00    0.00 

你能帮忙解决这样的问题吗?

1 个答案:

答案 0 :(得分:0)

所以你需要将它转出来。根据需要添加更多案例陈述:

select PAPROJNUMBER, 
    PACOSTCATID, 
    EMPLOYID,
    sum(case when PADT = '2017-04-09' then PABase_Qty else 0 end) as date_4_9_17,
    sum(case when PADT = '2017-04-10' then PABase_Qty else 0 end) as date_4_10_17,
    sum(case when PADT = '2017-04-11' then PABase_Qty else 0 end) as date_4_11_17
from PTE10001
where PADT between '2017-04-09' and '2017-04-11'
group by PAPROJNUMBER, 
    PACOSTCATID, 
    EMPLOYID