我有一个查询导致一个不同长度的字段(6,8,10,...)和各自的计数如下所示:
"region","repeatLength","count" "promoter","6","272387" "promoter","8","86929" "promoter","10","28337" "promoter","12","8873" "promoter","14","3080" "promoter","16","1098" "promoter","18","475" "promoter","20","206" "promoter","22","133" "promoter","24","75" "promoter","26","42" "promoter","28","32" "promoter","30","16" "promoter","32","6" "promoter","34","9"
此表由此次调用生成:
select region, repeatLength, count(*) as count
from alignedRepeats
group by region, repeatLength;
我希望能够压缩这些结果,以便重复长度< 18是完整的,但是通过对计数字段求和,将重复长度> = 18聚合成一行。这可以在单个SQL查询中进行,而无需创建临时表并将它们联合起来吗?
很抱歉,如果这是一个简单的问题,我是一个SQL新手。
答案 0 :(得分:4)
select region,
case when repeatLength >= 18 then ">=18" else repeatLength end as repeatLength,
count(*) as count
from alignedRepeats
group by region,
case when repeatLength >= 18 then ">=18" else repeatLength end;
答案 1 :(得分:1)
您不必创建临时表。但是你可以使用联合:
select region, repeatLength, 1 as Colname
from alignedRepeats
where repeatLength < 18
union
select region, repeatLength, count(*)
from alignedRepeats
where repeatLength >= 18
group by region, repeatLength;
不确定您想要包含的最后一个字段,因此'1为Colname'
答案 2 :(得分:1)
正确答案(已测试)
select
region,
repeatLength,
count(*) as count
from
alignedRepeats
where
repeatLength < 18
group by
region,
repeatLength
union
select
region,
999,
count(*)
from
alignedRepeats
where
repeatLength >= 18
group by
region
注意:使用“999”表示聚合行。
答案 3 :(得分:0)
引入另一个字段highGroup,如果repeatlength&lt; = 18则为0,否则为1,然后在外部查询中的highGroupfield上进行分组。