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 |
eb1962
Starting Member
6 Posts |
Posted - 2006-10-09 : 09:19:15
|
I am loosing my mind over a rather "simple" matter.I have a table which I have created by summarizing some quantities from a detailed order table, in order to have summarized quantities by product, through the following SQL:INSERT INTO SUMMARIZED_ORDER_TAB(ORDER_ID, PRODUCT_ID, QUANTITY)SELECT ORDER_ID, PRODUCT_ID, SUM(QUANTITY) AS QUANTITYFROM DETAILED_ORDER_TABGROUP BY ORDER_ID, PRODUCT_IDHAVING (ORDER_ID IN(SELECT ORDER_IDFROM DETAILED_ORDER_TAB AS TmpGROUP BY ORDER_ID, PRODUCT_IDHAVING COUNT(*) > 0 AND PRODUCT_ID = DETAILED_ORDER_TAB.PRODUCT_ID))ORDER BY ORDER_ID, PRODUCT_IDNow I must number those lines.The structure of the SUMMARIZED_ORDER_TAB is the following:ORDER_ID varchar (20)ORDER_LINE_ID smallintPRODUCT_ID varchar (20)QUANTITY decimalThe order line id is currently blank in ALL records and the following records are available:+ ORDER_ID + ORDER_LINE_ID + PRODUCT_ID + QTY ++----------+---------------+------------+-----+| 1 | | ABC1 | 100 || 1 | | ABC2 | 130 || 1 | | ABC3 | 20 || 2 | | ABC1 | 200 || 2 | | ABC3 | 10 |Result should be:+ ORDER_ID + ORDER_LINE_ID + PRODUCT_ID + QTY ++----------+---------------+------------+-----+| 1 | 1 | ABC1 | 100 || 1 | 2 | ABC2 | 130 || 1 | 3 | ABC3 | 20 || 2 | 1 | ABC1 | 200 || 2 | 2 | ABC3 | 10 |What would you suggest to create the ORDER_LINE_ID directly when creating the new table or to amend it at a later stage?How can I do it?I have tried in several ways but I am not getting anywhere...Could you please help me?Thanks,Emanuele |
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2006-10-09 : 09:31:20
|
[code]Select Order_Id, (Select count(*) from SUMMARIZED_ORDER_TAB y where y.Order_ID = x.Order_ID and y.ProductID <= x.ProductID) as Order_Line_ID, Product_ID, QtyFrom SUMMARIZED_ORDER_TAB xorder by x.order_id, product_id[/code]Harsh AthalyeIndia."Nothing is Impossible" |
 |
|
eb1962
Starting Member
6 Posts |
Posted - 2006-10-09 : 09:46:39
|
First, I do apologize for having cross my post... I am new and I hope u do bear with me.Harsh, I have tried what you have suggested but it doesn't give the expected results...Here they are, between parenthesys I have written what they should be: ORDER LINE PRODUCT QTY 016342 1 (1) 5002720 6,3 016342 3 (2) 5112100 18,298 016342 3 (3) 5112101 54,388 016342 4 (4) 5205800 24900 016343 1 (1) 5107600 750 016343 2 (2) 5245500 56,25 016343 4 (3) 5266000 85,056 016343 4 (4) 5266001 27,444 016470 1 (1) 5204600 877,5 016470 2 (2) 5207000 21,965 016470 3 (3) 7870230 87,75 016474 5 (1) 1170050 19355 016474 5 (2) 1170051 19481 016474 5 (3) 1170052 19770 016474 5 (4) 1170053 18932 016474 5 (5) 1170054 19569 016474 6 (6) 3445220 104494 016474 7 (7) 603022100 32716 016474 8 (8) 6129810 771 016474 9 (9) 614397500 113354 016474 10(10) 628010600 32216 016474 11(11) 6340049 237,399 016474 12(12) BO020660066 39273And finally I must update the table with these new line numbers...Thanks everybody for your patience too! |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-09 : 10:20:26
|
Take a look at the cross postings, you might have got an answer there.Peter LarssonHelsingborg, Sweden |
 |
|
eb1962
Starting Member
6 Posts |
Posted - 2006-10-09 : 10:42:51
|
I figured there is an extra parameter I have to take into account being a lot number.....Thus the new structure isORDER_IDORDER_LINE_IDPRODUCT_IDLOT_NUMBERQuantitySo if I haveORDER ORDER_LINE_ID PRODUCT_ID LOT_NUMBER QUANTITY1 ABC ABC1 101 ABC ABC2 151 ABC ABC3 201 ABD ABD1 51 ABD ABD2 31 ABE ABE1 2 2 ABC ABC1 102 ABC ABC2 152 ABC ABC3 202 ABD ABD1 53 ABC ABC1 103 ABC ABC2 153 ABC ABC3 203 ABD ABD2 3This should result in:ORDER ORDER_LINE_ID PRODUCT_ID LOT_NUMBER QUANTITY1 1 ABC ABC1 101 2 ABC ABC2 151 3 ABC ABC3 201 4 ABD ABD1 51 5 ABD ABD2 31 6 ABE ABE1 2 2 1 ABC ABC1 102 2 ABC ABC2 152 3 ABC ABC3 202 4 ABD ABD1 53 1 ABC ABC1 103 2 ABC ABC2 153 3 ABC ABC3 203 4 ABD ABD2 3Am I asking too much? Certainly I am for my capabilities! |
 |
|
eb1962
Starting Member
6 Posts |
Posted - 2006-10-09 : 11:43:21
|
I HAVE MADE IT!!!Thanks to Peter and Harsh's suggestions!!!Here it is!!UPDATE bSET b.ORDER_LINE_ID = (SELECT COUNT(*) FROM SUMMARIZED_ORDER_TAB a WHERE b.ORDER_ID = a.ORDER_ID AND ((a.PRODUCT_ID < b.PRODUCT_ID) OR ((b.PRODUCT_ID = a.PRODUCT_ID) AND b.LOT_NUMBER <= a.LOT_NUMBER)))FROM SUMMARIZED_ORDER_TAB bTHANKS FOR YOUR SPLENDID HELP! I would have never made it! |
 |
|
|
|
|
|
|