Search code examples
sqlpivotamazon-redshiftaggregate-functions

find the max count in a subgroup


I have a table (simplified)

enter image description here

I need to produce a report like this:

enter image description here

I need to find per user: total servers, total events, total succeeded and failed events. All this per user so I will be grouping by user.

But I'm also asked to compute the max number of events per single server per customer, i.e. subgrouping by server within each user. How do I do that?


Solution

  • You can do this with two levels of aggregation: first by user and server, then by user only.

    select user_id, 
        count(*)          total_servers,
        sum(total_events) total_events,
        sum(succeeded)    succeeded,
        sum(failed)       failed,        
        max(total_events) max_per_server
    from (
        select userid, server, 
            count(*) total_events,
            sum(case when event_status = 'success' then 1 else 0 end) succeeded,
            sum(case when event_status = 'success' then 1 else 0 end) failed
        from mytable t
        group by userid, server
    ) t
    group by user_id
    

    This should scan the table only once, and hence is likely to perform more efficiently than solutions involving joining CTEs.