Solved

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

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Need help with a query 4 44
temp table - optimal records for performance.. (tsql) 6 23
Stored procedure query with if 27 32
SQL HELP 2 75
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now