Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SQL Group by Count String occurance

I am wanting to count the occurrences of a specific string in SQL.
I have UserID’s which are unique and each user can carry out an "action". I’ve tried a few things but still cannot get it to work.

So these actions can be "throw" "pickup" "craft"

SELECT userid, COUNT(action)
FROM `playeractions`
GROUP BY action;
userid COUNT(action)
7656119 129
76561194 4

Expected results required

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

userid throw pickup craft
7656119 29 100 0
76561194 2 2 0

Existing data of the table

userid action
7656119 throw
76561194 pickup
76561194 pickup
76561194 throw

>Solution :

you can simple privot the data

SELECT
    userid,
    SUM(action = 'throw') as 'throw',
    SUM(action = 'pickup') as 'pickup',
    SUM(action = 'craft') as 'craft'
FROM table1
GROUP BY userid
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading