如何在SQL查询中选择每个组的第一行?

时间:2009-12-10 15:25:29

标签: sql sql-server-ce

我有这个SQL查询:

SELECT   Foo, Bar, SUM(Values) AS Sum
FROM     SomeTable
GROUP BY Foo, Bar
ORDER BY Foo DESC, Sum DESC

这导致输出类似于:

47  1   100
47  0   10
47  2   10
46  0   100
46  1   10
46  2   10
44  0   2

我希望每个Foo只有第一行而忽略其余部分。

47  1   100
46  0   100
44  0   2

我该怎么做?

9 个答案:

答案 0 :(得分:23)

declare @sometable table ( foo int, bar int, value int )

insert into @sometable values (47, 1, 100)
insert into @sometable values (47, 0, 10)
insert into @sometable values (47, 2, 10)
insert into @sometable values (46, 0, 100)
insert into @sometable values (46, 1, 10)
insert into @sometable values (46, 2, 10)
insert into @sometable values (44, 0, 2)

;WITH cte AS 
(
    SELECT   Foo, Bar, SUM(value) AS SumValue, ROW_NUMBER() OVER(PARTITION BY Foo ORDER BY FOO DESC, SUM(value) DESC) AS RowNumber
    FROM     @SomeTable
    GROUP BY Foo, Bar
)
SELECT * 
FROM cte
WHERE RowNumber = 1

答案 1 :(得分:2)

我可能不同意rjmunru,因为使用Ansii样式连接通常比子查询更容易阅读,但对于他自己的每个 - 我只是按照我们的DBA所说的去做。

如果您只想查询查询的第一个结果,您可能可以使用rownum(如果使用oracle,其他数据库可能有类似的东西)。

从foo_t f中选择* 其中f.bar ='bleh'和rownum = 1

当然,HAVING子句也可能是合适的,具体取决于你想要做什么。

“HAVING用于对GROUP BY创建的组执行类似于基本SQL语句中行的WHERE子句的操作.WHERE子句限制评估的行.HAVING子句限制返回的分组行。”

HTH

答案 2 :(得分:1)

仅在Players.Nick上进行分组,然后选择说明的第一个(分钟)

SELECT     Players.Nick, MIN(Reasons.Description), SUM(Marks.Value) AS Sum
FROM         Marks INNER JOIN
                      Players ON Marks.PlayerID = Players.ID INNER JOIN
                      Reasons ON Marks.ReasonId = Reasons.ID
GROUP BY Players.Nick
ORDER BY Players.Nick, Sum DESC

如果你总是想要第一个而不知道它

答案 3 :(得分:1)

这是一个老帖子,但今天我遇到了同样的问题。我通过尝试许多查询来解决它,直到它工作。我正在使用SQL Compact 3.5和visual basic 2010。

此示例适用于名为“TESTMAX”的表,其中包含“Id”(主键),“nom”(名称)和“value”列,您可以使用此示例获取每个“nom”具有最大“值”的行“:

SELECT TESTMAX.Id, TESTMAX.NOM, TESTMAX.Value
FROM     TESTMAX INNER JOIN
                  TESTMAX AS TESTMAX_1 ON TESTMAX.NOM = TESTMAX_1.NOM
WHERE  (TESTMAX.Value IN
                      (SELECT MAX(Value) AS Expr1
                       FROM      TESTMAX AS TESTMAX_2
                       WHERE   (NOM = TESTMAX_1.NOM)))
GROUP BY TESTMAX.Id, TESTMAX.NOM, TESTMAX.Value

如果要删除其他行,也可以使用:

DELETE FROM TESTMAX
WHERE  (Id NOT IN
                      (SELECT TESTMAX_3.Id
                       FROM      TESTMAX AS TESTMAX_3 INNER JOIN
                                         TESTMAX AS TESTMAX_1 ON TESTMAX_3.NOM = TESTMAX_1.NOM
                       WHERE   (TESTMAX_3.Value IN
                                             (SELECT MAX(Value) AS Expr1
                                              FROM      TESTMAX AS TESTMAX_2
                                              WHERE   (NOM = TESTMAX_1.NOM)))
                       GROUP BY TESTMAX_3.Id, TESTMAX_3.NOM, TESTMAX_3.Value))

答案 4 :(得分:0)

(已编辑基于已编辑的问题) 然后,由于您希望根据聚合列的值进行筛选,因此您需要的是具有子句。

  SELECT p.Nick, r.Description, SUM(m.Value) Sum
  FROM Marks m
    JOIN Players p
      ON m.PlayerID = p.ID 
    JOIN Reasons r 
      ON m.ReasonId = r.ID
  GROUP BY p.Nick, r.Description
  Having SUM(m.Value) =
      (Select Max(Sum) From
        (SELECT SUM(m.Value) Sum
         FROM Marks mi
           JOIN Players pi
              ON mi.PlayerID = pi.ID 
           JOIN Reasons r i
             ON mi.ReasonId = ri.ID
         Where pi.Nick = p.Nick
         GROUP BY pi.Nick, ri.Description))

  Order By p.Nick, Sum Desc

答案 5 :(得分:0)

通常,尝试使用子查询而不是加入和分组 - 它通常会使SQL更容易理解。

SELECT Nick,
   (SELECT Description from Reasons WHERE Reasons.ID = (
       SELECT FIRST(Marks.ReasonId) from Marks WHERE Marks.PlayerID = Players.ID)
   ),
   (SELECT SUM(Value) from Marks WHERE Marks.PlayerID = Players.ID)

答案 6 :(得分:0)

这是一个使用'HAVING'条款的机会吗? (你想区分聚合函数 - 'Sum')?

答案 7 :(得分:0)

好奇。只有这样才能让它工作才能在内存中使用临时保存表。 (TSQL语法)

-- original test data
declare @sometable table ( foo int, bar int, value int )

insert into @sometable values (1, 5, 10)
insert into @sometable values (1, 4, 20)
insert into @sometable values (2, 1, 1)
insert into @sometable values (2, 1, 10)
insert into @sometable values (2, 1, 1)
insert into @sometable values (2, 2, 13)
insert into @sometable values (3, 4, 25)
insert into @sometable values (3, 5, 1)
insert into @sometable values (3, 1, 1)
insert into @sometable values (3, 1, 1)
insert into @sometable values (3, 1, 1)
insert into @sometable values (3, 1, 1)
insert into @sometable values (3, 1, 1)

-- temp table for initial aggregation
declare @t2 table (foo int, bar int, sums int)
insert into @t2
select foo, bar, sum(value) 
from @sometable
group by foo, bar

-- final result
select foo, bar, sums
from @t2 a
where sums = 
    (select max(sums) from @t2 b 
     where b.foo = a.foo)

答案 8 :(得分:0)

SQL Server 2005可以使用它:

声明@sometable表(foo int,bar int,value int)

插入@sometable值(1,5,10) 插入@sometable值(1,4,20) 插入@sometable值(2,1,1) 插入@sometable值(2,1,10) 插入@sometable值(2,1,1) 插入@sometable值(2,2,13) 插入@sometable值(3,4,25) 插入@sometable值(3,5,1) 插入@sometable值(3,1,1) 插入@sometable值(3,1,1) 插入@sometable值(3,1,1) 插入@sometable值(3,1,1) 插入@sometable值(3,1,1)

- 初始聚合的临时表 声明@t2表(foo int,bar int,sums int) 插入@ t2 选择foo,bar,sum(value) 来自@sometable foo,bar

选择*来自(  SELECT foo,bar,sums,ROW_NUMBER()OVER(由Foo ORDER BY Sums DESC划分)ROWNO  FROM @ t2)x其中x.ROWNO = 1