i have 2 queries. 1 numerator , 1 denominator. how combine 2 queries result 1 table numerator, denominator, , grouping?

example of desired output:
numerator | denominator | grouping ----------|-------------|--------- 30 | 51 | 1111 172 | 216 | 2768
you have 2 different aggregates on same table. many reasons, performance being 1 of them, not want break query down 2 parts , re-join them together. can accomplish correct result using column-level filtering instead of clause filtering:
select [officerid] ,sum(case when [st3id] != '' 1 else 0 end) [numerator] ,count(*) [denomimator] [dbo].[cobanvideos] [starting] > '6/1/2015 0:00:00 am' , [starting] < '7/1/2015 0:00:00 am' group [officerid] by using case statement filter data @ column level, can retrieve both values @ same time. can calculate percentage value (numerator/denminator) adding following additional column:
select [officerid] ,sum(case when [st3id] != '' 1 else 0 end) [numerator] ,count(*) [denomimator] ,case when count(*) <> 0 sum(case when [st3id] != '' 1.0 else 0 end) / count(*) else 0 end [pct st3] [dbo].[cobanvideos] [starting] > '6/1/2015 0:00:00 am' , [starting] < '7/1/2015 0:00:00 am' group [officerid] sql window functions give whole other set of tools working aggregates @ different levels of aggregation, in 1 query. if interested, can follow example of how calculate ratio per officerid, officers, , determine contribution percent of each officer overall total, single select.
Comments
Post a Comment