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 |
David_G
Starting Member
10 Posts |
Posted - 2012-07-30 : 02:56:04
|
If I had a table like below how would I loop through and update the Qty column based on the ratio of the previous column with the same SKUCreate table #lines(SKU varchar(50),UOM varchar(35),Qty int)Insert into #lines(SKU,UOM,Qty) values('ABC','EACH','1')Insert into #lines(SKU,UOM,Qty) values('ABC','INNER','12')Insert into #lines(SKU,UOM,Qty) values('ABC','OUTER','144')Insert into #lines(SKU,UOM,Qty) values('ABC','CTN','1728')Insert into #lines(SKU,UOM,Qty) values('DEF','EACH','1')Insert into #lines(SKU,UOM,Qty) values('DEF','INNER','5')Insert into #lines(SKU,UOM,Qty) values('DEF','OUTER','25')Insert into #lines(SKU,UOM,Qty) values('DEF','CTN','125')select * from #linesABC EACH 1ABC INNER 12ABC OUTER 144ABC CTN 1728DEF EACH 1DEF INNER 5DEF OUTER 25DEF CTN 125-- What I need to do is replace the Qty column value with the ratio of the following ...-- CTN/OUTER into Qty column for SKU CARTON-- OUTER/INNER into Qty Column for SKU OUTER-- INNER/EACH into Qty Column for SKU INNER-- After loooping or update the table would look like thisABC EACH 1ABC INNER 12ABC OUTER 12ABC CTN 12DEF EACH 1DEF INNER 5DEF OUTER 5DEF CTN 5 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-07-30 : 03:09:58
|
[code];With SKU_OrderAS(SELECT t.*,t1.ValFROM table tINNER JOIN (SELECT 'EACH' AS Cat,1 AS Val UNION ALL SELECT 'INNER',2 UNION ALL SELECT 'OUTER',3 UNION ALL SELECT 'CTN',4 )t1ON t1.Cat = t.UOM)SELECT s1.SKU,s1.UOM,s1.Qty/ISNULL(s2.Qty,1) AS QtyFROM Sku_Order s1LEFT JOIN Sku_Order s2ON s2.SKU = s1.SKUAND s2.Val = s1.Val-1if you need to update main table replace above select with updateUPDATE s1SET s1.Qty = s1.Qty/s2.QtyFROM Sku_Order s1INNER JOIN Sku_Order s2ON s2.SKU = s1.SKUAND s2.Val = s1.Val-1[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
David_G
Starting Member
10 Posts |
Posted - 2012-07-30 : 22:32:03
|
Thank you very much.This worked well.Regards,David |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-07-30 : 23:49:39
|
welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
David_G
Starting Member
10 Posts |
Posted - 2012-09-08 : 21:10:11
|
Hello, Thanks very much for your assistance. Everthing worked well but I now need to adjust this code to handle the fact that we may not always have one of the values for the division.I have the select part working for testing but cant work out how to translate this into an update statement which is what I require.Any assistance will be greatly appreciated.SELECT t.*,t1.Val, 0 as backinto ##wFROM #lines tINNER JOIN (SELECT 'EACH' AS Cat,1 AS Val UNION ALL SELECT 'INNER',2 UNION ALL SELECT 'OUTER',3 UNION ALL SELECT 'CTN',4 )t1ON t1.Cat = t. UOMselect * from ##wupdate ##w set back = isnull((select max(w1.Val) from ##w w1 inner join ##w w2 on (w1.SKU = w2.SKU) where w1.Val < ##w.Val and ##w.SKU =w2.SKU),0)-- I need to code below to update the QTY columnSELECT w1.SKU, w1.UOM, w1.QTY, cast(w1.QTY as decimal)/ISNULL(cast(w2.QTY asdecimal),1) AS RATIOFROM ##w w1 LEFT JOIN ##w w2 ON (w2.SKU = w1.SKU and w1.back = w2.Val)drop table ##w |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-09 : 11:56:06
|
in cases where one of quantity does not exist what should be the division result?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|