将行转置为 SQL 中的列

Transpose rows into columns in SQL

我有 2 周的数据来自复杂的 'With statement'-

DATE        WEEKDAY     CODE
2015/01/05  Monday      A
2015/01/06  Tuesday     A
2015/01/07  Wednesday   P
2015/01/08  Thursday    A
2015/01/09  Friday      P
2015/01/12  Monday      P
2015/01/13  Tuesday     P
2015/01/14  Wednesday   P
2015/01/15  Thursday    P
2015/01/16  Friday      P

我想像这样换算成两周 -

WEEK        Monday  Tuesday Wednesday   Thursday    Friday
2015/01/05  A       A       P           A           P
2015/01/12  P       P       P           P           P

With PS_Att_pivot_data AS (--------------)  SELECT * FROM ( SELECT (Case when Weekday ='Monday' then SchoolDay end) AS Week, Weekday, Att_code FROM PS_Att_pivot_data ) PIVOT (max(Att_code) for Weekday in ('Monday', 'Tuesday','Wednesday','Thursday','Friday'))

我的脚本给出的结果是这样的 -

WEEK     'Monday'   'Tuesday'   'Wednesday' 'Thursday'  'Friday'
(null)  (null)       P           P            P          P 
2015/01/12  P         (null)    (null)  (null)  (null)
2015/01/05  A        (null)     (null)  (null)  (null)

请帮我做 Pivot 语句。 谢谢,

听起来这可能就是您想要的:

with sample_data as (select to_date('05/01/2015', 'dd/mm/yyyy') dt, 'Monday' weekday, 'A' code from dual union all
                     select to_date('06/01/2015', 'dd/mm/yyyy') dt, 'Tuesday' weekday, 'A' code from dual union all
                     select to_date('07/01/2015', 'dd/mm/yyyy') dt, 'Wednesday' weekday, 'P' code from dual union all
                     select to_date('08/01/2015', 'dd/mm/yyyy') dt, 'Thursday' weekday, 'A' code from dual union all
                     select to_date('09/01/2015', 'dd/mm/yyyy') dt, 'Friday' weekday, 'P' code from dual union all
                     select to_date('12/01/2015', 'dd/mm/yyyy') dt, 'Monday' weekday, 'P' code from dual union all
                     select to_date('13/01/2015', 'dd/mm/yyyy') dt, 'Tuesday' weekday, 'P' code from dual union all
                     select to_date('14/01/2015', 'dd/mm/yyyy') dt, 'Wednesday' weekday, 'P' code from dual union all
                     select to_date('15/01/2015', 'dd/mm/yyyy') dt, 'Thursday' weekday, 'P' code from dual union all
                     select to_date('16/01/2015', 'dd/mm/yyyy') dt, 'Friday' weekday, 'P' code from dual)
-- end of mimicking your data in a table called sample_data
select *
from   (select trunc(dt, 'iw') week_start_dt,
               weekday,
               code
        from   sample_data) 
pivot  (max(code) for weekday in ('Monday' as monday, 
                                  'Tuesday' as tuesday,
                                  'Wednesday' as wednesday,
                                  'Thursday' as thursday,
                                  'Friday' as friday))
order by week_start_dt;

WEEK_START_DT MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY
------------- ------ ------- --------- -------- ------
2015/01/05    A      A       P         A        P     
2015/01/12    P      P       P         P        P     

真心希望WEEKDAY是你真实代码中的dt列派生出来的!