SAS SQL: How to do (group base on Case) without pulling data first? -
i need count data of particular massive table without pulling data. group data , perform count...
select a.type, a.color, count(a.claimnumber) count_of_claim mytable group a.type, a.color
but in instance, color determined case when.. else
statements. sort of this: (i made example, structure of production problem same).
select b.type, b.color, count(claimnumber) claimnumber_count ( select a.type, case when a.company = 'honda' , substr(a.modelnum, 1, 5) <> 'aaaaa' 'red' when substr(a.modelnum, 1, 6) = 'bbbbbb' 'blue' when substr(a.modelnum, 1, 5) = 'ccccc' 'white' else 'black' end color, substr(a.claimnumber, 1, 10) claimnumber mytable ) b group b.type, b.color
i pulling data down, determined color when/else case, , counting how many there using group. there way can increment counter, or (grouping) without having derived table?
i read lot of questions on stackoverflow, empathized do counting on server side, , implement query such. current solution slow. pulling 1 year worth of record , counting them (over 4-5 hours).
dummy output of claim count:
type | color | count ------------------- car red 16894 car blue 2316 car black 1560 car white 89143 bus red 165 bus blue 9611230 bus black 25284 bus white 16929971
it not hard group derived column. can use column numbers variable list in group clause
select a.type, , a.color , count(a.claimnumber) count_of_claim mytable group 1,2
if getting performance issues when replace a.color case statement perhaps database not optimized summarize way? case seems using couple of variables make new color column perhaps can first summarize variables , run faster? if nothing else should need execute fewer substr() function calls.
select a.type , case when a.company = 'honda' , not (substr(a.modelnum, 1, 5) = 'aaaaa') 'red' when substr(a.modelnum, 1, 6) = 'bbbbbb' 'blue' when substr(a.modelnum, 1, 5) = 'ccccc' 'white' else 'black' end color , sum(nobs) claimnumber_count (select type,company,modelnum , count(claimnumber) nobs mytable group 1,2,3) group 1,2
Comments
Post a Comment