Lets say I have such a table:
id|time|operation
1 2 read
2 5 write
3 3 read
4 7 read
5 2 save
6 1 open
and now I would like to do two things:
- Divide all these records into two groups:
1) all rows where operation equals to “read”
2) all other rows. - Sum the time in each group.
So that my query would result only into two rows.
What I got so far is:
select
sum(time) as total_time,
operation
group by
operation
;
Although that gives me many groups, depending on the number of distinct operations.
How I could group them only into two categories?
Cheers!
group bycan take arbitrary clauses, sowill work. Essentially you’d be grouping on the boolean result of the comparison, not the value of the operation field, so any record which is “read” will be group
1, and any non-read will be group0.