嗨我有结构表
display_order period_to_date from_date thru_date beginning_market_value net_flows net_capital_gain net_income ending_market_value net_invested_capital inv_progress
1 MTD 03/31/2015 04/30/2015 36812682.75 -758103.94 139045.6376 51236.84278 36244861.29 36054578.81 190282.4803
2 QTD 03/31/2015 04/30/2015 36812682.75 -758103.94 139045.6376 51236.84278 36244861.29 36054578.81 190282.4803
3 YTD 12/31/2014 04/30/2015 36943568.78 -1508486.51 558539.325 251239.695 36244861.29 35435082.27 809779.02
4 INCP 05/09/2002 04/30/2015 14760256.05 -4742497.95 16969340.37 9257762.809 36244861.29 10017758.1 26227103.18
我希望将输出显示为
MTD QTD YTD INCP
from_date 03/31/2015 03/31/2015 12/31/2014 05/09/2002
thru_date 04/30/2015 04/30/2015 04/30/2015 04/30/2015
beginning_market_value 36812682.75 36812682.75 36943568.78 14,760,256.05
net_flows -758103.94 -758103.94 -1508486.51 -4742497.95
net_capital_gain 139045.6376 139045.6376 558539.325 16969340.37
net_income 51236.84278 51236.84278 251239.695 9257762.809
ending_market_value 36244861.29 36244861.29 36244861.29 36,244,861.29
net_invested_capital 36054578.81 36054578.81 35435082.27 10,017,758.10
inv_progress 190282.4803 190282.4803 809779.02 26,227,103.18
但是我无法得到这个结果..我正在使用的是
select category,
max(case when period_to_date = 'MTD' then value end) [MTD],
max(case when period_to_date = 'QTD' then value end) [QTD],
max(case when period_to_date = 'MTD' then value end) [YTD],
max(case when period_to_date = 'MTD' then value end) [INCP]
from
(
--select period_to_date, from_date value, 'from_date' category
--from transtable
--union all
--select period_to_date, thru_date value, 'thru_date' category
--from transtable
--union all
select period_to_date, beginning_market_value value, 'beginning_market_value' category
from transtable
union all
select period_to_date, net_flows value, 'net_flows' category
from transtable
union all
select period_to_date, net_capital_gain value, 'net_capital_gain' category
from transtable
union all
select period_to_date, net_income value, 'net_income' category
from transtable
union all
select period_to_date, ending_market_value value, 'ending_market_value' category
from transtable
union all
select period_to_date, net_invested_capital value, 'net_invested_capital' category
from transtable
union all
select period_to_date, inv_progress value, 'inv_progress' category
from transtable
) un
group by category
但它没有给出正确的结果..它显示所有4列的相同值
category MTD QTD YTD INCP
beginning_market_value 36812682.7481145 36812682.7481145 36812682.7481145 36812682.7481145
ending_market_value 36244861.2884538 36244861.2884538 36244861.2884538 36244861.2884538
inv_progress 190282.480339332 190282.480339332 190282.480339332 190282.480339332
net_capital_gain 139045.637561555 139045.637561555 139045.637561555 139045.637561555
net_flows -758103.939999958 -758103.939999958 -758103.939999958 -758103.939999958
net_income 51236.842777777 51236.842777777 51236.842777777 51236.842777777
net_invested_capital 36054578.8081145 36054578.8081145 36054578.8081145 36054578.8081145
我如何达到预期的效果? 提前致谢
答案 0 :(得分:2)
尝试这样的事情......
SELECT * FROM
(
SELECT CAST(period_to_date AS VARCHAR(20)) AS period_to_date
,CAST(from_date AS VARCHAR(20)) AS from_date
,CAST(thru_date AS VARCHAR(20)) AS thru_date
,CAST(beginning_market_value AS VARCHAR(20)) AS beginning_market_value
,CAST(net_flows AS VARCHAR(20)) AS net_flows
,CAST(net_capital_gain AS VARCHAR(20)) AS net_capital_gain
,CAST(net_income AS VARCHAR(20)) AS net_income
,CAST(ending_market_value AS VARCHAR(20)) AS ending_market_value
,CAST(net_invested_capital AS VARCHAR(20)) AS net_invested_capital
,CAST(inv_progress AS VARCHAR(20)) AS inv_progress
FROM TableName)T
UNPIVOT (Vals FOR Category IN (from_date,thru_date,beginning_market_value,net_flows,net_capital_gain
,net_income,ending_market_value,net_invested_capital,inv_progress)
)up
PIVOT (MAX(Vals)
FOR period_to_date IN (MTD, QTD,YTD,INCP)
)P
SQL FIDDLE