There is smoke coming out of my ears.
In your example ‘you suck’ needs to be a field rather than a string. so "group by ‘WPS’ when the value is (WN PL SH etcc) or by pool_type
so I could end up with a table like this…
WPS (value)
EX (value)
QN (value)
TR (value)
instead of
WN (value)
PL (value)
SH (value)
WP (value)
PS (value)
WS (value)
WPS (value)
EX (value)
QN (value)
TR (value)
But it doesn’t work (I get the complaint - ‘pool type’ is invalid in the select list because it is not contained in an agregate function or group by clause (even though it is!))
so for now - I’ve solved it temporarily by wrapping the whole mega query inside another query with a simple group by clause (in other words crate a table that shows ‘WPS’ for all the variants and put it in a new field) then run a query on that grouping by the new field…
select site,program_name,pool_type,sum(handle) as handle,sum(commission) as commission from
(select (case trackingid when 2 then ‘site1’ else ‘site2’ end) as site,
program_name,
(case when pool_type in (‘WN’,‘PL’,‘SH’,‘WP’,‘PS’,‘WS’,‘WPS’) then ‘WPS’
else pool_type end) pool_type,
sum(cast(total_bet_amount as real)-cast(refund_amt as real)) as handle,
((case when pool_type in (‘WN’,‘PL’,‘SH’,‘WP’,‘PS’,‘WS’,‘WPS’) then (select commission from amtotecommissions where program_name=a.program_name and pool_type=‘WN’ and _date=a._date)
else
(select commission from amtotecommissions where program_name=a.program_name and pool_type=a.pool_type and _date=a._date)
end
)/100)* sum(cast(total_bet_amount as real)-cast(refund_amt as real)) as commission
from amtoteaccountactivity a inner join clients on a.accountnumber = clients.accountnumber where _date=‘08/06/11’ and transaction_type = ‘Bet’
group by _date,program_name,trackingid,pool_type) q1
group by program_name,site,pool_type
order by program_name,site,pool_type