Solved

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

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

724 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