Solved

SQL Query - Get most recent entry for each member

Posted on 2008-10-08
10
1,266 Views
Last Modified: 2008-10-20
I'm stuck on developing this query and could use some help. What I am trying to do is look at a table (BP) and find how long it has been since the last entry was made for each entity. The query I have developed does that, but if an entity has more than one entry in there, it shows both and I just want the most recent one displayed. Hopefully this makes sense.
SELECT b.BPID, a.EntityShortName, b.DateEntered, DateDiff(day,b.DateEntered,GetDate()) DaysSinceLastBP

FROM BP AS b 

INNER JOIN RiskMgmtAssignments AS a ON b.EntityID = a.EntityID 

INNER JOIN RMCs AS rm ON a.RMCID = rm.ID

WHERE (b.IsComplete = 1) AND (rm.Name = @username)

ORDER BY 

DateDiff(day,b.DateEntered,GetDate()) DESC

Open in new window

0
Comment
Question by:cciservices
  • 4
  • 4
  • 2
10 Comments
 
LVL 1

Accepted Solution

by:
cciservices earned 0 total points
ID: 22671465
I came up with this query that appears to do the trick, but if someone knows a more efficient way to get the same result, I'm happy to give you the points for it.
SELECT DISTINCT a.EntityShortName, 

(SELECT TOP 1 DateEntered FROM BP WHERE EntityID = a.EntityID ORDER BY DateEntered DESC) DateEntered, 

DateDiff(day,(SELECT TOP 1 DateEntered FROM BP WHERE EntityID = a.EntityID ORDER BY DateEntered DESC),GetDate()) DaysSinceLastBP

FROM BP AS b 

INNER JOIN RiskMgmtAssignments AS a ON b.EntityID = a.EntityID 

INNER JOIN RMCs AS rm ON a.RMCID = rm.ID

WHERE (b.IsComplete = 1) AND (rm.Name = @username)

ORDER BY DaysSinceLastBP DESC

Open in new window

0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22671478
Try this:

select b.bpid,dateentered, datediff(d,b.dateentered,getdate()) DaysSinceLastBP
from (select bpid,entityid,dateentered = max(dateentered) from bp where iscomplete=1 group by bpid,entityid ) b
where exists (select 1 from riskmgmtassignments a join rmcs rm on a.rmcid=rm.id where a.entityid=b.entityid and rm.name=@username)
order by datediff(d,b.dateentered,getdate())  desc
0
 
LVL 3

Expert Comment

by:3abqari
ID: 22671505
Use the "Top" keyword with how many records you would like returned.

for example: Select Top 1 * from tablename


SELECT TOP 1 b.BPID, a.EntityShortName, b.DateEntered, DateDiff(day,b.DateEntered,GetDate()) DaysSinceLastBP

FROM BP AS b 

INNER JOIN RiskMgmtAssignments AS a ON b.EntityID = a.EntityID 

INNER JOIN RMCs AS rm ON a.RMCID = rm.ID

WHERE (b.IsComplete = 1) AND (rm.Name = @username)

ORDER BY 

DateDiff(day,b.DateEntered,GetDate()) DESC

Open in new window

0
 
LVL 1

Author Comment

by:cciservices
ID: 22671521
Brandon,
Nope. That didn't do it. Still pulls in more than 1 entry for each entity. Thanks, though.
0
 
LVL 1

Author Comment

by:cciservices
ID: 22671557
3abqari,
That is only going to get me one record total and that is not what I am looking for. I am looking for 1 entry per entity. For instance, there are 42 different entities and 60 entries. I need the most recent entries for those 42, so there should be 42 records returned with the most recent dateEntered for each of them.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22671581
which part is doing it?


select 1 from riskmgmtassignments a join rmcs rm on a.rmcid=rm.id where rm.name=@username

or

select bpid,entityid,dateentered = max(dateentered) from bp where iscomplete=1 group by bpid,entityid
0
 
LVL 3

Expert Comment

by:3abqari
ID: 22671592
Have you tried adding a group by clause?
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22671595
What does this give you?


select b.bpid,dateentered, datediff(d,b.dateentered,getdate()) DaysSinceLastBP
from (select entityid,dateentered = max(dateentered) from bp where iscomplete=1 group by entityid ) b
where exists (select 1 from riskmgmtassignments a join rmcs rm on a.rmcid=rm.id where a.entityid=b.entityid and rm.name=@username)
order by datediff(d,b.dateentered,getdate())  desc

0
 
LVL 1

Author Comment

by:cciservices
ID: 22671644
That doesn't work either because you took bpid out of your subquery and adding into it and and the group by makes it return too many records as well. Did you see my second sql statement that I came up with that is working for me (ID:22671465)? I'm going to use that, but if you can write it in a more efficient manner, I'll give you the points.
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22671861
I'm trying to figure out where your one to many relationships occur which is what I was getting at in http:#22671581.

Do you have many BP to each RiskMgmtAssignments?
Do you have many RMCs to each RiskMgmtAssignments?

vice versa on either?
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

746 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

11 Experts available now in Live!

Get 1:1 Help Now