Click here to Skip to main content
15,899,475 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can you please assist I have the below sql script to get all registrant on the system with the latest status that the registrant is current on.
On table A below when I execute this script below am expecting to get the top record for this registrant like the one with the latest created date (2018-02-27) but that is not what the script is returning, its returning the one with this created date (2007-06-12).
Can you please assist by checking what is wrong with my script below.

Table A.
Forename1	Surname	     Status	      Created  		       dcStatus
Mmi Eagles	Tshebele	A	2018-02-27 11:40:50.793	          L         
Mmi Eagles	Tshebele	H	2010-02-26 17:24:22.410	          L         
Mmi Eagles	Tshebele	B	2007-11-02 07:47:50.297   	   L         
Mmi Eagles	Tshebele	A	2007-06-12 12:18:46.993 	   L


What I have tried:

Select 
  d.Forename1,
  d.Surname,
  s.Status,
  s.Created,
  u.dcStatus
   FROM [Warehouse1].[dbo].[User]u
   Inner join [DebtRelief] d
   inner join [Statuses] s
   on s.CrID=d.ID
   on u.UserID = d.Createdby
     where u.dcStatus = 'l' 
   order by s.Created desc
Posted
Updated 8-Jul-19 23:02pm
v2
Comments
deepankarbhatnagar 9-Jul-19 6:33am    
You need to provide all the tables used here with data to resolve this issue..

1 solution

If you are trying to fetch the latest status per registrant, then you need to look at starting with an SQL GROUP BY Statement[^] to "put together" all the individual members status records. You can then return the member and their latest date by using the MAX aggregate function as a subquery. JOIN that to your status table and you can get just the rows you need from that. JOINing that to the rest of your data should be trivial.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900