进一步筛选SQL结果

进一步筛选SQL结果,sql,sql-server,filter,Sql,Sql Server,Filter,我得到了一个使用SQL2005返回正确结果集的查询。详情如下: select case when convert(varchar(4),datepart(yyyy,bug.datecreated),101)+ ' Q' +convert(varchar(2),datepart(qq,bug.datecreated),101) = '1969 Q4' then '2009 Q2' else convert(varchar(4),datepart(yyyy,bug.date

我得到了一个使用SQL2005返回正确结果集的查询。详情如下:

select case when convert(varchar(4),datepart(yyyy,bug.datecreated),101)+ ' Q' +convert(varchar(2),datepart(qq,bug.datecreated),101) = '1969 Q4' then '2009 Q2' else convert(varchar(4),datepart(yyyy,bug.datecreated),101)+ ' Q' +convert(varchar(2),datepart(qq,bug.datecreated),101) end as [Quarter], bugtypes.bugtypename, count(bug.bugid) as [Total] from bug left outer join bugtypes on bug.crntbugtypeid = bugtypes.bugtypeid and bug.projectid = bugtypes.projectid where (bug.projectid = 44 and bug.currentowner in (-1000000031,-1000000045) and bug.crntplatformid in (42,37,25,14)) or (bug.projectid = 44 and bug.currentowner in (select memberid from groupmembers where projectid = 44 and groupid in (87,88)) and bug.crntplatformid in (42,37,25,14)) group by case when convert(varchar(4),datepart(yyyy,bug.datecreated),101)+ ' Q' +convert(varchar(2),datepart(qq,bug.datecreated),101) = '1969 Q4' then '2009 Q2' else convert(varchar(4),datepart(yyyy,bug.datecreated),101)+ ' Q' +convert(varchar(2),datepart(qq,bug.datecreated),101) end, bugtypes.bugtypename order by 1,3 desc 它生成了一个很好地分组的年份和季度列表,一个相关的描述符,以及一个按降序排列的事件计数。我想做的是进一步过滤,这样每个季度只显示10个提交最多的事件


我正在努力解决的是如何获得这个结果集并实现它。

您已经按季度和总额订购了。您是否尝试过使用:

SELECT TOP 10
。。。。。查询的其余部分

编辑:在阅读了你的评论后,我意识到你需要使用来实现这一点。您可以在CTE中包装,如下所示:

;WITH IncidentsTable AS
(
   ... Insert Your Query here ...
)
SELECT * FROM
(
    SELECT [Quarter],
       BugTypeName,
       Total,
       Rank() OVER (Partition BY [Quarter] order by Total DESC) AS Ranking
     FROM
    IncidentsTable
)
WHERE
    Ranking <= 10
ORDER BY
      Quarter, Total;

您已经按季度和总额订购了。您是否尝试过使用:

SELECT TOP 10
。。。。。查询的其余部分

编辑:在阅读了你的评论后,我意识到你需要使用来实现这一点。您可以在CTE中包装,如下所示:

;WITH IncidentsTable AS
(
   ... Insert Your Query here ...
)
SELECT * FROM
(
    SELECT [Quarter],
       BugTypeName,
       Total,
       Rank() OVER (Partition BY [Quarter] order by Total DESC) AS Ranking
     FROM
    IncidentsTable
)
WHERE
    Ranking <= 10
ORDER BY
      Quarter, Total;

这样的模式会对每个季度进行分区和编号忽略数字这样的模式会对每个季度进行分区和编号忽略数字您可以使用秩和分区

Select * From 
(
    Select *,   Rank() over (Partition BY qtr order by qtr, id ) as Rank
    From
    (
              Select 1 as id, 1 as qtr,'hello' as msg
    union all select 2, 1,'hello'
    union all select 3,1,'hello'
    union all select 4,1,'hello'
    union all select 5,1,'hello'
    union all select 6,2,'hello'
    union all select 7,2,'hello'
    union all select 8,2,'hello'
    union all select 9,2,'hello'
    union all select 10,2,'hello'
    ) BaseQuery 
)QryWithRank
where rank <= 2

您可以使用秩和分区

Select * From 
(
    Select *,   Rank() over (Partition BY qtr order by qtr, id ) as Rank
    From
    (
              Select 1 as id, 1 as qtr,'hello' as msg
    union all select 2, 1,'hello'
    union all select 3,1,'hello'
    union all select 4,1,'hello'
    union all select 5,1,'hello'
    union all select 6,2,'hello'
    union all select 7,2,'hello'
    union all select 8,2,'hello'
    union all select 9,2,'hello'
    union all select 10,2,'hello'
    ) BaseQuery 
)QryWithRank
where rank <= 2

也许按第3列Total desc排序,并添加一个类,其中ROWNUM WHERE和how,但我不希望只有10条记录。我希望每个季度有10条记录。也许按第3列Total desc排序,并添加一个类,其中ROWNUM WHERE和how,但我不希望只有10条记录。我希望每个季度有10条记录。这只会返回10条记录,我希望每个季度有前10条记录。因此,如果有15个季度,我希望返回150条记录,每个季度前10条记录。我喜欢这一条的结构,它比尝试将我的查询插入Alex K的要干净一些。这只会返回10条记录,我希望每个季度前10条记录。因此,如果有15个季度,我希望返回150条记录,每个季度前10条记录。我喜欢这一条的结构,它比尝试将我的查询插入Alex K的查询要干净一些。这很接近,我用Rank替换了Row_Number,它起了作用。这很接近,我用Rank替换了Row_Number,它起了作用。