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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 cursor to set

Author  Topic 

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-08-10 : 21:58:35
[code]DECLARE STK_CURSOR1 CURSOR FAST_FORWARD FOR
SELECT t.trx_type, t.trnx_ref, COALESCE(t.quantity, 0), COALESCE(t.qtt_out, 0), COALESCE(t.total_price, 0),
CASE WHEN h.forex_rate IS NULL or h.forex_rate = 0 then 1 ELSE h.forex_rate END forex_rate,
CASE WHEN t.trx_type IN ('PDO','GRO') then name else cm_name END name, t.do_date
FROM (
SELECT CASE WHEN trx_type = 'ADJ' and quantity > 0 and (SELECT content a FROM sys_config WHERE header = 'ST_ADJASCOST')='Y' then 'PDO'
WHEN trx_type = 'WHX' and quantity > 0 and (SELECT content a FROM sys_config WHERE header = 'ST_WHXASCOST')='Y' then 'PDO' else trx_type END trx_type,
t.trnx_ref, quantity, qtt_out, total_price, name, cm_name, do_date
FROM st_trx t
left join customer c on t.company=c.cm_cust_no
left join supplier s on t.company=s.supp_no
WHERE (do_date <= '20091231') and
(st_code=@st_code)
)t left join st_head h on t.trx_type=h.trx_type and t.trnx_ref=h.do_no

OPEN STK_CURSOR1
FETCH NEXT FROM STK_CURSOR1
INTO @trx_type, @trnx_ref, @quantity, @qtt_out, @total_price, @forex_rate, @name

WHILE @@FETCH_STATUS = 0
BEGIN
SET @prev_balance = @balance
SET @balance = @balance + (CASE WHEN @do_date <= @enddate then @quantity-@qtt_out else 0 END)
SET @prev_wacost = CASE WHEN @do_date <= @startdate then @wacost END
if @trx_type = 'PDO'
BEGIN
SET @mrcost = ISNULL(NULLIF((@forex_rate * @totaL_price), 0) / NULLIF(@quantity, 0), 0)
if @mrcost <> 0 SET @last_PDO_cost = @mrcost

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
END

FETCH NEXT FROM STK_CURSOR1
INTO @trx_type, @trnx_ref, @quantity, @qtt_out, @total_price, @forex_rate, @name

END[/code]
how do you convert this cursor to a select statement?


Hope can help...but advise to wait pros with confirmation...

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-08-11 : 00:01:02
what are the output / purpose of this query ?

calculate @balance, @mrcost, @wacost ?



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-08-11 : 01:40:12
yea... @balance, @mrcost, @wacost
this i what i covnert from a looping report...but dunno how to convert to set based


Hope can help...but advise to wait pros with confirmation...
Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-08-11 : 01:52:02
well...@balance and @mrcost i settled, but i have no idea how to get @wacost(control by @prev_balance)
how do select query know @prev_balance is >= 0?


Hope can help...but advise to wait pros with confirmation...
Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-08-11 : 03:20:00
sifu...>"< stucked
quote:
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




Hope can help...but advise to wait pros with confirmation...
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-08-11 : 04:36:40
try this. Basically it is using CTE calculate the running balance and temp table to join back itself to get the prev balance for the wacost


;with cte (trx_type, trnx_ref, quantity, qtt_out, total_price, forex_rate, [name], do_date, mrcost, row_no)
as
(
SELECT t.trx_type,
t.trnx_ref,
quantity = COALESCE(t.quantity, 0),
qtt_out = COALESCE(t.qtt_out, 0),
total_price = COALESCE(t.total_price, 0),
forex_rate = CASE WHEN h.forex_rate IS NULL or h.forex_rate = 0 then 1 ELSE h.forex_rate END,
[name] = CASE WHEN t.trx_type IN ('PDO','GRO') then name else cm_name END,
t.do_date,
mrcost = ISNULL(NULLIF((forex_rate * totaL_price), 0) / NULLIF(quantity, 0), 0),
row_no = ROW_NUMBER() OVER (ORDER BY t.trx_type, t.trnx_ref, to_date)
FROM (
SELECT CASE WHEN trx_type = 'ADJ'
and quantity > 0
and (SELECT content a FROM sys_config WHERE header = 'ST_ADJASCOST') = 'Y'
then 'PDO'
WHEN trx_type = 'WHX'
and quantity > 0
and (SELECT content a FROM sys_config WHERE header = 'ST_WHXASCOST') = 'Y'
then 'PDO'
else trx_type
END trx_type,
t.trnx_ref,
quantity,
qtt_out,
total_price,
[name],
cm_name,
do_date
FROM st_trx t
left join customer c on t.company=c.cm_cust_no
left join supplier s on t.company=s.supp_no
WHERE do_date <= '20091231'
and st_code = @st_code
) t
left join st_head h on t.trx_type = h.trx_type
and t.trnx_ref = h.do_no
)
select *
into #temp
from cte c
cross apply
(
select balance = SUM(CASE WHEN do_date <= @enddate then quantity - qtt_out else 0 END),
from cte x
where x.row_no <= c.row_no
) d

select t1.*,
wacost = case when t2.balance >= 0 and t2.wacost = 0
then
t1.mrcost
else
ISNULL(NULLIF((t2.balance * t2.wacost + t1.quantity * t1.mrcost), 0)
/ NULLIF((t2.balance + t1.quantity), 0), 0)
end
from #temp t1
left join #temp t2 on t1.row_no = t2.row_no + 1



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-08-11 : 04:45:00
LOL, cursor faster then set...OMG


Hope can help...but advise to wait pros with confirmation...
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-08-11 : 05:06:52
yes. it is possible. Running Total are best to be done at the client or reports side where the data is presented.


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-08-11 : 05:44:38
u know i don have report side...this is my report side xD


Hope can help...but advise to wait pros with confirmation...
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-08-11 : 20:55:43
you can also try recursive CTE


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -