我有一张这样的桌子: 会计年度,地区,国家/地区,Office1,Office2,Office3,Office4
办公室1-4是外键。
我想得到这样的输出: 办公室1:财政年度,地区,国家 办公室2:财政年度,地区,国家 办公室3:财政年度,地区,国家 办公室4:财政年度,地区,国家
可以使用pivot来完成吗?
答案 0 :(得分:1)
这更像是我认为的UNPIVOT:
SELECT [Fiscal Year], Region, County, OFfice
FROM
(SELECT [Fiscal Year], Region, County, OFfice1, Office2, Office3, Office4
FROM unpvt) p
UNPIVOT
(yourtable FOR Office IN
(Office1, Office2, Office3, Office4)
) AS unpvt;
但你也可以通过一个简单的查询来做到这一点:
select [Fiscal Year], Region, County, OFfice1
from yourtable
union
select [Fiscal Year], Region, County, OFfice2
from yourtable
union
select [Fiscal Year], Region, County, OFfice3
from yourtable
union
select [Fiscal Year], Region, County, OFfice4
from yourtable