Solved

SQL Question - Retrieve latest records per initial

Posted on 2016-08-31
8
66 Views
Last Modified: 2016-09-01
Hi Experts,

I have a table named HistoryChanges with following fields

ID (PK) Int Auto num
Initial varchar (5)
DateEntered DateTime
Other columns are not relevant

Would like to get all records from a given initial which were entered latest.

Meaning if Initial A entered first 5 records then Initial B entered another 5, later Initial A entered 2 records,

If I'm looking for Initial A I would only want see the last two records, while if searching for Initial B I would want see 5 records.

This could be determined either by ID which is auto num or by DateEntered which captures date and time record was entered.

PS, I have SQL Express 2005.
0
Comment
Question by:bfuchs
  • 4
  • 3
8 Comments
 
LVL 12

Expert Comment

by:funwithdotnet
Comment Utility
Something like:
SELECT * FROM [dbo].[MyTable]
WHERE DateEntered > (
-- The date of second most recent initial.
SELECT TOP 1 DateEntered 
FROM [dbo].[MyTable] a
where Initial NOT IN (
-- The most recent initial
SELECT TOP 1 Initial 
FROM [dbo].[MyTable] a
ORDER BY DateEntered DESC
)
ORDER BY DateEntered DESC
) 

Open in new window


should work okay.

Good luck!
0
 
LVL 40

Expert Comment

by:Sharath
Comment Utility
you can try like this.
;with cte as (
select ID - row_number() over (partition by initial order by ID) rn ,*
  from HistoryChanges),
       cte1 as (
select *,rank() over (partition by initial order by rn desc) rn1
  from cte)
select ID, initial, DateEntered
  from cte1
 where rn1 = 1
order by id

Open in new window


Tested with this sample data.
declare @HistoryChanges table (ID int identity, initial varchar(5), DateEntered datetime)
insert @HistoryChanges values ('A', '2016-08-30 10:00:00')
insert @HistoryChanges values ('A', '2016-08-30 10:01:00')
insert @HistoryChanges values ('A', '2016-08-30 10:02:00')
insert @HistoryChanges values ('B', '2016-08-30 10:03:00')
insert @HistoryChanges values ('B', '2016-08-30 10:04:00')
insert @HistoryChanges values ('A', '2016-08-30 10:05:00')
insert @HistoryChanges values ('A', '2016-08-30 10:06:00')

;with cte as (
select ID - row_number() over (partition by initial order by ID) rn ,*
  from @HistoryChanges),
       cte1 as (
select *,rank() over (partition by initial order by rn desc) rn1
  from cte)
select ID, initial, DateEntered
  from cte1
 where rn1 = 1
order by id
/*
ID	initial	DateEntered
4	B	2016-08-30 10:03:00.000
5	B	2016-08-30 10:04:00.000
6	A	2016-08-30 10:05:00.000
7	A	2016-08-30 10:06:00.000
*/

Open in new window

0
 
LVL 3

Author Comment

by:bfuchs
Comment Utility
Hi Experts,

Just arrived at work, will test them and let you know.

Thanks,
Ben
0
 
LVL 3

Author Comment

by:bfuchs
Comment Utility
Hi,

I guess was not clear enough.

I need to get results from a particular Initial only, and see which were this initial latest entries,

Possible your example does that, however I'm not sure where do I specify which initial i'm looking for?

Thanks,
Ben
0
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 
LVL 40

Expert Comment

by:Sharath
Comment Utility
try like this.
declare @initial varchar(5)
select @initial = 'A'
;with cte as (
select ID - row_number() over (partition by initial order by ID) rn ,*
  from HistoryChanges
 where initial = @initial),
       cte1 as (
select *,rank() over (partition by initial order by rn desc) rn1
  from cte)
select ID, initial, DateEntered
  from cte1
 where rn1 = 1
order by id

Open in new window

0
 
LVL 3

Author Comment

by:bfuchs
Comment Utility
@Sarath,

This looks like working:)

Just one question, where do I enter additional criteria besides initial?

for example where tablename = 'somevalue' and initial is not null, meaning if initial is null then it should not count as different value and therefore consider as cut off point.

In other words if we have initial AA record 1,2,3 and null record 4 and AA record 5, I do want see 123 and 5.

Thanks,
Ben
0
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
Comment Utility
Well. In that case, you cannot rely on your auto increment ID. You can try like this.
declare @initial varchar(5)
select @initial = 'A'
;with cte as (
select row_number() over (order by ID) - row_number() over (partition by initial order by ID) rn ,*
  from HistoryChanges
 where initial is not null),
       cte1 as (
select *,rank() over (partition by initial order by rn desc) rn1
  from cte)
select ID, initial, DateEntered
  from cte1
 where rn1 = 1
   and initial = @initial
order by id

Open in new window

0
 
LVL 3

Author Closing Comment

by:bfuchs
Comment Utility
Excelent!!

Thank You!
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

Creating and Managing Databases with phpMyAdmin in cPanel.
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

744 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

17 Experts available now in Live!

Get 1:1 Help Now