2017-04-25 8 views
0

に発生したとき、私は、この値を持つAAテーブルを持っている:Spliting行値は、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   

答えて

1
select itprod, prof1 as prod, quantity1 as quantity from your_table 
union all 
select itprod, prof2, quantity2 from your_table 
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 
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 | 
+--------+-------+----------+