2011-07-30 2 views
1

나는 StackOverflow 데이터 덤프로 놀고있다. 내가 가진 달 연간 질문의 번호 목록을 선택할 수 있습니다T-SQL에서 두 개의 다른 열을 계산하는 방법은 무엇입니까?

: 지금은 T-SQL 문제가 내가 WHERE -row에 and tags.tagname = 'scala'를 추가하는 경우

select datepart(year, posts.creationdate) as year, 
datepart(month, posts.creationdate) as month, 
count(distinct posts.id) as questions 
from posts 
inner join posttags on posttags.postid = posts.id 
inner join tags on tags.id = posttags.tagid 
where posts.posttypeid = 1 
group by datepart(month, posts.creationdate), 
datepart(year, posts.creationdate) 
order by datepart(year, posts.creationdate), 
datepart(month, posts.creationdate) 

, 그때는 얻을 모든 "스칼라 질문"의 수. 질문의 총 개수와 동일한 결과 집합에 특정 태그가 포함 된 질문의 개수를 다른 열에 표시 할 수있는 방법이 있습니까?

and tags.tagname = 'scala'을 추가하면 더 이상 한 달에 총 질문 수가 표시되지 않습니다.

이러한 결과 집합을 하나의 단위로 묶을 수있는 방법에 대한 아이디어가 있습니까?

답변

2

left outer joinposttags에 대해 사용하는 경우 count(posttags.tagid)은 0이 아닌 값만 계산합니다. 왼쪽 외부 조인은 scala 태그 만 포함하므로 distinct은 건너 뜁니다().

select datepart(year, posts.creationdate) as year, 
     datepart(month, posts.creationdate) as month, 
     count(*) as questions, 
     count(posttags.tagid) as sc 
from posts 
    left outer join posttags 
    on posttags.postid = posts.id and 
     posttags.tagid = (select id 
         from tags 
         where tagname = 'scala') 
where posts.posttypeid = 1 
group by datepart(month, posts.creationdate), 
     datepart(year, posts.creationdate) 
order by datepart(year, posts.creationdate), 
     datepart(month, posts.creationdate) 

여기보십시오 : http://data.stackexchange.com/stackoverflow/q/107948/

+0

미카엘 - 나는 당신이 당신의 테이블을 조인하는 것을 잊지 어디에서 날 볼 것이라고 생각하지 않았다. posttags.tagid = (tagname = 'scala'및 posttags.tagid = id 인 태그에서 ID를 선택하십시오.) –

+0

@ t-clausen.dk. 나는하지 않았다. 하위 쿼리는 하나의 행/값만 반환합니다. 'scala'의 ID는 '3143'이므로 서브 쿼리를 사용하는 대신 '3143'을 사용하여 조인을 작성할 수 있습니다. http://data.stackexchange.com/stackoverflow/q/107949/ –

+1

좋은 한 번 다시 Mikael. 나는 생각했다. 결코 신경 쓰지 마라. +1 :) –

2

두 세트의 데이터 (월별 질문 및 월별 질문)가 있으므로 두 가지 쿼리가 필요합니다. 한 가지 가능한 솔루션은 common table expressions을 사용하여 데이터의 두 "임시보기"를 만드는 것입니다. 예를 들면 다음과 같습니다.

with total as (
    select datepart(year, posts.creationdate) as year, 
      datepart(month, posts.creationdate) as month, 
      count(distinct posts.id) as questions 
    from posts 
     inner join posttags on posttags.postid = posts.id 
     inner join tags on tags.id = posttags.tagid 
    where posts.posttypeid = 1 
    group by datepart(month, posts.creationdate), datepart(year, posts.creationdate) 
), scala as (
    select datepart(year, posts.creationdate) as year, 
      datepart(month, posts.creationdate) as month, 
      count(distinct posts.id) as questions 
    from posts 
     inner join posttags on posttags.postid = posts.id 
     inner join tags on tags.id = posttags.tagid 
    where posts.posttypeid = 1 and tags.tagname = 'scala' 
    group by datepart(month, posts.creationdate), datepart(year, posts.creationdate) 
) 
select total.year, total.month, total.questions as total_questions, scala.questions as scala_questions 
from total 
    join scala on total.year = scala.year and total.month = scala.month 
order by total.year, total.month​ 

결과는 here입니다.

관련 문제