外键列的SQL透视
我有一张这样的桌子: 会计年度、地区、国家、办公室1、办公室2、办公室3、办公室4 其中office 1-4是外键 我希望得到如下输出: 办公室1:财政年度、地区、国家 办公室2:财政年度、地区、国家 办公室3:财政年度、地区、国家 办公室4:财政年度、地区、国家外键列的SQL透视,sql,sql-server,foreign-keys,pivot,Sql,Sql Server,Foreign Keys,Pivot,我有一张这样的桌子: 会计年度、地区、国家、办公室1、办公室2、办公室3、办公室4 其中office 1-4是外键 我希望得到如下输出: 办公室1:财政年度、地区、国家 办公室2:财政年度、地区、国家 办公室3:财政年度、地区、国家 办公室4:财政年度、地区、国家 这可以通过pivot实现吗?我认为这更像是UNPIVOT: SELECT [Fiscal Year], Region, County, OFfice FROM (SELECT [Fiscal Year], Region, Co
这可以通过pivot实现吗?我认为这更像是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