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
Solved

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

Posted on 2011-03-02
2
165 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
2 Comments
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Query - which index being used? 2 61
upgrade sql 2005 32bit to sql 2008 32 or 64bit on a server 2008 r2 box 6 76
Need help debbuging stored procedure 21 44
Help  needed 3 36
When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

828 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