Rus*_*rne 4 sql t-sql pivot unpivot sql-server-2008
我有一张相当宽的桌子,我想转换成高大的.目前的数据如下:
VEND YEAR I1_DOLS I1_QTY I2_DOLS I2_QTY I3_DOLS I3_QTY ...
1234 2011 101587 508 203345 334 105938 257
1234 2012 257843 587 235883 247 178475 456
1011 2010 584737 432 587274 356 175737 563
1011 2011 517774 356 483858 456 481785 354
Run Code Online (Sandbox Code Playgroud)
我想将其转换为如下所示的表:
VEND YEAR MONTH DOLS QTY
1234 2011 1 101587 508
1234 2011 2 203345 334
1234 2011 3 105938 257
1234 2012 1 257843 587
1234 2012 2 235883 247
.
.
.
Run Code Online (Sandbox Code Playgroud)
我认为PIVOT是我需要的,但我似乎无法弄清楚这一点.
您可以使用以下方式取消数据CROSS APPLY (VALUES)
.这是一篇解释如何完成的文章:
http://www.sqlservercentral.com/articles/CROSS+APPLY+VALUES+UNPIVOT/91234/
基本上代码是:
SELECT vend,
year,
month,
dols,
qty
FROM YourTable t
CROSS APPLY
(
VALUES
(1, I1_DOLS, I1_QTY),
(2, I2_DOLS, I2_QTY),
(3, I3_DOLS, I3_QTY)
) x (month, dols, qty);
Run Code Online (Sandbox Code Playgroud)
或者您可以使用UNION ALL
查询:
select vend, year, 1 month, [I1_DOLS] Dols, [I1_QTY] Qty
from yourtable
union all
select vend, year, 2 month, [I2_DOLS] Dols, [I2_QTY] Qty
from yourtable
union all
select vend, year, 3 month, [I3_DOLS] Dols, [I3_QTY] Qty
from yourtable
Run Code Online (Sandbox Code Playgroud)
或者你甚至可以应用UNPIVOT
和PIVOT
函数来转换数据:
select *
from
(
select vend,
year,
replace(replace(replace(col, 'I', ''), '_Dols', ''), '_Qty', '') month,
case when col like '%Dols%' then 'dols' else 'qty' end col_name,
value
from
(
select vend, year, [I1_DOLS], [I1_QTY], [I2_DOLS], [I2_QTY], [I3_DOLS], [I3_QTY]
from yourtable
) src
unpivot
(
value
for col in ([I1_DOLS], [I1_QTY], [I2_DOLS], [I2_QTY], [I3_DOLS], [I3_QTY])
) un
) unp
pivot
(
max(value)
for col_name in (dols, qty)
) piv
Run Code Online (Sandbox Code Playgroud)
这三个都会给出相同的结果:
| VEND | YEAR | MONTH | DOLS | QTY |
--------------------------------------
| 1234 | 2011 | 1 | 101587 | 508 |
| 1234 | 2011 | 2 | 203345 | 334 |
| 1234 | 2011 | 3 | 105938 | 257 |
| 1234 | 2012 | 1 | 257843 | 587 |
| 1234 | 2012 | 2 | 235883 | 247 |
| 1234 | 2012 | 3 | 178475 | 456 |
| 1011 | 2010 | 1 | 584737 | 432 |
| 1011 | 2010 | 2 | 587274 | 356 |
| 1011 | 2010 | 3 | 175737 | 563 |
| 1011 | 2011 | 1 | 517774 | 356 |
| 1011 | 2011 | 2 | 483858 | 456 |
| 1011 | 2011 | 3 | 481785 | 354 |
Run Code Online (Sandbox Code Playgroud)