用于2个值列的SQL Pivot

时间:2019-06-06 00:57:17

标签: sql tsql

我需要透视这样的sql数据集。

studentID	year level	Results	Dimension
23096	7	576	reading
23096	7	640	writing
23096	7	704	spelling
23096	7	768	numeracy
23096	7	832	grammarPunctuation
23096	8	896	reading
23096	8	960	writing
23096	8	1024	spelling
23096	8	1088	numeracy
23096	8	1152	grammarPunctuation
23104	8	1216	reading
23104	8	1280	writing
23104	8	1344	spelling
23104	8	1408	numeracy
23104	8	1472	grammarPunctuation
23116	8	1536	reading
23116	8	1600	writing
23116	8	1664	spelling
23116	8	1728	grammarPunctuation
23117	7	1792	reading
23117	7	1856	writing
23117	7	1920	spelling
23117	7	1984	numeracy
23117	7	2048	grammarPunctuation

看起来像

StudentID Dimension   Year7_Results  Year8_Results  
 23096    reading             576                896     
 23096    writing             640                960 
 23096    spelling            704                1024
 23096    numeracy            768                1088
 23096    grammarPunctuation  832                1152

我的代码

Select * from 
(
select StudentID,yearlevel,resultes,Dimantion  ,ROW_NUMBER() OVER(partition by studentID,Dimension order by studentID,Dimension) AS PX,
'R' + CAST(ROW_NUMBER() OVER(partition by studentID,Dimension order by studentID,Dimension) as varchar(2) ) AS PX2
 from table
 )Temp
 PIVOT
 (
 MAX(Dimension) FOR PX IN ("1","2")
 )PVI1 
 PIVOT 
 (
 SUM(resultes) FOR PX2 IN ("R1","R2")
 )PIVOT2

2 个答案:

答案 0 :(得分:1)

您可以使用条件聚合来代替数据透视:

数据示例:

declare @table as table (
       studentID  int          not null,
       yearlevel  int          not null,
       results    int          null,
       dimension  nvarchar(50) null
    )
    
    insert into @table
    values 
    (23096,7,576,'reading'),
    (23096,7,640,'writing'),
    (23096,7,704,'spelling'),
    (23096,7,768,'numeracy'),
    (23096,7,832,'grammarPunctuation'),
    (23096,8,896,'reading'),
    (23096,8,960,'writing'),
    (23096,8,1024,'spelling'),
    (23096,8,1088,'numeracy'),
    (23096,8,1152,'grammarPunctuation'),
    (23104,8,1216,'reading'),
    (23104,8,1280,'writing'),
    (23104,8,1344,'spelling'),
    (23104,8,1408,'numeracy'),
    (23104,8,1472,'grammarPunctuation'),
    (23116,8,1536,'reading'),
    (23116,8,1600,'writing'),
    (23116,8,1664,'spelling'),
    (23116,8,1728,'grammarPunctuation'),
    (23117,7,1792,'reading'),
    (23117,7,1856,'writing'),
    (23117,7,1920,'spelling'),
    (23117,7,1984,'numeracy'),
    (23117,7,2048,'grammarPunctuation')

查询:

select  StudentID, 
        Dimension, 
        max(case when yearlevel = 7 then results end) as Year7_Results  ,
        max(case when yearlevel = 8 then results end) as Year8_Results  
from  @table
group by StudentID, Dimension

enter image description here

答案 1 :(得分:0)

您可以在不使用数据透视功能的情况下解决该问题。首先创建包含所有学生记录的表。

drop table if exists #student_results

create table #student_results (
   studentID  int          not null,
   year_level int          not null,
   Results    int          null,
   Dimension  nvarchar(50) null
)

然后从示例数据中插入一些结果。

insert into #student_results
values 
(23096, 7,  576, 'reading' )
, (23096,   7,  640, 'writing' )
, (23096,   7,  704, 'spelling' )
, (23096,   7,  768, 'numeracy' )
, (23096,   8,  960, 'writing')
, (23096,   8,  1024, 'spelling')
, (23096,   8,  1088, 'numeracy')
, (23096,   8,  1152, 'grammarPunctuation')
, (23104,   8,  1216, 'reading')
, (23104,   8,  1280, 'writing')
, (23104,   8,  1344, 'spelling')
, (23104,   8,  1408, 'numeracy')
, (23104,   8,  1472, 'grammarPunctuation')
, (23116,   8,  1536, 'reading')
, (23116,   8,  1600, 'writing')
, (23116,   8,  1664, 'spelling')
, (23116,   8,  1728, 'grammarPunctuation')
, (23117,   7,  1792, 'reading')
, (23117,   7,  1856, 'writing')
, (23117,   7,  1920, 'spelling')
, (23117,   7,  1984, 'numeracy')
, (23117,   7,  2048, 'grammarPunctuation')
;

使用CTE,我们打破了逻辑。

  1. 我们创建一个包含StudentID和Dimension的所有组合的表格。
  2. 使用7年级的结果创建表格
  3. 使用8年级的结果创建表格
  4. 合并结果
with students
    as (select studentID
            , Dimension
        from   #student_results
        group by studentID
             , Dimension),
    year7
    as (select *
        from   #student_results
        where  year_level = 7),
    year8
    as (select *
        from   #student_results
        where  year_level = 8)
    select students.studentID
        , students.Dimension
        , year7.Results as Year7_Results
        , year8.Results as Year8_Results
    from   students
    left join year7
            on students.studentID = year7.studentID
              and students.Dimension = year7.Dimension
    left join year8
            on students.studentID = year8.studentID
              and students.Dimension = year8.Dimension;

result