|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2009-07-14 : 23:49:41
|
[code]SELECT t.st_code, m.st_desc, SUM(CASE WHEN t.trx_type IN ('CN', 'CNC') then 0 else t.total_price*h.forex_rate END) as SALES, SUM(CASE WHEN t.trx_type IN ('CN', 'CNC') then 0 else t.qtt_out-t.quantity END) as QUANTITY, SUM(t.cost_mr) as COST, SUM(ISNULL((NULLIF(CASE WHEN t.trx_type IN ('CN', 'CNC') then 0 else t.total_price*h.forex_rate END-t.cost_mr, 0)/NULLIF(CASE WHEN t.trx_type IN ('CN', 'CNC') then 0 else t.total_price*h.forex_rate END, 0)), 0)*100) as SALES, SUM(ISNULL((NULLIF(CASE WHEN t.trx_type IN ('CN', 'CNC') then 0 else t.total_price*h.forex_rate END-t.cost_mr, 0)/NULLIF(t.cost_mr, 0)), 0)*100) as COSTFROM st_trx t join ( SELECT trx_type, forex_code, CASE WHEN trx_type IN ('DO', 'CDO','DOL') then do_no else in_no END as ref_no, CASE WHEN forex_rate IS NULL or forex_rate = 0 then 1 else forex_rate end as forex_rate, CASE WHEN trx_type IN ('DO', 'CDO','DOL') then CASE WHEN in_date IS NULL then do_date else in_date END else in_date END as date FROM st_head WHERE trx_type IN('DO','CDO','DOL','INV','CS','CN','DN','POS','INC','CNC') )h on t.trnx_ref=h.ref_no and t.trx_type=h.trx_type join st_mast m on t.st_code = m.st_codeWHERE (h.date >='20090101' and h.date <'20100101') GROUP BY t.st_code, m.st_descORDER BY t.st_code[/code]Is this query still able to minimize its speed?sorry i still learning how to minimize everything...[code]cached plan size 140Bcomplie cpu 213complie memory 2448complie time 213estimated subtree cost 531.334[/code] Hope can help...but advise to wait pros with confirmation... |
|