sql服务器如何区分多列而非所有列的数据

sql server how to distinct data from multiple column but not all column

我有一个问题,我需要显示来自某些 table、sql 服务器的唯一数据,我有查询但如果不是所有列 I select 我有一个错误GROUP BY.

中不包含

Table 来源:

+------+------+----------+-------+
| code | name | process  | time  |
+------+------+----------+-------+
| a    | qq   | write    | 10:10 |
| b    | qq   | read     | 11:10 |
| c    | qq   | read     | 12:11 |
| a    | qq   | write    | 09:40 |
| b    | yy   | write    | 08:50 |
| a    | yy   | read     | 11:10 |
| b    | yy   | write    | 09:40 |
+------+------+----------+-------+

Table我需要:

+------+------+----------+-------+
| code | name | process  | time  |
+------+------+----------+-------+
| a    | qq   | write    | 10:10 |
| b    | qq   | read     | 11:10 |
| b    | yy   | write    | 09:40 |
| c    | qq   | read     | 12:11 |
| a    | yy   | read     | 11:10 |
+------+------+----------+-------+

我使用的代码:

select code, name, process, time 
from tablesource 
group by code, name, process;

但是这个查询结果出错:

[42000] [Microsoft][SQL Server Native Client 10.0][SQL Server]Column 'time' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. (8120),

但是如果我在 group by 中插入列时间,结果与 table 源相同。请帮助我。

您需要修改查询以添加时间聚合函数。

select code, name, process, max(time) from tablesource 
group by code, name, process;