Solved

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

Posted on 2007-11-16
4
1,260 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
connection to SQL 2012 error in windows 10 18 49
Export individual report to pdf 2 41
How do you think this website does searches? 5 36
TDE for SQL Web Edition 1 42
I have a large data set and a SSIS package. How can I load this file in multi threading?
A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

752 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