Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
Harhour
Starting Member
3 Posts |
Posted - 2010-10-31 : 05:24:23
|
hI ALLthis i my statementselect item_code,order_id ,wk1_no,wk2_no,wk3_no,wk4_no,wk5_no,wk6_no,wk7_no,wk8_no,idwk from kdr_order_dtlwhere order_id='2010_10_order_405'i get 24 rows item_code order_id wk1_no wk2_no wk3_no wk4_no wk5_no wk6_no wk7_no wk8_no idwk002K7X1 2010_10_Order_405 week47 week48 week49 week50 week51 week52 week1 week2 2275002K7X1 2010_10_Order_405 week47 week48 week49 week50 week51 week52 week1 week2 2276002K7X1 2010_10_Order_405 week47 week48 week49 week50 week51 week52 week1 week2 2277002K7X1 2010_10_Order_405 week47 week48 week49 week50 week51 week52 week1 week2 2278002K7X1 2010_10_Order_405 week47 week48 week49 week50 week51 week52 week1 week2 2279002K7X1 2010_10_Order_405 week47 week48 week49 week50 week51 week52 week1 week2 2280002K7X1 2010_10_Order_405 week47 week48 week49 week50 week51 week52 week1 week2 2281002K7X1 2010_10_Order_405 week47 week48 week49 week50 week51 week52 week1 week2 2282002L0G7 2010_10_Order_405 week47 week48 week49 week50 week51 week52 week1 week2 2283002L0G7 2010_10_Order_405 week47 week48 week49 week50 week51 week52 week1 week2 2284002L0G7 2010_10_Order_405 week47 week48 week49 week50 week51 week52 week1 week2 2285002L0G7 2010_10_Order_405 week47 week48 week49 week50 week51 week52 week1 week2 2286002L0G7 2010_10_Order_405 week47 week48 week49 week50 week51 week52 week1 week2 2287002L0G7 2010_10_Order_405 week47 week48 week49 week50 week51 week52 week1 week2 2288002L0G7 2010_10_Order_405 week47 week48 week49 week50 week51 week52 week1 week2 2289002L0G7 2010_10_Order_405 week47 week48 week49 week50 week51 week52 week1 week2 2290002M7D8 2010_10_Order_405 week47 week48 week49 week50 week51 week52 week1 week2 2291002M7D8 2010_10_Order_405 week47 week48 week49 week50 week51 week52 week1 week2 2292002M7D8 2010_10_Order_405 week47 week48 week49 week50 week51 week52 week1 week2 2293002M7D8 2010_10_Order_405 week47 week48 week49 week50 week51 week52 week1 week2 2294002M7D8 2010_10_Order_405 week47 week48 week49 week50 week51 week52 week1 week2 2295002M7D8 2010_10_Order_405 week47 week48 week49 week50 week51 week52 week1 week2 2296002M7D8 2010_10_Order_405 week47 week48 week49 week50 week51 week52 week1 week2 2297002M7D8 2010_10_Order_405 week47 week48 week49 week50 week51 week52 week1 week2 2298 I used union to transform columns wk1_no wk2_no wk3_no wk4_no wk5_no wk6_no wk7_no wk8_no into rows and have the column name Wk NBSELECT DISTINCT TOP (100) PERCENT Item_code, Order_id, Wk1_no AS [WK NB]FROM KDR_Order_DtlWHERE (Order_id = '2010_10_order_405')UNION ALLSELECT DISTINCT TOP (100) PERCENT Item_code, Order_id, Wk2_noFROM KDR_Order_Dtl AS KDR_Order_Dtl_7WHERE (Order_id = '2010_10_order_405')UNION ALLSELECT DISTINCT TOP (100) PERCENT Item_code, Order_id, Wk3_noFROM KDR_Order_Dtl AS KDR_Order_Dtl_6WHERE (Order_id = '2010_10_order_405')UNION ALLSELECT DISTINCT TOP (100) PERCENT Item_code, Order_id, Wk4_noFROM KDR_Order_Dtl AS KDR_Order_Dtl_5WHERE (Order_id = '2010_10_order_405')UNION ALLSELECT DISTINCT TOP (100) PERCENT Item_code, Order_id, Wk5_noFROM KDR_Order_Dtl AS KDR_Order_Dtl_4WHERE (Order_id = '2010_10_order_405')UNION ALLSELECT DISTINCT TOP (100) PERCENT Item_code, Order_id, Wk6_noFROM KDR_Order_Dtl AS KDR_Order_Dtl_3WHERE (Order_id = '2010_10_order_405')UNION ALLSELECT DISTINCT TOP (100) PERCENT Item_code, Order_id, Wk7_noFROM KDR_Order_Dtl AS KDR_Order_Dtl_2WHERE (Order_id = '2010_10_order_405')UNION ALLSELECT DISTINCT TOP (100) PERCENT Item_code, Order_id, Wk8_noFROM KDR_Order_Dtl AS KDR_Order_Dtl_1WHERE (Order_id = '2010_10_order_405')ORDER BY Item_code I am getting 24 rows Item_code Order_id WK NB002K7X1 2010_10_Order_405 week1002K7X1 2010_10_Order_405 week2002K7X1 2010_10_Order_405 week47002K7X1 2010_10_Order_405 week48002K7X1 2010_10_Order_405 week49002K7X1 2010_10_Order_405 week50002K7X1 2010_10_Order_405 week51002K7X1 2010_10_Order_405 week52002L0G7 2010_10_Order_405 week1002L0G7 2010_10_Order_405 week2002L0G7 2010_10_Order_405 week47002L0G7 2010_10_Order_405 week48002L0G7 2010_10_Order_405 week49002L0G7 2010_10_Order_405 week50002L0G7 2010_10_Order_405 week51002L0G7 2010_10_Order_405 week52002M7D8 2010_10_Order_405 week1002M7D8 2010_10_Order_405 week2002M7D8 2010_10_Order_405 week47002M7D8 2010_10_Order_405 week48002M7D8 2010_10_Order_405 week49002M7D8 2010_10_Order_405 week50002M7D8 2010_10_Order_405 week51002M7D8 2010_10_Order_405 week52 Here is my problem i want to add the column IDWK from the first statement to the second statement (Union) and to get 24 rows i want like this for order 2010_10_Order_405 Item_code Order_id WK NB IDWK002K7X1 2010_10_Order_405 week1 2275002K7X1 2010_10_Order_405 week2 2276002K7X1 2010_10_Order_405 week47 2278002K7X1 2010_10_Order_405 week48 2279002K7X1 2010_10_Order_405 week49 2280002K7X1 2010_10_Order_405 week50 2281002K7X1 2010_10_Order_405 week51 2282002K7X1 2010_10_Order_405 week52 2283002L0G7 2010_10_Order_405 week1 2284002L0G7 2010_10_Order_405 week2 2285002L0G7 2010_10_Order_405 week47 2286002L0G7 2010_10_Order_405 week48 2287002L0G7 2010_10_Order_405 week49 2288002L0G7 2010_10_Order_405 week50 2289002L0G7 2010_10_Order_405 week51 2290002L0G7 2010_10_Order_405 week52 2291002M7D8 2010_10_Order_405 week1 2292002M7D8 2010_10_Order_405 week2 2293002M7D8 2010_10_Order_405 week47 2294002M7D8 2010_10_Order_405 week48 2295002M7D8 2010_10_Order_405 week49 2296002M7D8 2010_10_Order_405 week50 2297002M7D8 2010_10_Order_405 week51 2298002M7D8 2010_10_Order_405 week52 2299 and then i want the same output but for all order numbers more tan 24 rows.. thanks in return |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2010-10-31 : 08:37:54
|
Have you considered the new UNPIVOT operator?SELECT u.Item_Code, u.Order_ID, u.WeekNumber, u.IdWkFROM dbo.kdr_order_dtl AS sUNPIVOT ( WeekNumber FOR theCol IN (s.wk1_no, s.wk2_no, s.wk3_no, s.wk4_no, s.wk5_no, s.wk6_no, s.wk7_no, s.wk8_no) ) AS u N 56°04'39.26"E 12°55'05.63" |
|
|
|
|
|
|
|