Solved

Query to find min/max date for when a list of items appeared in a table

Posted on 2007-11-16
4
1,255 Views
Last Modified: 2012-08-14
Hello -

I have a table with name and dates in a table.   For each name I need to find the first and last date for which the name appears in the table.  I dont want to run any loops but for some reason can't warp my head around the query. I'll be storing the data in a temp table so if it's two queries and one as an update etc. thats fine.  I just want to avoid the performance hit of a loop or cursor.  

Output:

Name, First Date, Last Date
0
Comment
Question by:gigglick
  • 2
4 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 20299527
Hello gigglick,

SELECT [Name], MIN([Date]) AS FirstDate, MAX([Date]) AS LastDate
FROM SomeTable
GROUP BY [Name]

Regards,

Patrick
0
 
LVL 5

Author Comment

by:gigglick
ID: 20299547
oy vey...no more late night Thursdays for me!   Thanks so much - worked perfectly!!
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 20299555
:)
0
 
LVL 4

Expert Comment

by:rallsaldo
ID: 20299603
Hi,

You could use MIN and MAX functions as:

SELECT YourNameField, MIN(YourDateField) FROM yourTable GROUP BY YourNameField
SELECT YourNameField, MAX(YourDateField) FROM yourTable GROUP BY YourNameField

Giving you something along the lines of:

 SELECT DISTINCT tbl.YourNameField, qry1.MaxDate, qry2.MinDate
  FROM YourTable tbl
  INNER JOIN (SELECT TEAM_Code_Home, MAX(MAT_Date) AS MaxDate FROM YourTable GROUP BY YourNameField) qry1 ON tbl.YourNameField = qry1.YourNameField
INNER JOIN (SELECT TEAM_Code_Home, MIN(MAT_Date) AS MinDate FROM YourTable GROUP BY YourNameField) qry2 ON tbl.YourNameField = qry2.YourNameField

Hope that helps,
R
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Read about achieving the basic levels of HRIS security in the workplace.
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now