Solved

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

Posted on 2011-03-02
2
166 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 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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

733 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