简体   繁体   中英

How to add a second dimension column to an aggregated metric

I have a SUM metric and a dimension I am grouping by. I want to display another dimension, and I know for a fact that the values of the second dimension are equal for all corresponding values in the first dimension. (meaning I would get the same results if I group by the first dimension and if I group by the second dimension). how can I display the second dimension as well?

select 
SUM(case
when (eventType like 'Add') then 1 else 0
end) as sum_uploads,
userId
from entry
group by userId
order by sum_uploads desc

the desired result would be if I could run something like that (with the addition of the third column):

select 
SUM(case
when (eventType like 'Add') then 1 else 0
end) as sum_uploads,
userId, userType
from entry
group by userId
order by sum_uploads desc

I don't know exactly what to try.

You typically GROUP BY the same columns as you SELECT , except those who are arguments to set functions. Ie add the column userType to the GROUP BY :

select 
    SUM(case when (eventType like 'Add') then 1 else 0 end) as sum_uploads,
    userId, userType
from entry
group by userId, userType
order by sum_uploads desc

BTW, instead of LIKE without using any wild-cards, I'd do eventType = 'Add' .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM