以下SQL提供了我想要的正确结果,但数据输出也包含空行,这是我不想要的。
Select
v.ID
,max(case when v.param = 'param_1' then v.pValue end) as param1
,max(case when v.param = 'param_2' then v.pValue end) as param2
,max(case when v.param = 'param_3' then v.pValue end) as param3
from
datasource v
Group by v.ID
例如,为了解决这个问题,可以使用第二个SQL来处理上面的SQL,并设置像“parama1不为空”的过滤器。但是我想在同一个Statement中执行它,避免(不包括)所有树param列为null或为空的所有行。
答案 0 :(得分:1)
您可以使用having
声明:
select v.ID
max(case when v.param = 'param_1' then v.pValue end) as param1,
max(case when v.param = 'param_2' then v.pValue end) as param2,
max(case when v.param = 'param_3' then v.pValue end) as param3
from datasource v
group by v.ID
having max(case when v.param = 'param_1' then v.pValue end) is not null;
答案 1 :(得分:1)
只需添加一个WHERE
子句,只包含至少包含param值的行:
Select
v.ID
,max(case when v.param = 'param_1' then v.pValue end) as param1
,max(case when v.param = 'param_2' then v.pValue end) as param2
,max(case when v.param = 'param_3' then v.pValue end) as param3
from
datasource v
where v.param in ('param_1', 'param_2', 'param_3')
Group by v.ID