SELECT MIN(DATE) 的 SQL 语法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8779829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SQL grammar for SELECT MIN(DATE)
提问by DrXCheng
I have a table with structure:
id(INT PK), title(VARCHAR), date(DATE)
我有一个结构表:
id(INT PK), title(VARCHAR), date(DATE)
How do I select all distinct titles with their earliest date?
如何选择所有具有最早日期的不同标题?
Apparently, SELECT DISTINCT title, MIN(date) FROM table
doesn't work.
显然,SELECT DISTINCT title, MIN(date) FROM table
不起作用。
回答by ean5533
You need to use GROUP BY
instead of DISTINCT
if you want to use aggregation functions.
如果要使用聚合函数GROUP BY
,DISTINCT
则需要使用而不是。
SELECT title, MIN(date)
FROM table
GROUP BY title
回答by gbn
An aggregate function requires a GROUP BY in standard SQL
聚合函数需要标准 SQL 中的 GROUP BY
This is "Get minimum date per title" in plain language
这是简明语言的“获取每个标题的最短日期”
SELECT title, MIN(date) FROM table GROUP BY title
Most RDBMS and the standard require that column is either in the GROUP BY or in a functions (MIN, COUNT etc): MySQL is the notable exception with some extensions that give unpredictable behaviour
大多数 RDBMS 和标准要求该列位于 GROUP BY 或函数(MIN、COUNT 等)中:MySQL 是一个值得注意的例外,它的一些扩展会产生不可预测的行为
回答by JIYAUL MUSTAPHA
SELECT MIN(Date) AS Date FROM tbl_Employee /*To get First date Of Employee*/
回答by Jessica
To get the titles for dates greater than a week ago today, use this:
要获取今天大于一周前的日期的标题,请使用以下命令:
SELECT title, MIN(date_key_no) AS intro_date FROM table HAVING MIN(date_key_no)>= TO_NUMBER(TO_CHAR(SysDate, 'YYYYMMDD')) - 7
SELECT title, MIN(date_key_no) AS intro_date FROM table HAVING MIN(date_key_no)>= TO_NUMBER(TO_CHAR(SysDate, 'YYYYMMDD')) - 7