SQL组按一个字段和按另一个字段排序

时间:2009-10-29 15:58:42

标签: sql-server sql-server-2005 tsql

表:

declare @Table table (    
 id int,    
 ticketid int,    
 sponsor int,    
 dev int,
 qa int,
 savedate datetime
)

insert into @Table values (1,100,22,0, 0, '2008-10-29 11:17:59.527')
insert into @Table values (2,100,5,0, 0, '2008-10-29 11:00:37.030')
insert into @Table values (3,101,22,0, 0, '2009-10-29 11:10:27.687')
insert into @Table values (5,101,44,0, 0, '2008-10-31 12:07:52.917')
insert into @Table values (6,101,32,0, 0, '2009-06-30 08:16:12.343')
insert into @Table values (7,101,44,0, 0, '2009-10-31 10:12:11.369')

我正在尝试选择最高1的最大值,其中recordid是某个记录,由赞助商分组。

我的进步:

select max(savedate)
from @Table
where ticketid = 101
group by sponsor

返回

2009-10-29 11:10:27.687
2009-06-30 08:16:12.343
2009-10-31 10:12:11.370

关闭,我分组正确,但我想要前1个最近的日期。所以我这样做:

select top 1 max(savedate)
from @Table
where ticketid = 101
group by sponsor

返回

2009-10-29 11:10:27.687

哇哇,得到它,休息的时间......等等......这不是最近的约会!让我们尝试通过已保存的

来订购这些
select top 1 max(savedate)
from @Table
where ticketid = 101
group by sponsor
order by savedate desc

哦不!可怕的:

  

“Column”@ Table.savedate“无效   在ORDER BY子句中,因为它是   不包含在聚合中   函数或GROUP BY子句。“

但是在选择列表中汇总了已保存的IS!我该怎么做我想做的事?

3 个答案:

答案 0 :(得分:2)

在输入这个东西并确保我不会遗漏细节之后,我在最后得到了答案。我想我还是会添加它,所以我或其他任何人都可以在以后找到它,如果它们像我在这种情况下一样短视。

select top 1 max(savedate) as date
from @Table
where ticketid = 101
group by sponsor
order by date desc

max(savedate)与savedate不同!别名然后引用聚合工作完美:

2009-10-31 10:12:11.370

希望这有助于某人。

答案 1 :(得分:0)

select top 1 max(savedate)
from @Table
where ticketid = 101
group by sponsor
order by max(savedate) desc

答案 2 :(得分:0)

您可能希望将赞助商与MAX(savedate)一起退回。

如果你没有,那么请注意这个查询:

SELECT  MAX(savedate)
FROM    @Table
WHERE   ticketid = 101

与您的解决方案完全相同

select top 1 max(savedate) as date
from @Table
where ticketid = 101
group by sponsor
order by date desc

,虽然效率更高。

或者,您可以执行以下操作:

SELECT  TOP 1 sponsor, savedate
FROM    @Table
WHERE   ticketid = 101
ORDER BY
        savedate DESC

<强>更新

此查询将返回上次将任务分配给当前发起人的日期:

declare @Table table (    
 id int PRIMARY KEY,    
 ticketid int,    
 sponsor int,    
 dev int,
 qa int,
 savedate datetime
)

insert into @Table values (1,100,22,0, 0, '2008-10-29 11:17:59.527')
insert into @Table values (2,100,5,0, 0, '2008-10-29 11:00:37.030')
insert into @Table values (3,101,22,0, 0, '2009-10-29 11:10:27.687')
insert into @Table values (5,101,44,0, 0, '2008-10-31 12:07:52.917')
insert into @Table values (6,101,32,0, 0, '2009-06-30 08:16:12.343')
insert into @Table values (7,101,44,0, 0, '2009-10-30 10:12:11.369')
insert into @Table values (8,101,44,1, 0, '2009-10-31 10:12:11.369')

;WITH    rows AS
        (
        SELECT  *, ROW_NUMBER() OVER (PARTITION BY ticketid ORDER BY savedate DESC) AS rn
        FROM    @Table
        )
SELECT  rl.sponsor, ro.savedate
FROM    rows rl
CROSS APPLY
        (
        SELECT  TOP 1 rc.savedate
        FROM    rows rc
        JOIN    rows rn
        ON      rn.ticketid = rc.ticketid
                AND rn.rn = rc.rn + 1
                AND rn.sponsor <> rc.sponsor
        WHERE   rc.ticketid = rl.ticketid
        ORDER BY
                rc.rn
        ) ro
WHERE   rl.rn = 1

我为赞助商44添加了一条记录到测试数据。

查询将返回行17,因为赞助商未在行8中更改。