userNid
userNid

Reputation: 99

SQL Min function on Date

How does Min function work on dates ? If 2 records have the same date and time stamp , the min function returns 1. Does it pull records based on when it was put into the table ?

Upvotes: 1

Views: 10683

Answers (3)

dognose
dognose

Reputation: 20889

Elliot already expained it.

Just a sidenode, if you are using MySQL: MySQL allows to aggregate on a certain column, while fetching other columns without aggregation. (SQL Server does NOT allow that!)

Example:

date       | name
2015-03-06 | A
2015-03-06 | B

Using SELECT Min(date), name FROM table on MySQL will return various results. Sometimes it will be

2015-03-06 | A

sometimes

2015-03-06 | B

Docu:

When using this feature, all rows in each group should have the same values for the columns that are omitted from the GROUP BY part. The server is free to return any value from the group, so the results are indeterminate unless all values are the same.

SQL Server will throw an error, that no aggregation has been performed on column name. See also http://dev.mysql.com/doc/refman/5.0/en/group-by-handling.html

MySQL works this way, cause sometimes grouping on the second column is not really required, for example:

SELECT MAX(id), user_id FROM posts WHERE user_id = 6

(There could be NO other user_id than 6, so aggregation is not required in MySQL - However not paying attention on THIS will lead to wrong results as example one shows.)

Upvotes: 0

RLoniello
RLoniello

Reputation: 2329

MIN() returns the smallest of all selected values of a column. It seems to me that your statement may simply be asking if a minimum exists.

Please post your sql statement.

possibly this is what you need:

SELECT MIN (date) AS "Min Date"  
FROM tablename;

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201409

MIN is an aggregate function so it will return 1 record in your question's case. Since the two records have the same date and timestamp it doesn't matter which date and timestamp are returned (they're the same). Finally, the time the records were inserted is not considered.

Upvotes: 2

Related Questions