简体   繁体   中英

SQL grammar for SELECT MIN(DATE)

I have a table with structure: 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.

You need to use GROUP BY instead of DISTINCT if you want to use aggregation functions.

SELECT title, MIN(date)
FROM table
GROUP BY title

An aggregate function requires a GROUP BY in standard SQL

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

如果您想获取更新的记录,则可以使用以下查询。

SELECT title, MAX(date) FROM table GROUP BY title
SELECT  MIN(Date)  AS Date  FROM tbl_Employee /*To get First date Of Employee*/

要获取今天大于一周前的日期标题,请使用以下内容:

SELECT title, MIN(date_key_no) AS intro_date FROM table HAVING MIN(date_key_no)>= TO_NUMBER(TO_CHAR(SysDate, 'YYYYMMDD')) - 7

You are missing a GROUP BY here.

SELECT title, MIN (date) FROM table GROUP BY title

Above should fix this. And you don't even need a DISTINCT now.

SELECT MIN(t.date)
FROM table t

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM