SQL语言急求解决方法
SQL语言急求
,
请问一下如这图所示,怎么根据查询结果和表tb1写出SQL语句,今天面试的时候遇到,没有答出了,郁闷啊!!请各位大神不吝赐教,谢谢!!
------解决思路----------------------
with tb1(id,[year],[Quarter]) as
(
select 1, 2001, 1 union all
select 2, 2001, 2 union all
select 3, 2001, 3 union all
select 4, 2001, 4 union all
select 5, 2002, 1 union all
select 6, 2002, 2 union all
select 7, 2002, 3 union all
select 8, 2002, 4
)
, data as
(
select [year],[Quarter] from tb1
)
select
[year]
,[1] as Quarter1
,[2] as Quarter2
,[3] as Quarter3
,[4] as Quarter4
from data
pivot (
max ([Quarter])
for [Quarter] in ([1],[2],[3],[4])
)as pivortable;
请问一下如这图所示,怎么根据查询结果和表tb1写出SQL语句,今天面试的时候遇到,没有答出了,郁闷啊!!请各位大神不吝赐教,谢谢!!
------解决思路----------------------
with tb1(id,[year],[Quarter]) as
(
select 1, 2001, 1 union all
select 2, 2001, 2 union all
select 3, 2001, 3 union all
select 4, 2001, 4 union all
select 5, 2002, 1 union all
select 6, 2002, 2 union all
select 7, 2002, 3 union all
select 8, 2002, 4
)
, data as
(
select [year],[Quarter] from tb1
)
select
[year]
,[1] as Quarter1
,[2] as Quarter2
,[3] as Quarter3
,[4] as Quarter4
from data
pivot (
max ([Quarter])
for [Quarter] in ([1],[2],[3],[4])
)as pivortable;