2011-07-30 23 views
1

私はStackOverflowのデーターダンプで遊んでいます。今、私は、T-SQLの問題を抱えている:T-SQLで2つの異なる列を数えるには?

私は月と年ごとの質問の数とリストを選択することができます:私はWHERE行×上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'を追加すると、月に合計質問数が表示されなくなるため、

これらの結果セットを1つにまとめる方法についてのアイデアはありますか?

答えて

2

left outer joinposttagsに対して使用した場合、count(posttags.tagid)はNULL以外の値のみをカウントします。また、左外部結合には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。私はしませんでした。サブクエリは、1つの行/値のみを返します。 'scala'のIDは '3143 'なので、サブクエリに値を返すのではなく、' 3143'を使って結合を書くことができます。 http://data.stackexchange.com/stackoverflow/q/107949/ –

+1

もう一度素晴らしいミカエル。私は思った..決して+1を気にしない:) –

2

2つのデータセット(月ごとの質問と月ごとのスカラー質問)があるため、2つのクエリが必要です。考えられる解決策の1つは、common table expressionsを使用して、データの2つの「一時的なビュー」を作成することです。例として:

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です。

関連する問題