Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 How do I get the 'Max Date' record???

Author  Topic 

jonathanr
Starting Member

11 Posts

Posted - 2011-09-12 : 23:19:03
Hi,

I have a query (below) that I am trying to get the most recent date for all Formatted_Licence_Number lines. The Inspections_Facts_Key is unique.

In the results I am getting several lines (dates) for the same Formatted_Licence_Number where what I want is to get 1 line for the most recent date for each Formatted_Licence_Number. Any tips will be most apprecited.

Thanks,

Jonathanr

[QUERY]
SELECT
MAX (Inspection_Date) AS Date,
Formatted_Licence_Number,
t2.Inspections_Facts_Key

FROM
Infodbo.Licensing_Facts t1
INNER JOIN Infodbo.Inspections_Facts t2
ON t1.Licensing_Facts_Key=t2.Licensing_Facts_Key


WHERE t1.Type_Code = 'fp'
AND t1.Class_Code = 'health'
AND t2.Result_Code IS NOT NULL
GROUP BY Formatted_Licence_Number,t2.Inspections_Facts_Key

ORDER BY Formatted_Licence_Number

[RESULT]
Date Formatted_Licence_Number Inspections_Facts_Key
29/06/2009 FP1 15103
06/01/2009 FP1 20373
15/03/2011 FP1 21383
06/01/2010 FP1 22162
21/10/2008 FP10 8170
22/06/2009 FP10 9209
19/07/2011 FP100 12940
12/01/2010 FP100 13636
18/04/2011 FP100 13802

jonathanr
Starting Member

11 Posts

Posted - 2011-09-12 : 23:23:54
PS. The result I am looking for is something like:

Date Formatted_Licence_Number Inspections_Facts_Key
06/01/2010 FP1 22162
22/06/2009 FP10 9209
19/07/2011 FP100 12940
Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2011-09-12 : 23:54:47
With a CTE
With	e (Inspection_Date, Formatted_Licence_Number, Inspections_Facts_Key, id)
AS (

SELECT Inspection_Date, Formatted_Licence_Number, t2.Inspections_Facts_Key,
RowNumber() OVER (partition by Inspection_Date, Formatted_Licence_Number ORDER BY Inspection_Date DESC) as id
FROM Infodbo.Licensing_Facts t1
INNER JOIN
Infodbo.Inspections_Facts t2
ON t1.Licensing_Facts_Key = t2.Licensing_Facts_Key

WHERE t1.Type_Code = 'fp'
AND t1.Class_Code = 'health'
AND t2.Result_Code IS NOT NULL
)

SELECT Inspection_Date, Formatted_Licence_Number, Inspections_Facts_Key
FROM e
WHERE id = 1;


With a correlated subquery
SELECT	Inspection_Date, Formatted_Licence_Number, t2.Inspections_Facts_Key,
FROM Infodbo.Licensing_Facts t1
INNER JOIN
Infodbo.Inspections_Facts t2
ON t1.Licensing_Facts_Key = t2.Licensing_Facts_Key
INNER JOIN (
SELECT Formatted_Licence_Number, MAX(Inspection_Date) dt
FROM Infodbo.Licensing_Facts t1
INNER JOIN
Infodbo.Inspections_Facts t2
ON t1.Licensing_Facts_Key = t2.Licensing_Facts_Key
GROUP BY
Formatted_Licence_Number
) x
On x.Formatted_Licence_Number = t1.Formatted_Licence_Number
And x.dt = t2.Inspection_Date
WHERE t1.Type_Code = 'fp'
AND t1.Class_Code = 'health'
AND t2.Result_Code IS NOT NULL;
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-13 : 00:08:47
[code]SELECT Date,Formatted_Licence_Number,Inspections_Facts_Key
FROM
(
SELECT ROW_NUMBER() OVER(PARTITION BY Formatted_Licence_Number ORDER BY Inspections_Facts_Key
DESC) AS Seq,
Inspection_Date AS Date,
Formatted_Licence_Number,
t2.Inspections_Facts_Key

FROM
Infodbo.Licensing_Facts t1
INNER JOIN Infodbo.Inspections_Facts t2
ON t1.Licensing_Facts_Key=t2.Licensing_Facts_Key


WHERE t1.Type_Code = 'fp'
AND t1.Class_Code = 'health'
AND t2.Result_Code IS NOT NULL
)t
WHERE Seq=1
ORDER BY Formatted_Licence_Number
[/code]



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

jonathanr
Starting Member

11 Posts

Posted - 2011-09-13 : 01:10:10
Thanks guys,

visakh16 - when I run a query that returns all rows the following is returned for FP1:


27/10/2008 FP1 14560
17/12/2008 FP1 14696
06/01/2009 FP1 20373
04/05/2009 FP1 8948
22/06/2009 FP1 15079
29/06/2009 FP1 15103
23/10/2009 FP1 9836
28/10/2009 FP1 9865
06/01/2010 FP1 22162
19/01/2010 FP1 10280
27/01/2010 FP1 10301
07/05/2010 FP1 10860
14/05/2010 FP1 10898
17/02/2011 FP1 12194
15/03/2011 FP1 21383
19/07/2011 FP1 14065

When I run your query the following is returned for FP1:
06/01/2010 FP1 22162

I was hoping the following would be returned:
19/07/2011 FP1 14065

Cheers,

jonathanr
Go to Top of Page

jonathanr
Starting Member

11 Posts

Posted - 2011-09-13 : 01:18:48
russell

It looks like your correlated subquery is returning the correct values.

Cheers,

jonathanr
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-13 : 01:20:18
for that you need this

SELECT Date,Formatted_Licence_Number,Inspections_Facts_Key
FROM
(
SELECT ROW_NUMBER() OVER(PARTITION BY Formatted_Licence_Number ORDER BY Inspection_Date DESC) AS Seq,
Inspection_Date AS Date,
Formatted_Licence_Number,
t2.Inspections_Facts_Key

FROM
Infodbo.Licensing_Facts t1
INNER JOIN Infodbo.Inspections_Facts t2
ON t1.Licensing_Facts_Key=t2.Licensing_Facts_Key


WHERE t1.Type_Code = 'fp'
AND t1.Class_Code = 'health'
AND t2.Result_Code IS NOT NULL
)t
WHERE Seq=1
ORDER BY Formatted_Licence_Number






------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

jonathanr
Starting Member

11 Posts

Posted - 2011-09-13 : 01:28:27
Thank you visakh16 and russel - you both rock!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-13 : 02:16:00
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -