2010-08-17 2 views
3

나는 아래와 같이 다양한 길이 (6, 8, 10, ...) 및 각각의 카운트의 필드에 결과 쿼리가 있습니다일부 필드 합계 등 혼자 SQL을 남기시겠습니까

 
"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; 
+0

+1 통합 된 repeatLength 열에 이름을 지정하려고 할 수 있습니다. –

+0

@Mark : 고마워요. – RedFilter

+0

와우, 대단하네요! – Rich

0

repeatlength < = 18 인 경우 0 인 다른 필드 highGroup을 소개하고, 그렇지 않은 경우 1을 외부 조회의 highGroupfield에 그룹화하십시오.

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'

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"를 사용하여 집계 행을 나타냅니다.