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 |
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2009-07-27 : 21:21:18
|
[code]DECLARE STK_CURSOR2 CURSOR FAST_FORWARD FORSELECT COALESCE(t.total_price, 0), COALESCE(t.quantity, 0), COALESCE(t.qtt_out, 0), t.do_date, t.trx_type, CASE WHEN h.forex_rate IS NULL OR h.forex_rate = 0 then 1 ELSE h.forex_rate END forex_rateFROM ( SELECT CASE WHEN trx_type = 'ADJ' and quantity > 0 and @st_adjascost='Y' then 'PDO' WHEN trx_type = 'WHX' and quantity > 0 and @st_whxascost='Y' then 'PDO' else trx_type END trx_type, trnx_ref, quantity, total_price, qtt_out, do_date FROM st_trx t right join ( SELECT st_code FROM st_mast WHERE @a = 1 and (st_code=@st_code) UNION SELECT st_code FROM st_msuspend WHERE @b = 1 and (st_code=@st_code) )m on t.st_code=m.st_code WHERE do_date <= @enddate )t left join st_head h on t.trnx_ref=h.do_no and t.trx_type=h.trx_typeORDER BY t.do_dateOPEN STK_CURSOR2FETCH NEXT FROM STK_CURSOR2INTO @total_price, @quantity, @qtt_out, @do_date, @trx_type, @forex_rateWHILE @@FETCH_STATUS = 0BEGIN SET @bforward = @bforward + (CASE WHEN @do_date < @startdate then @quantity-@qtt_out else 0 END) SET @qty_in = @qty_in + (CASE WHEN @do_date <= @enddate and @do_date >= @startdate then @quantity else 0 END) SET @qty_out = @qty_out + (CASE WHEN @do_date < @enddate and @do_date >= @startdate then @qtt_out else 0 END) SET @prev_balance = @balance SET @balance = @balance + (CASE WHEN @do_date < @enddate then @quantity-@qtt_out else 0 END) if @trx_type = 'PDO' BEGIN if ISNULL(NULLIF((@forex_rate * @totaL_price),0) / NULLIF(@quantity,0),0) <> 0 BEGIN SET @mrcost = (@forex_rate * @totaL_price) / @quantity END if @prev_balance >= 0 BEGIN if @wacost = 0 BEGIN SET @wacost = @mrcost END else BEGIN SET @wacost = ISNULL(NULLIF((@prev_balance*@wacost+@quantity*@mrcost), 0)/NULLIF((@prev_balance+@quantity), 0), 0) END END else BEGIN SET @wacost = @mrcost END if @wacost = 0 BEGIN SET @wacost = @aver_cost END END FETCH NEXT FROM STK_CURSOR2 INTO @total_price, @quantity, @qtt_out, @do_date, @trx_type, @forex_rateENDCLOSE STK_CURSOR2DEALLOCATE STK_CURSOR2[/code]hi, guys...how to convert this to 1 select statement....i deadlock at the top mrcost cost <> 0 there....[edit]show where i need help... Hope can help...but advise to wait pros with confirmation... |
|
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2009-07-27 : 22:52:08
|
i think i stay back to cursor...seems to be too complicated.. Hope can help...but advise to wait pros with confirmation... |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-07-27 : 23:14:53
|
what is the cursor doing ? I don't see anything beside calculating some variables. Is this the complete query ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2009-07-27 : 23:42:14
|
ya...this cursor is to calculate data.i need to get@bforward = SUM(CASE WHEN do_date < @startdate then quantity-qtt_out else 0 END)@qty_in = SUM(CASE WHEN do_date <= @enddate and do_date >= @startdate then quantity else 0 END)@qty_out = SUM(CASE WHEN do_date < @enddate and do_date >= @startdate then qtt_out else 0 END)@balance = SUM(CASE WHEN do_date < @enddate then quantity-qtt_out else 0 END)@mrcost = ((@forex_rate * @totaL_price) / @quantity) with condition TOP 1 do_date, ((forex_rate * totaL_price) / quantity)<> 0, trx_type = 'PDO'@wacost = i also dunno how to explain....just try to match above formula...fromquote:
SELECT COALESCE(t.total_price, 0), COALESCE(t.quantity, 0), COALESCE(t.qtt_out, 0), t.do_date, t.trx_type, CASE WHEN h.forex_rate IS NULL OR h.forex_rate = 0 then 1 ELSE h.forex_rate END forex_rateFROM ( SELECT CASE WHEN trx_type = 'ADJ' and quantity > 0 and @st_adjascost='Y' then 'PDO' WHEN trx_type = 'WHX' and quantity > 0 and @st_whxascost='Y' then 'PDO' else trx_type END trx_type, trnx_ref, quantity, total_price, qtt_out, do_date FROM st_trx t right join ( SELECT st_code FROM st_mast WHERE @a = 1 and (st_code=@st_code) UNION SELECT st_code FROM st_msuspend WHERE @b = 1 and (st_code=@st_code) )m on t.st_code=m.st_code WHERE do_date <= @enddate )t left join st_head h on t.trnx_ref=h.do_no and t.trx_type=h.trx_typeORDER BY t.do_date
red = i solved.... Hope can help...but advise to wait pros with confirmation... |
 |
|
|
|
|
|
|
|