Aggregate coffee table without using two samples

Is there a way to form a request that will accept this:

Id | action | object 
-------------
1 | view | article1 
2 | view | article2 
3 | view | article1 
4 | order | article1 
5 | order | article1 
6 | order | article3 

and do this:

name     | views | orders
----------------------
article1 |   2   |   2
article2 |   1   |   0
article3 |   0   |   1

One solution would be to use two choices and append them later in code. Is there a better one?

refers to && & TIA

noircc

+3
source share
1 answer

Maybe something like this:

SELECT
    SUM(CASE WHEN Table1.action='view' THEN 1 ELSE 0 END) AS views,
    SUM(CASE WHEN Table1.action='order' THEN 1 ELSE 0 END) AS orders,
    Table1.object 
FROM
    Table1
GROUP BY
    Table1.object 
+3
source

All Articles