0

I have data across two tables bound by an ID. The problem is I do not want to select the column names but the actual data inside. So I decided to use a SELECT CASE statement but it produces a table with many nulls. How would I just select the data that isn't null?

column value   column name
200              car
300              boat

SQL QUERY:

SELECT 
CASE WHEN  b.name = 'car' THEN a.value END as 'car',
CASE WHEN  b.boat= 'car' THEN a.value END as 'boat' 
WHERE a.id = b.id   

The output is

CAR  BOAT
200  NULL
NULL 300 

How would I only select none null values?

user999690
  • 257
  • 4
  • 10
  • 23

1 Answers1

2

GROUP BY with MAX for the other columns:

SELECT a.id,
       MAX(CASE WHEN  b.name = 'car' THEN a.value END) as 'car',
       MAX(CASE WHEN  b.boat= 'car' THEN a.value END) as 'boat' 
WHERE a.id = b.id   
GROUP BY a.id
Mahmoud Gamal
  • 78,257
  • 17
  • 139
  • 164