MS SQL矩阵 - 将行和行值显示为列

时间:2013-11-15 11:32:45

标签: sql sql-server sql-server-2012

嗨有像矩阵这样的情况随着select查询有一个startdate和enddate where条件我知道什么时候开始和结束基本上是7天计算

T1 = 2 is a sum of 1+1 in same date

SUM(T1) 
WHERE Date BETWEEN 2013/11/11 AND 2013-11-17
+-----+-------+----+----+----+----+------------+
| ID  | Name  | T1 | T2 | T3 | T4 |    Date    |
+-----+-------+----+----+----+----+------------+
| 100 | House |  2 |  2 |  2 |  2 | 2013-11-14 |
| 100 | House |  2 |  2 |  2 |  2 | 2013-11-15 |
+-----+-------+----+----+----+----+------------+

我需要这样

+-----+------+------+------+------+------+------+------+------+
| ID  | Type | Day1 | Day2 | Day3 | Day4 | Day5 | Day6 | Day7 |
+-----+------+------+------+------+------+------+------+------+
| 100 | T1   |   00 |   00 |   00 |    2 |    2 |   00 |   00 |
| 100 | T2   |   00 |   00 |   00 |    2 |    2 |   00 |   00 |
| 100 | T3   |   00 |   00 |   00 |    2 |    2 |   00 |   00 |
| 100 | T4   |   00 |   00 |   00 |    2 |    2 |   00 |   00 |
+-----+------+------+------+------+------+------+------+------+

我尝试使用union all pivot但不知道这个有多复杂

天才请帮帮我

谢谢

你好@conan正在工作,但如果我需要在FROM中添加两个表,如

FROM #test,test1 WHERE test.Id=test2.Id

当我这样做时,就是说id是

Msg 8156, Level 16, State 1, Line 19 The column 'Id' was specified multiple times for 'pre'.

Msg 8156, Level 16, State 1, Line 24 The column 'Id' was specified multiple times for 'Unp'.

Msg 8156, Level 16, State 1, Line 26 The column 'Id' was specified multiple times for 'Final'.

2 个答案:

答案 0 :(得分:1)

if object_id('tempdb..#test') is not null drop table #test

create table #test 
    (id int ,name varchar(5),t1 int,t2 int, t3 int ,t4 int,datecol datetime)


insert into #test values (100,'house',2,2,2,2,'20131114');
insert into #test values (100,'house',2,2,2,2,'20131115');
insert into #test values (200,'cabin',1,1,1,1,'20131112');
insert into #test values (200,'cabin',1,1,1,1,'20131113');

select * from #test

DECLARE @startdate AS DATETIME

SET @startdate = '20131111'


SELECT ID
    ,Type
    ,sum(CASE WHEN DaysDiff = 1 THEN holdcolumn ELSE 0 END) AS Day1
    ,sum(CASE WHEN DaysDiff = 2 THEN holdcolumn ELSE 0 END) AS Day2
    ,sum(CASE WHEN DaysDiff = 3 THEN holdcolumn ELSE 0 END) AS Day3
    ,sum(CASE WHEN DaysDiff = 4 THEN holdcolumn ELSE 0 END) AS Day4
    ,sum(CASE WHEN DaysDiff = 5 THEN holdcolumn ELSE 0 END) AS Day5
    ,sum(CASE WHEN DaysDiff = 6 THEN holdcolumn ELSE 0 END) AS Day6
    ,sum(CASE WHEN DaysDiff = 7 THEN holdcolumn ELSE 0 END) AS Day7
FROM (
    SELECT *
    FROM (
        SELECT datediff(d, @startdate, datecol) + 1 DaysDiff
            ,*
        FROM #test
        WHERE datecol BETWEEN @startdate
                AND dateadd(d, 6, @startdate)
        ) AS pre
    unpivot(Holdcolumn FOR [Type] IN (
                t1
                ,t2
                ,t3
                ,t4
                )) AS Unp
    ) AS Final
GROUP BY id
    ,type
ORDER BY id
    ,type

答案 1 :(得分:1)

Declare @test  table 
(id int ,name varchar(5),t1 int,t2 int, t3 int ,t4 int,datecol datetime)
insert into @test values (100,'house',2,2,2,2,'20131114');
insert into @test values (100,'house',2,2,2,2,'20131115');
insert into @test values (200,'cabin',1,1,1,1,'20131112');
insert into @test values (200,'cabin',1,1,1,1,'20131113');

Select * from 
(select ID, DName, Orders, Typess
From (select ID, DATENAME(DW, datecol) DName, t1, t2, t3, t4 from @test) a
unpivot(
Orders FOR Typess IN (t1, t2, t3, t4))as unpvot) b
pivot (
sum(Orders) for DName in (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday))as pvot
order by id