简体   繁体   English

分组依据:如果我不想对列执行聚合函数怎么办?

[英]Group By: What if I don't want to perform an aggregate function on a column?

I have a table, Students, with the following columns: 我有一个表格,学生,有以下几列:

________________________________________________
| id   |  name  |  class  |  date_registrered  | 
------------------------------------------------

I want to select one row for every unique class, and only the row with the largest value in date_registrered, ie I want to select the latest registrered Student for every class, including all the data for that one. 我想为每个唯一的班级选择一行,并且只选择date_registrered中具有最大值的行,即我想为每个班级选择最新的注册学生,包括该班级的所有数据。

I tried: 我试过了:

SELECT id, name, class, MAX(date_registrered)
FROM Students
GROUP BY class;

I get the following error: 我收到以下错误:

Column 'Students.id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. 选择列表中的“ Students.id”列无效,因为它既不包含在聚合函数中也不在GROUP BY子句中。

This question on SO adresses a simplified version of this issue. SO上的这个问题解决了这个问题的简化版本。 However, the example is for 2 columns only. 但是,该示例仅适用于2列。

I only want to group by class, and I only want to perform an aggregate function on date_registrered. 我只想按类分组,并且只想对date_registrered执行聚合函数。 I also want to display all the other columns for the row with the max date_registrered for every class. 我还想显示该行的所有其他列,以及每个类的最大date_registrered。

Do you know how to fix it? 你知道怎么解决吗?

use ROW_NUMBER() 使用ROW_NUMBER()

SELECT *
FROM ( SELECT id, name, class, date_registrered
              ROW_NUMBER() OVER (partition by class ORDER BY date_registrered DESC) rn
       FROM Students
     ) T
WHERE T.rn = 1

The error message explains your issue very well, you can't perform an aggregation on one column, and not use the rest in the GROUP BY . 错误消息很好地说明了您的问题,您无法在一个列上执行汇总,也不能在GROUP BY使用其余的汇总。 In this case, you'll want to use something like ROW_NUMBER : 在这种情况下,您需要使用类似ROW_NUMBER

WITH CTE AS
(
    SELECT  id,
            name,
            class,
            date_registered,
            RN = ROW_NUMBER() OVER(PARTITION BY class ORDER BY date_registrered DESC)
    FROM students
)
SELECT  id,
        name,
        class,
        date_registered
FROM CTE
WHERE RN = 1;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我不想分组的列=> COLUMN必须出现在GROUP BY子句中或在汇总FUNCTION中使用 - Column I don't want to group => COLUMN must appear IN the GROUP BY clause OR be used IN an aggregate FUNCTION 如果没有可在T-SQL PIVOT中用作聚合函数的列怎么办? - What if I don't have a column I can use as an aggregate function in my T-SQL PIVOT? SQL:选择 max(A), B 但不想分组或聚合 B - SQL: select max(A), B but don't want to group by or aggregate B connot使用GROUP BY执行聚合函数 - connot perform an aggregate function … with GROUP BY Postgres要求我不要GROUP BY - Postgres request GROUP BY that I don't want 在 group by 期间,我需要使用一个不在 group by 中使用的变量,我也不想使用它的聚合函数(我想要它原样) - During group by I need to take a variable which is not using in group by also I don't want to take its aggregation function (I want it as it is) 当我不想使用聚合时,是否应该使用Pivot将SQL Server 2008行值转换为列名? - Should I use Pivot to translate SQL Server 2008 row values to column names when I don't want to use an aggregate? 我不明白聚合函数是什么以及它如何应用于我的查询 - I don't understand what aggregate function is and how it applies to my query 按要求分组,甚至以为我不想分组 - Group by required even thought i don't want to group by SQL-创建要在案例中使用但不想按其分组的列 - SQL - Creating a column to use in a case but don't want to group by it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM