如何在T-SQL中计算两个不同的列?

时间:2011-07-30 13:08:48

标签: sql tsql stackexchange dataexplorer

我正在玩StackOverflow数据转储。现在我有一个T-SQL问题:

我可以选择一个包含每月和每年问题数量的列表:

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'行添加WHERE,那么我会得到所有“scala-questions”的数量。有什么方法可以在同一个结果集中显示问题总数和包含特定标记的问题数(在不同的列中)。

因为当我添加and tags.tagname = 'scala'时,我无法再看到每月的问题总数。

关于如何将这些结果集合并为一个的任何想法?

2 个答案:

答案 0 :(得分:2)

您需要两个查询来执行此操作,因为您有两组数据(按月分类的问题和按月分类的scala问题)。一种可能的解决方案是使用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的结果。

答案 1 :(得分:2)

如果对left outer join使用posttagscount(posttags.tagid)将只计算非空值。由于左外连接仅包含scala标记,因此您可以跳过distinct中的count(distinct posts.id)

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/