避免使用SQL中的空行,其中大小写何时和group by正在使用中

时间:2018-05-08 12:35:17

标签: sql db2

以下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或为空的所有行。

2 个答案:

答案 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