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 |
|
rajdaksha
Aged Yak Warrior
595 Posts |
Posted - 2009-07-24 : 07:57:25
|
Hi GuysAm in little stuck..I want to calculate on Carrying_Cost column value by each row. I have table data like thisSl.No Demand Period SKU Carrying_Cost1 10 10 A 02 20 20 B 0 ---3 30 30 C 04 40 40 D 0 Calculation isSecond Row Calculation isCarrying_Cost = Carrying_cost of first row value*0.5*100(default value)+Demand(current Row)*0.5*100(Default Value0+Demand(current Row)*100*Sl.No(first row Value which means previous row value)First Row Carrying_Cost Values is First row Demand Value please help on this.....thanks..-------------------------R.. |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2009-07-24 : 08:14:57
|
| Could you plug some numbers in to your algorithm to make it clearer what you want?Jim |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-07-24 : 08:16:27
|
1. can you show us what is the expected carrying cost value for all the rows ?2. quote: Second Row Calculation isCarrying_Cost = Carrying_cost of first row value*0.5*100(default value) is this always first row value or prev row value+ Demand(current Row)*0.5*100(Default Value0+ Demand(current Row)*100*Sl.No(first row Value which means previous row value)First Row Carrying_Cost Values is First row Demand Value
KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
rajdaksha
Aged Yak Warrior
595 Posts |
Posted - 2009-07-24 : 08:29:40
|
| [code] First Row Carrying_Cost is Frist row Demand Value so carrying_cost = 10Now the table is Sl.No Demand Period SKU Carrying_Cost1 10 10 A 102 20 20 B 0 3 30 30 C 04 40 40 D 0Now Rest of the row calci isCarrying_Cost = 10*0.5*100 + 20(current row demand)*0.5*100 + 20*100*1(previous row sl.No) [/code]-------------------------R.. |
 |
|
|
rajdaksha
Aged Yak Warrior
595 Posts |
Posted - 2009-07-24 : 09:12:17
|
| Hi khtan & jimf is there........-------------------------R.. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-07-24 : 09:20:31
|
[code]DECLARE @sample TABLE( S1No int, Demand int, Period int, SKU CHAR)INSERT INTO @sampleSELECT 1, 10, 10, 'A' UNION ALLSELECT 2, 20, 20, 'B' UNION ALLSELECT 3, 30, 30, 'C' UNION ALLSELECT 4, 40, 40, 'D';WITH data(S1No, Demand, Period, SKU, row_no)AS( SELECT S1No, Demand, Period, SKU, row_no = row_number() OVER (ORDER BY S1No) FROM @sample)SELECT d.S1No, d.Demand, d.Period, d.SKU, Carrying_Cost = SUM(CASE WHEN d.row_no = 1 THEN c.Demand WHEN c.row_no = 1 THEN c.Demand * 0.5 * 100 WHEN c.row_no = d.row_no THEN c.Demand * 0.5 * 100 WHEN c.row_no = d.row_no - 1 THEN d.Demand * 100 * c.S1No END)FROM data d INNER JOIN data c ON c.row_no = 1 OR (c.row_no >= d.row_no - 1 AND c.row_no <= d.row_no)GROUP BY d.S1No, d.Demand, d.Period, d.SKU/*S1No Demand Period SKU Carrying_Cost ----------- ----------- ----------- ---- ---------------------------------------- 1 10 10 A 10.02 20 20 B 1500.03 30 30 C 8000.04 40 40 D 14500.0(4 row(s) affected)*/[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
rajdaksha
Aged Yak Warrior
595 Posts |
Posted - 2009-07-24 : 09:23:58
|
| Hi KHWhat is thatFROM ledgertable INNER JOIN ledgertrans ON ledgertable.accountnum = ledgertrans.accountnum INNER JOIN vendtrans ON ledgertrans.voucher = vendtrans.voucher INNER JOIN bankchequetable c ON vendtrans.paymreference = bankchequetable.chequenum-------------------------R.. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-07-24 : 09:37:01
|
C&P error. Edited the post. KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
rajdaksha
Aged Yak Warrior
595 Posts |
Posted - 2009-07-24 : 09:46:59
|
| hi KHI need one more help. what i trying to say isFirst time only we can take the Demand data...which i have mentioned in red color value 10second time onwarda we can take Carrying_Cost instead of demand data which you have find in first..second time onwards Carrying_Cost(find value in frist row)*0.5*100 third time onwards Carrying_Cost(find value in second row)*0.5*100fourth time onwards Carrying_Cost(find value in third row)*0.5*100like that...-------------------------R.. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-07-24 : 09:49:48
|
can you confirm this is correct ? if not can you show how it should be calculated ?S1No Demand Period SKU Carrying_Cost----------- ----------- ----------- ---- -------------1 10 10 A 10.02 20 20 B 1500.03 30 30 C 8000.04 40 40 D 14500.0 KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
rajdaksha
Aged Yak Warrior
595 Posts |
Posted - 2009-07-27 : 01:31:22
|
Hi KhtanSorry my network was down due to maintenance..I want like this …pls help on this.. S1No Demand Period SKU Carrying_Cost----------- ----------- ----------- ---- -------------1 10 10 A 10.02 20 20 B 3500.03 30 30 C 182500.04 40 40 D 9139000.00First Time calc Carrying_Cost = First time only we have to take the value of Carrying_Cost is Demand value = 10RestCarrying_Cost = (Previous Row Carrying_Cost)*0.5*100 + 20(current row demand)*0.5*100 + 20(current row demand)*100*1(previous row sl.No)ExCarrying_Cost = SELECT 10*0.5*100 + 20*0.5*100 + 20*100*1SELECT 3500*0.5*100 + 30*0.5*100 + 30*100*2SELECT 182500.0*0.5*100 + 40*0.5*100 + 40*100*3 -------------------------R.. |
 |
|
|
rajdaksha
Aged Yak Warrior
595 Posts |
Posted - 2009-07-27 : 03:27:00
|
| Hi khtanAre you there we can discuss now....-------------------------R.. |
 |
|
|
rajdaksha
Aged Yak Warrior
595 Posts |
Posted - 2009-07-27 : 05:27:08
|
| hi Any one help on this .....-------------------------R.. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-07-27 : 06:12:25
|
[code]DECLARE @sample TABLE( S1No int, Demand int, Period int, SKU CHAR, Carrying_Cost decimal(10,2))INSERT INTO @sample (S1No, Demand, Period, SKU)SELECT 1, 10, 10, 'A' UNION ALLSELECT 2, 20, 20, 'B' UNION ALLSELECT 3, 30, 30, 'C' UNION ALLSELECT 4, 40, 40, 'D'DECLARE @S1No int, @First_S1No intSELECT @S1No = MIN(S1No)FROM @sampleSELECT @First_S1No = @S1NoWHILE @S1No IS NOT NULLBEGIN ;WITH data(S1No, Demand, Period, SKU, Carrying_Cost, row_no) AS ( SELECT S1No, Demand, Period, SKU, Carrying_Cost, row_no = row_number() OVER (ORDER BY S1No) FROM @sample ) UPDATE c SET Carrying_Cost = CASE WHEN @S1No = @First_S1No THEN c.Demand ELSE (p.Carrying_Cost * 0.5 * 100) + (c.Demand * 0.5 * 100) + (c.Demand * 100 * p.S1No) END FROM data c LEFT JOIN data p ON c.row_no = p.row_no + 1 WHERE c.S1No = @S1No SELECT @S1No = MIN(S1No) FROM @sample WHERE S1No > @S1NoENDSELECT *FROM @sample/*S1No Demand Period SKU Carrying_Cost ----------- ----------- ----------- ---- ------------- 1 10 10 A 10.002 20 20 B 3500.003 30 30 C 182500.004 40 40 D 9139000.00(4 row(s) affected)*/[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-07-27 : 06:21:37
|
the recursive CTE wayDECLARE @sample TABLE( S1No int, Demand int, Period int, SKU CHAR, Carrying_Cost decimal(10,2))INSERT INTO @sample (S1No, Demand, Period, SKU)SELECT 1, 10, 10, 'A' UNION ALLSELECT 2, 20, 20, 'B' UNION ALLSELECT 3, 30, 30, 'C' UNION ALLSELECT 4, 40, 40, 'D'DECLARE @First_S1No intSELECT @First_S1No = MIN(S1No)FROM @sample;WITH data (S1No, Demand, Period, SKU, Carrying_Cost, row_no)AS( -- Anchor Member SELECT S1No, Demand, Period, SKU, Carrying_Cost = CONVERT(decimal(10,2), Demand), row_no = CONVERT(int, 1) FROM @sample WHERE S1No = @First_S1No UNION ALL -- Recursive Member SELECT c.S1No, c.Demand, c.Period, c.SKU, Carrying_Cost = CONVERT(decimal(10,2), (p.Carrying_Cost * 0.5 * 100) + (c.Demand * 0.5 * 100) + (c.Demand * 100 * p.S1No)), row_no = c.row_no FROM data p INNER JOIN ( SELECT S1No, Demand, Period, SKU, row_no = CONVERT(int, row_number() OVER (ORDER BY S1No)) FROM @sample ) c ON c.row_no = p.row_no + 1)UPDATE sSET Carrying_Cost = d.Carrying_CostFROM @sample s INNER JOIN data d ON s.S1No = d.S1NoSELECT *FROM @sample/*S1No Demand Period SKU Carrying_Cost ----------- ----------- ----------- ---- ------------- 1 10 10 A 10.002 20 20 B 3500.003 30 30 C 182500.004 40 40 D 9139000.00(4 row(s) affected)*/ KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
rajdaksha
Aged Yak Warrior
595 Posts |
Posted - 2009-07-27 : 06:37:12
|
| Hi khtanThanks a lot friend....its working fine..But i can't able to understand why u used LEFT OUTER JOIN...can you explain pls what u done...bcoz its shows null value for first row..while track the code row by row.-------------------------R.. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-07-27 : 06:42:33
|
the LEFT JOIN is to handle the case of first row. For first row, there isn't any previous row for JOINing. KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
rajdaksha
Aged Yak Warrior
595 Posts |
Posted - 2009-07-27 : 06:51:40
|
| HiI got... thanks for your help..-------------------------R.. |
 |
|
|
rajdaksha
Aged Yak Warrior
595 Posts |
Posted - 2009-07-27 : 07:30:31
|
HiI have little alter your code for my handy...correct me if am in wrong... DECLARE @sample TABLE( row_no INT IDENTITY(1,1), S1No int, Demand int, Period int, SKU CHAR, Carrying_Cost decimal(16,2))INSERT INTO @sample (S1No, Demand, Period, SKU)SELECT 1, 10, 10, 'A' UNION ALLSELECT 2, 20, 20, 'B' UNION ALLSELECT 3, 30, 30, 'C' UNION ALLSELECT 4, 40, 40, 'D' UNION ALLSELECT 5, 50, 50, 'E'DECLARE @S1No int, @First_S1No intSELECT @S1No = MIN(S1No)FROM @sampleSELECT @First_S1No = @S1NoWHILE @S1No <= ISNULL((SELECT COUNT(row_no) FROM @sample),0)BEGIN UPDATE c SET Carrying_Cost = CASE WHEN @S1No = @First_S1No THEN c.Demand ELSE (p.Carrying_Cost * 0.5 * 100) + (c.Demand * 0.5 * 100) + (c.Demand * 100 * p.S1No) END FROM @sample c LEFT JOIN @sample p ON c.row_no = p.row_no + 1 WHERE c.S1No = @S1No SET @S1No = @S1No + 1ENDSELECT *FROM @sample -------------------------R.. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-07-27 : 07:33:21
|
If you are sure that S1No is continuous without gap. KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
rajdaksha
Aged Yak Warrior
595 Posts |
Posted - 2009-07-27 : 07:36:11
|
| Yes S1No is continuous............-------------------------R.. |
 |
|
|
Next Page
|
|
|
|
|