?
Solved

Get the max date with corresponding code which < = current date

Posted on 2011-03-02
2
Medium Priority
?
169 Views
Last Modified: 2012-05-11
I have a Excel spread sheet with table and data in it.
Also the required final result.


The code i have is not picking the max date <= getdate()
what is that i am doing wrong
SELECT ELIG.ID_STD_DEMO_DISTRICT,ELIG.TXADAELIGCODE, MAXADA.MAXTXADAELIGDATE ,AEC.ADA_ELIGIBILITY_CODE_CODE
        FROM dbo.TXADAELIG ELIG
			 INNER JOIN 
				  (SELECT ID_STD_DEMO_DISTRICT, MAX(TXADAELIGDATE) MAXTXADAELIGDATE 
						FROM dbo.TXADAELIG 	
						GROUP BY ID_STD_DEMO_DISTRICT 
					)MAXADA					
			  ON ELIG.ID_STD_DEMO_DISTRICT = MAXADA.ID_STD_DEMO_DISTRICT
			  AND ELIG.TXADAELIGDATE = MAXADA.MAXTXADAELIGDATE 
			  and MAXADA.MAXTXADAELIGDATE <= getdate()
			  
INNER JOIN dbo.CDM_SET_ADA_ELIGIBILITY_CODE AEC(NOLOCK)
ON AEC.ID_SET_ADA_ELIGIBILITY_CODE = ELIG.TXADAELIGCODE

WHERE ELIG.ID_STD_DEMO_DISTRICT =5001

Open in new window

test.xls
0
Comment
Question by:mercybthomas74
[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
2 Comments
 
LVL 23

Accepted Solution

by:
wdosanjos earned 2000 total points
ID: 35018275
Try this:

SELECT ELIG.ID_STD_DEMO_DISTRICT,ELIG.TXADAELIGCODE, MAXADA.MAXTXADAELIGDATE ,AEC.ADA_ELIGIBILITY_CODE_CODE
        FROM dbo.TXADAELIG ELIG
			 INNER JOIN 
				  (SELECT ID_STD_DEMO_DISTRICT, MAX(TXADAELIGDATE) MAXTXADAELIGDATE 
						FROM dbo.TXADAELIG 	
						WHERE TXADAELIGDATE <= getdate()
						GROUP BY ID_STD_DEMO_DISTRICT 
					)MAXADA					
			  ON ELIG.ID_STD_DEMO_DISTRICT = MAXADA.ID_STD_DEMO_DISTRICT
			  AND ELIG.TXADAELIGDATE = MAXADA.MAXTXADAELIGDATE 
			  
INNER JOIN dbo.CDM_SET_ADA_ELIGIBILITY_CODE AEC(NOLOCK)
ON AEC.ID_SET_ADA_ELIGIBILITY_CODE = ELIG.TXADAELIGCODE

WHERE ELIG.ID_STD_DEMO_DISTRICT =5001

Open in new window

0
 
LVL 15

Expert Comment

by:derekkromm
ID: 35018284
Right now its going to return results where the max date is <= getdate()

So if its

3/1, 3/2, and 3/3, it won't return since 3/3 is not <= getdate()

If you want it to return 3/2, move the "date <= getdate()" line inside the subquery

INNER JOIN
                          (SELECT ID_STD_DEMO_DISTRICT, MAX(TXADAELIGDATE) MAXTXADAELIGDATE
                                    FROM dbo.TXADAELIG       
                                    WHERE TXADAELIGDATE <= getdate()
                                    GROUP BY ID_STD_DEMO_DISTRICT
                              )MAXADA                              
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

762 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