-1

My problem is that I have a list of foreign keys and the date that they were added, as so:

+----------------+---------------+
|  Foreign Key   |  Date         |
+----------------+---------------+
|  4             |  2022-05-25   |
|  5             |  2022-05-30   |
|  4             |  2022-05-30   |
+----------------+---------------+

What I require is that only the earliest date is displayed, so I do not want that second '4' as it was created after the first one. Is there a way that I can display just the earliest dates of each key? Like this:

+----------------+---------------+
|  Foreign Key   |  Date         |
+----------------+---------------+
|  4             |  2022-05-25   |
|  5             |  2022-05-30   |
+----------------+---------------+
George
  • 19
  • 8
  • What DBMS are you asking this for? You got an answer for T-SQL (Microsoft SQL Server). The answer for other DBMS may differ slightly. Are the real column names "Foreign Key" and "Date"? – Thorsten Kettner May 22 '22 at 13:53
  • Does this answer your question? [Fetch the row which has the Max value for a column](https://stackoverflow.com/questions/121387/fetch-the-row-which-has-the-max-value-for-a-column) – philipxy May 22 '22 at 20:18
  • [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/q/261592/3404097) Re (re)search success: Please before considering posting read the manual/reference & google any error message & many clear, concise & precise phrasings of your question/problem/goal, with & without your particular names/strings/numbers, 'site:stackoverflow.com' & tags; read many answers. Reflect research in posts. SO/SE search is poor & literal & sometimes limited to titles, but read the help. Google re googling/searching, including in Q&A at [meta] & [meta.se]. [ask] [Help] – philipxy May 22 '22 at 20:19

1 Answers1

5

Try using MIN() function

SELECT [Foreign Key]
      ,MIN(Date) as [Date]
FROM YourTable
GROUP BY [Foreign Key]
Dimi
  • 452
  • 1
  • 9
  • 1
    You should mention that this syntax is SQL Server specific. In standard SQL we use double quotes for names with blanks. But, well, maybe there is no blank in the real column nameand no quoting is needed after all. – Thorsten Kettner May 22 '22 at 13:51