0

I'm having the following data:

ID   user    value     lang
------------------------------
1    1       foo1      de
2    1       foo2      de
3    1       bar1      en
4    1       bar2      en
5    1       bar3      en
6    1       bar4      fr

desired output:

user   de    en    fr 
------------------------------
1      2     3     1

i want to get the total count of all languages from user 1.

I've tried with COUNT, SUM and HAVING .. but it didn't work.

1
  • Can you please add what you have tried? Did you group by? Commented Jan 30, 2020 at 13:43

1 Answer 1

2

You can use conditional aggregation

SELECT user,
       SUM(CASE WHEN lang='de' THEN 1 ELSE 0 END) as 'de',
       SUM(CASE WHEN lang='en' THEN 1 ELSE 0 END) as 'en',
       SUM(CASE WHEN lang='fr' THEN 1 ELSE 0 END) as 'fr'
  FROM t
 GROUP BY user

or directly without conditionals :

SELECT user,
       SUM(lang='de') as 'de',
       SUM(lang='en') as 'en',
       SUM(lang='fr') as 'fr'
  FROM t
 GROUP BY user

Demo

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.