Solved

SQL Query - Get most recent entry for each member

Posted on 2008-10-08
10
1,281 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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
 
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
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 setup several different housekeeping processes for a SQL Server.

696 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