MySQL Forums
Forum List  »  Newbie

Re: Difficult.. How do you make a query to create columns that sums up rows based on condition?
Posted by: John Smith
Date: February 21, 2018 04:34PM

I didnt know what to look for, thank you for mentioning pivot table and found a solution that works here:

https://stackoverflow.com/questions/7674786/mysql-pivot-table

SELECT P.`company_name`,
COUNT(
CASE
WHEN P.`action`='EMAIL'
THEN 1
ELSE NULL
END
) AS 'EMAIL',
COUNT(
CASE
WHEN P.`action`='PRINT' AND P.`pagecount` = '1'
THEN P.`pagecount`
ELSE NULL
END
) AS 'PRINT 1 pages',
COUNT(
CASE
WHEN P.`action`='PRINT' AND P.`pagecount` = '2'
THEN P.`pagecount`
ELSE NULL
END
) AS 'PRINT 2 pages',
COUNT(
CASE
WHEN P.`action`='PRINT' AND P.`pagecount` = '3'
THEN P.`pagecount`
ELSE NULL
END
) AS 'PRINT 3 pages'
FROM test_pivot P
GROUP BY P.`company_name`;

Options: ReplyQuote


Subject
Written By
Posted
Re: Difficult.. How do you make a query to create columns that sums up rows based on condition?
February 21, 2018 04:34PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.