从选择中排除空列

时间:2019-10-31 12:03:29

标签: sql postgresql select

我有一个选择的命令:

$.ajax()

结果是:

select 
sum(case when co_dim_tempo = 20190901 then 1 else 0 end) "1" , 
sum(case when co_dim_tempo = 20190902 then 1 else 0 end) "2" , 
sum(case when co_dim_tempo = 20190903 then 1 else 0 end) "3" , 
sum(case when co_dim_tempo = 20190904 then 1 else 0 end) "4" , 
sum(case when co_dim_tempo = 20190905 then 1 else 0 end) "5" , 
sum(case when co_dim_tempo = 20190906 then 1 else 0 end) "6"
from table

我喜欢排除列 1 | 2 | 3 | 4 | 5 | 6 5 | 0 | 8 | 7 | 0 | 3 ,因为结果为零,如下所示:

2 and 5

2 个答案:

答案 0 :(得分:1)

您可以从下面的查询中获得COUNT

HAVING类从结果中删除 0 值。

如果我们要使用 RIGHT 之类的String函数,则必须将CAST的数据类型从co_dim_tempoVARCHAR {1}}。

BIGINT

除此之外,SELECT RIGHT(CAST(co_dim_tempo AS VARCHAR),1) co_dim_tempo, SUM(*) NoOfOccurences FROM YourTable GROUP BY RIGHT(CAST(co_dim_tempo AS VARCHAR),1) HAVING SUM(1) > 0 PIVOT可能会达到您期望的结果。

答案 1 :(得分:0)

选择权(CAST(co_dim_tempo AS VARCHAR),1)co_dim_tempo SUM(*)非官方货币 GROUP BY RIGHT(CAST(co_dim_tempo AS VARCHAR),1) 总和(1)> 0;

相关问题