Solved

Finding the oldest record in a group of records; some kind of looping through records?

Posted on 2014-04-01
4
153 Views
Last Modified: 2014-04-01
I have a table of data across a large group of people for charting events distinguished by these fields:

Earliest Date / Time image
as you can see any given PAT_NUM can have multiple entries against a PMP (order number) which vary in date and time.

What I'm after is a query that will work out the 1st (oldest) entry for a given PAT_NUM for a given PMP.

All fields are stored as text.
0
Comment
Question by:ghettocounselor
  • 2
  • 2
4 Comments
 
LVL 28

Expert Comment

by:sammySeltzer
ID: 39969690
I think you can do this in query.

SELEcT MAX(pat_num),... from your table

OR

SELECT * from yourtable ORDER BY pat_num ASC (ascending) displays from oldest to newest.

If you just wish to display ONE record, then use SELECT TOP 1 and Order by pat_num ASC
0
 

Author Comment

by:ghettocounselor
ID: 39969716
sorry, more clarity: I need the 1st (oldest) entry for 'each' PAT_NUM for 'each' PMP, as example: rather than the 4 entries i have above for the pat num ending in 4631I'm wanting only the oldest entry (by date and time, recognizing that there could be two entries on 1 day). The table has 100's of distinct pat_num's each of which will have 1 distinquished row of data in the end representing the oldest entry for each PMP number.
0
 
LVL 28

Accepted Solution

by:
sammySeltzer earned 500 total points
ID: 39969777
Oh, Ok.

You can try this:

;With CTE 
As
(
 Select  *  ,ROW_NUMBER() Over (Partition By Pat_num, pmp, trans_type, trans_type_desc Order By Pat_num, charted_date, charted_time ) As UN 
From  @yourTable 
)

Select Pat_num, pmp, trans_type, trans_type_desc, charted_date, charted_time From CTE Where UN = 1 Order By Pat_num

Open in new window

0
 

Author Closing Comment

by:ghettocounselor
ID: 39970010
SUPERB!

Thanks for the quick back and forth to work this out.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
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.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

708 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

19 Experts available now in Live!

Get 1:1 Help Now