SQL Server透视而不计算

时间:2018-01-29 07:28:38

标签: sql sql-server pivot transpose

我想转动一张桌子,我试过自我加入,但它很慢。请帮我做这个。下面是我的桌子结构。

STUDENT_ID   AC_YEAR    AC_TERM     STANDING      ENRL_TERM_SEQ 
10001         2015       SPRING        NULL           1          
10001         2015       FALL          NULL           2          
10001         2016       SPRING         W             3          
10001         2016       FALL           W             4          
10002         2015       SPRING        NULL           1          
10002         2015       FALL           W             2          
10002         2016       SPRING        NULL           3          
10002         2016       FALL          NULL           4          
10005         2015       SPRING        NULL           1          
10005         2015       FALL          NULL           2         
10005         2016       SPRING        NULL           3          
10005         2016       SUMMER        NULL           4          
10005         2016       FALL          NULL           5  

我想像下面那样转动

ST_ID  CUR_YR  CUR_TM CUR_SD  PR1_YR  PR1_TM  PR1_SD  PR2_YR PR_TM  PR2_SD
10001  2016    FALL    W      2016    SPRING    W     2015    FALL   NULL
10002  2016    FALL   NULL    2016    SPRING   NULL   2015    FALL    W 
10005  2016    FALL   NULL    2016    SUMMER   NULL   2016   SPRING  NULL

我可以用Pivot做到这一点吗?

请注意,在结果集中,由于空间限制我更改了字段名称

提前致谢。

我可以获得如下所示的行吗?

ST_ID  CUR_YR  CUR_TM CUR_SD  PR1_YR  PR1_TM  PR1_SD  PR2_YR PR_TM  PR2_SD 
10001  2015    SPRING   NULL   NULL   NULL    NULL    NULL   NULL    NULL
10001  2015    FALL     NULL   2015   SPRING  NULL    NULL   NULL    NULL
10001  2016    SPRING    W     2015   FALL    NULL    2015   SPRING  NULL
10001  2016    FALL      W     2016   SPRING   W      2015   FALL    NULL
10002  2015    SPRING   NULL   NULL   NULL    NULL    NULL   NULL    NULL
10002  2015    FALL      W     2015   SPRING  NULL    NULL   NULL    NULL
10002  2016    SPRING   NULL   2015   FALL     W      2015   SPRING  NULL
10002  2016    FALL     NULL   2016   SPRING  NULL    2015   FALL     W
10005  2015    SPRING   NULL   NULL   NULL    NULL    NULL   NULL    NULL
10005  2015    FALL     NULL   2015   SPRING  NULL    NULL   NULL    NULL
10005  2016    SPRING   NULL   2015   FALL    NULL    2015   SPRING  NULL
10005  2016    SUMMER   NULL   2016   SPRING  NULL    2015   FALL    NULL
10005  2016    FALL     NULL   2016   SUMMER  NULL    2016   SPRING  NULL

2 个答案:

答案 0 :(得分:1)

此查询将返回最后三个术语。如果您需要更多添加额外列并更改rn值:

select
    STUDENT_ID
    , max(case when rn = 1 then AC_YEAR end), max(case when rn = 1 then AC_TERM end), max(case when rn = 1 then STANDING end)
    , max(case when rn = 2 then AC_YEAR end), max(case when rn = 2 then AC_TERM end), max(case when rn = 2 then STANDING end)
    , max(case when rn = 3 then AC_YEAR end), max(case when rn = 3 then AC_TERM end), max(case when rn = 3 then STANDING end)
from (
    select
        *, row_number() over (partition by STUDENT_ID order by AC_YEAR desc, case AC_TERM when 'FALL' then 1 when 'SUMMER' then 2 when 'SPRING' then 3 end) rn
    from
        myTable
) t
where
    rn <= 3
group by STUDENT_ID

答案 1 :(得分:1)

根据ENRL_TERM_SEQ为相应学生提取最后三个学期并提取所需的专栏。

SELECT A.STUDENT_ID,

MAX(CASE WHEN A.TERM = 1 THEN A.AC_YEAR END) AS CUR_YR,
MAX(CASE WHEN A.TERM = 1 THEN A.AC_TERM END) AS CUR_TM,
MAX(CASE WHEN A.TERM = 1 THEN A.STANDING END) AS CUR_SD,

MAX(CASE WHEN A.TERM = 2 THEN A.AC_YEAR END) AS PR1_YR,
MAX(CASE WHEN A.TERM = 2 THEN A.AC_TERM END) AS PR1_TM,
MAX(CASE WHEN A.TERM = 2 THEN A.STANDING END) AS PR1_SD,

MAX(CASE WHEN A.TERM = 3 THEN A.AC_YEAR END) AS PR2_YR,
MAX(CASE WHEN A.TERM = 3 THEN A.AC_TERM END) AS PR2_YM,
MAX(CASE WHEN A.TERM = 3 THEN A.STANDING END) AS PR2_SD

FROM

(SELECT *, DENSE_RANK() OVER (PARTITION BY STUDENT_ID ORDER BY ENRL_TERM_SEQ DESC) AS TERM FROM YOUR_TABLE) A

GROUP BY A.STUDENT_ID
ORDER BY A.STUDENT_ID;