Home > OS >  How to find the earliest time a foreign key was added to a table in SQL
How to find the earliest time a foreign key was added to a table in SQL

Time:05-23

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   |
 ---------------- --------------- 

CodePudding user response:

Try using MIN() function

SELECT [Foreign Key]
      ,MIN(Date) as [Date]
FROM YourTable
GROUP BY [Foreign Key]
  • Related