我有一个包含此值的表:
ITPROD PROD1 PROD2 Quantity 1 Quantity 2
45842 69640 63908 3 2
70690 91387 90734 1 2
结果表应为:
ITPROD PROD Quantity
45842 69640 3
45842 63908 2
70690 91387 1
70690 90734 2
答案 0 :(得分:1)
select itprod, prof1 as prod, quantity1 as quantity from your_table
union all
select itprod, prof2, quantity2 from your_table
答案 1 :(得分:1)
将cross apply()
与values()
一起使用:
select
t.itprod
, v.Prod
, v.Quantity
from t
cross apply (values
(Prod1,Quantity1)
, (Prod2,Quantity2)
) v(Prod,Quantity)
rextester演示:http://rextester.com/MFDCA68129
返回:
+--------+-------+----------+
| itprod | Prod | Quantity |
+--------+-------+----------+
| 45842 | 69640 | 3 |
| 45842 | 63908 | 2 |
| 70690 | 91387 | 1 |
| 70690 | 90734 | 2 |
+--------+-------+----------+
答案 2 :(得分:0)
试试这个,
SELECT ITPROD AS ITPROD,PROD1 AS PROD,Quantity1 AS Quantity1 FROM SampleTable
UNION ALL
SELECT ITPROD AS ITPROD,PROD2 AS PROD,Quantity2 AS Quantity1 FROM SampleTable