简体   繁体   English

带列查询的UNPIVOT SQL

[英]UNPIVOT SQL with columns query

I have table Price : 我有餐桌Price

WhseKey ItemKey CurrID  Sheet1Price  Sheet2Price Sheet3Price   Sheet4Price
24      452     USD     14.90000     14.90000   13.70000       12.50000    11.03000
24      453     USD     1.15000      1.15000    1.05000        0.95000     0.85000
24      454     USD     12.95000     12.95000   11.90000       10.88000    9.70000  
24      459     USD     3.95000      3.95000    3.65000        3.30000     2.92000

I want the result to be like: 我希望结果像这样:

CurrID Name ID 
USD Sheet1Price Sheet1Price
USD Sheet2Price Sheet2Price
USD Sheet3Price Sheet3Price
USD Sheet4Price Sheet4Price

Current query: 当前查询:

select UPV.CurrID , UPV.Name
from Price
unpivot
(
    Name
    for Price in (Sheet1Price, Sheet2Price, Sheet3Price, Sheet4Price)
) UPV;

But the result is not correct, it comes as: 但是结果不正确,它是:

CurrID Name
USD 14.90000
USD 13.70000
USD 12.50000
USD 11.03000
USD 1.15000
USD 1.05000
USD 0.95000
USD 0.85000
USD 12.95000
USD 11.90000

What i am missing? 我想念什么?

To output filed names you should use UPV.Price , Try this: 要输出文件名,您应该使用UPV.Price ,请尝试以下操作:

select WhseKey, ItemKey, UPV.CurrID , UPV.Name, UPV.Price
from Price
unpivot
(
    Price
    for Name in (Sheet1Price, Sheet2Price, Sheet3Price, Sheet4Price)
) UPV;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM