我有這個值AA表:劈裂行入列時,值出現在SQL Server
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
我有這個值AA表:劈裂行入列時,值出現在SQL Server
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
select itprod, prof1 as prod, quantity1 as quantity from your_table
union all
select itprod, prof2, quantity2 from your_table
試試這個,
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
使用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 |
+--------+-------+----------+