Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2007-11-16
4
Medium Priority
?
1,265 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 93

Accepted Solution

by:
Patrick Matthews earned 2000 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 93

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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

722 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