Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Select MIN date from view

Posted on 2004-03-24
2
Medium Priority
?
326 Views
Last Modified: 2008-02-26
I have a view (vwHistory).  This view is a history of events that have happened to a claim.  It looks something like this:

vwHistory
ClaimNumber (varchar)
EventDate (DateTime)
Attribute1 (varchar)
Attribute2 (varchar)
Attribute3 (varchar)

ClaimNumber and EventDate together make a composite key.

I need a select statement that will give me all of the data values for the earliest EventDate for a particular claim number.

For example if my table looks like this:

ClaimNumber   EventDate     Att1        Att2      Att3
1                     1/1/04          A            B          C
1                     1/2/04          D            E          F
1                     1/3/04          G            H          I
2                     1/1/04          J             K          L
2                     1/2/04          M           N          O
3                     1/5/04          P            Q          R

Then my result set would look like this:

ClaimNumber   EventDate     Att1        Att2      Att3
1                     1/1/04          A            B          C
2                     1/1/04          J             K          L
3                     1/5/04          P            Q          R


Attributes 1 - 3 may or may not be identical for each row.


Thanks in advance,
HawkeyeNash
0
Comment
Question by:HawkeyeNash
2 Comments
 
LVL 7

Accepted Solution

by:
ChrisFretwell earned 500 total points
ID: 10671741
Here's one way

select vw.claimnumber, vw.eventdate, vw.att1, vw.att2,vw.att3
from vwhistory vw join
(select claimnumber, min(eventdate) as mindate from vwhistory group by claimnumber) as v2 on vw.claimnumber = v2.claimnumber and vw.eventdate = v2.mindate
order by vw.claimnumber
0
 

Author Comment

by:HawkeyeNash
ID: 10671811
Thanks, Chris.

Had the join in the wrong place.

Much appreciated.
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
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.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

885 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