Solved

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

Posted on 2007-11-16
4
1,259 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

740 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