由于我需要行中的日期而不是列中的日期,因此我试图取消对该表的透视。我相对较新,但我认为Unpivot应该有效。但是,它表示“语法错误”,这是意外的(在所有地方,我看到UNPIVOT命令后都有一个括号。
我尝试了其他子句来封装它,或者根本不使用它,但是总是出现相同的错误。应该使用标准SQL。
SELECT service_id, day, service
FROM calendar
UNPIVOT
(
service
FOR day in ( monday, tuesday, wednesday, thursday, friday, saturday, sunday )
) AS unpvt;
应该获得一个列表,其列标题应为:service_id,day,service
答案 0 :(得分:0)
您可以尝试这种方式-
SELECT service_id, service, day
FROM calendar
UNPIVOT
(
S FOR day in ( [monday], [tuesday], [wednesday], [thursday], [friday], [saturday], [sunday] )
) AS unpvt;
答案 1 :(得分:0)
标准SQL中没有unpivot
。我偏爱的方法是横向连接,但这因数据库而异。
通用方法是仅使用union all
:
select service_id, 'monday' as day, monday as service
from calendar
union all
select service_id, 'tuesday' as day, tuesday as service
from calendar
union all
select service_id, 'wednesday' as day, wednesday as service
from calendar
union all
select service_id, 'thursday' as day, thursday as service
from calendar
union all
select service_id, 'friday' as day, friday as service
from calendar
union all
select service_id, 'saturday' as day, saturday as service
from calendar
union all
select service_id, 'sunday' as day, sunday as service
from calendar;