在SQL Server中向透视查询添加列

时间:2016-07-06 20:30:50

标签: sql sql-server pivot

我有以下查询:

select 
    msisdn, value, date 
from 
    tang
where 
    date > dateadd(mm, -6, getdate())
    and value > 0

有一些数据如:

msisdn      value   date
772102730   1.0000  2016-01-07 08:48:30.540
772125566   2.0000  2016-01-07 10:01:50.753
772108924   1.0000  2016-01-07 10:04:27.940
772136349   5.0000  2016-01-07 10:08:07.183
772127823   1.0000  2016-01-07 10:08:19.773
774521923   1.0000  2016-01-07 10:10:13.927

以及以下数据透视查询:

select msisdn,r1,r2,r3
from 
(
    select *
    from 
    (
        select msisdn, value, 'r'+convert(varchar(4),rank() over (partition by msisdn order by date asc)) as r
        from tang
        where date > dateadd(mm,-6,getdate())
        and value > 0 
    ) a
    where r < 'r4' 
 ) a
pivot 
(
        min(value)
        for r in (r1,r2,r3)
) p

包含以下数据:

msisdn      r1      r2      r3
701048594   1.0000  2.0000  2.0000
720000810   2.0000  NULL    NULL
720030474   2.0000  2.0000  NULL
720049121   4.5000  2.0000  3.0000
720187431   2.0000  2.0000  2.0000
720189845   3.0000  NULL    NULL
720201300   5.0000  NULL    NULL
720229335   2.0000  NULL    NULL
720458626   2.0000  2.0000  2.0000
720512511   2.0000  5.0000  2.5000

我想添加以下数据列:

msisdn  r1  r2  r3 d1  d2  d3

其中d1是r1的数据,d2是r2的数据,d3是r3的数据

怎么做?

2 个答案:

答案 0 :(得分:0)

我已通过以下查询解决了问题:
select a.msisdn, r1, r2, r3, r1_date, r2_date, r3_date from ( select msisdn,r1,r2,r3 from ( select * from ( select msisdn, value, ''r''+convert(varchar(4),rank() over (partition by msisdn order by date asc)) as r from tang where date > dateadd(mm,-6,getdate()) and value > 0 ) a where r < ''r4'' ) a pivot ( min(value) for r in (r1,r2,r3) ) p ) a join ( select msisdn,r1_date,r2_date,r3_date from ( select * from ( select msisdn, date, ''r''+convert(varchar(4),rank() over (partition by msisdn order by date asc))+''_date'' as r from tang where date > dateadd(mm,-6,getdate()) and value > 0 ) a where r < ''r4_date'' ) a pivot ( min(date) for r in (r1_date,r2_date,r3_date) ) p ) b on a.msisdn = b.msisdn

答案 1 :(得分:0)

使用此查询可能会获得更好的性能,而不是组合2个透视查询。

SELECT  msisdn,
        MIN(CASE WHEN r = 1 THEN value END) AS r1,
        MIN(CASE WHEN r = 2 THEN value END) AS r2,
        MIN(CASE WHEN r = 3 THEN value END) AS r3,
        MIN(CASE WHEN r = 4 THEN value END) AS r4,
        MIN(CASE WHEN r = 1 THEN date END) AS d1,
        MIN(CASE WHEN r = 2 THEN date END) AS d2,
        MIN(CASE WHEN r = 3 THEN date END) AS d3,
        MIN(CASE WHEN r = 4 THEN date END) AS d4
FROM    (SELECT msisdn,
                value,
                RANK() OVER (PARTITION BY msisdn ORDER BY date ASC) AS r
         FROM   tang
         WHERE  date > DATEADD(mm,-6,GETDATE())
                AND value > 0
        ) a
WHERE r < 4
GROUP BY msisdn