0

I have a table (T)

User | Language | Value
-----------------------
1    |1         | string
1    |2         | otherString

and want to merge/join them to get this

User | Language_1 | Language_2
--------------------------------
1    |string      | otherString

I tried something like this

SELECT       USER,
             (CASE WHEN Language = 1 THEN Value END) AS language_1,
             (CASE WHEN Language = 2 THEN Value END) AS language_2
FROM         T

But I get something like this as result (I should have expected this)

User | Language_1 | Language_2
--------------------------------
1    |string      | NULL
1    |NULL        | otherString

Whats the right way to do it?

Tab Alleman
  • 31,483
  • 7
  • 36
  • 52
SCHTAILian
  • 428
  • 4
  • 16
  • Which RDBMS is this for? Please add a tag to specify whether you're using `mysql`, `postgresql`, `sql-server`, `oracle` or `db2` - or something else entirely. – marc_s Oct 22 '15 at 13:37
  • You really should avoid using reserved words as object or column names. USER and Language are both reserved words. – Sean Lange Oct 22 '15 at 13:37
  • @SeanLange indeed weren't thinking about it, gonna give it an edit when I am home from work – SCHTAILian Oct 22 '15 at 13:44

1 Answers1

2

You just need an aggregation

SELECT USER,
         MAX(CASE WHEN Language = 1 THEN Value END) AS language_1,
         MAX(CASE WHEN Language = 2 THEN Value END) AS language_2
FROM  T
GROUP BY USER
Vamsi Prabhala
  • 48,685
  • 4
  • 36
  • 58