从数据库表中展平数据

时间:2017-12-18 20:29:19

标签: sql sql-server-2012

表格中的数据为:

Grade |  TimeInterval |  SubjectName
1        0               History
1        1               Chemistry
1        3               Biology
2        0               Maths
2        2               Biology
2        3               History
3        0               Biology
3        1               History
3        2               Chemistry
3        3               Maths

我想将上述数据转换成以下格式,最好的方法是什么。

Grade TimeInterval0 TimeInterval1 TimeInterval2 TimeInterval3
1     History       Chemistry     Empty         Biology
2     Maths         Empty          Biology      History
3     Biology       History        Chemistry    Maths

我不知道我是否可以直接使用PIVOT转换,因为我的原始源数据中的TimeInterval将变为0,1,2而在我的展平目标表中,列名称被指定为TimeInterval0,TimeInterval1,TimeInterval2等

我无法更改架构,因为它已经拥有大量数据,而且许多其他表彼此相互关联。

3 个答案:

答案 0 :(得分:0)

您可以随时进行手动转轴。这允许您控制列名等,但您必须自己完成所有的数据透视工作。像这样的东西

Select G.Grade
    , Int0.SubjectName as TimeInterval0
    , Int1.SubjectName as TimeInterval1
    , Int2.SubjectName as TimeInterval2
    , Int3.SubjectName as TimeInterval3
from (Select distinct Grade from SourceTable) G
    left outer join SourceTable Int0 on Int0.Grade = G.Grade and Int0.TimeInterval = 0
    left outer join SourceTable Int1 on Int1.Grade = G.Grade and Int1.TimeInterval = 1
    left outer join SourceTable Int2 on Int2.Grade = G.Grade and Int2.TimeInterval = 2
    left outer join SourceTable Int3 on Int3.Grade = G.Grade and Int3.TimeInterval = 3

答案 1 :(得分:0)

您始终可以使用外部查询中的列别名重命名列。我在这里使用CTE。

IF OBJECT_ID('tempdb..#temp') IS NOT NULL DROP TABLE #temp;
CREATE TABLE #temp (Grade INT,  TimeInterval INT, SubjectName VARCHAR(100))

INSERT #temp
(
    Grade,
    TimeInterval,
    SubjectName
)
VALUES
    (1,        0,               'History    '),
    (1,        1,               'Chemistry  '),
    (1,        3,               'Biology    '),
    (2,        0,               'Maths      '),
    (2,        2,               'Biology    '),
    (2,        3,               'History    '),
    (3,        0,               'Biology    '),
    (3,        1,               'History    '),
    (3,        2,               'Chemistry  '),
    (3,        3,               'Maths      ');


WITH pivoted AS (
   SELECT * 
   FROM #temp
   PIVOT ( 
       MAX(SubjectName) FOR TimeInterval IN ([0], [1], [2], [3])
   ) pvt
)
SELECT grade,
       [TimeInterval0] = pivoted.[0],
       [TimeInterval1] = pivoted.[1],
       [TimeInterval2] = pivoted.[2],
       [TimeInterval3] = pivoted.[3]
FROM pivoted

答案 2 :(得分:0)

  

间隔为0-15,但未来会增加

您需要<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="Bank_Type123" class="form-control"> <option value="" disabled selected style="display:none;">Choose A Bank</option> <option value="10">Mizuho</option> <option value="11">UFJ</option> <option value="12">Yucho</option> </select> <div class="input-group px-1 py-1"> <span class="input-group-addon">Wallet</span> <span id="amount_of_money" class="input-group-addon unique"></span> <span class="input-group-addon">JPY</span> </div>

dynamic pivot

在这里演示http://rextester.com/LIW80994