我有两个以ID绑定的表的数据。问题是我不想选择列名,而是选择里面的实际数据。所以我决定使用SELECT CASE语句,但它会生成一个包含许多空值的表。我如何才能选择非空数据?
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
输出
CAR BOAT
200 NULL
NULL 300
我怎么只选择无空值?
答案 0 :(得分:2)
GROUP BY
MAX
用于其他列:
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