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

x
?
Solved

SQL Question - Retrieve latest records per initial

Posted on 2016-08-31
8
Medium Priority
?
83 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
ID: 41779233
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 41

Expert Comment

by:Sharath
ID: 41779988
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 4

Author Comment

by:bfuchs
ID: 41780515
Hi Experts,

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

Thanks,
Ben
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 4

Author Comment

by:bfuchs
ID: 41780616
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
 
LVL 41

Expert Comment

by:Sharath
ID: 41780653
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 4

Author Comment

by:bfuchs
ID: 41780720
@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 41

Accepted Solution

by:
Sharath earned 2000 total points
ID: 41780736
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 4

Author Closing Comment

by:bfuchs
ID: 41780788
Excelent!!

Thank You!
0

Featured Post

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

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 …
How much do you know about the future of data centers? If you're like 50% of organizations, then it's probably not enough. Read on to get up to speed on this emerging field.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

926 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