Solved

SQL 2000 Query Assistance

Posted on 2011-03-10
4
194 Views
Last Modified: 2012-06-27
First and foremost, this is for SQL 2000 not SQL 2005. The client is on SQL 2000 until July of this year, so having a coding method that will work in both SQL 2000 and SQL 2005 will be nice.

Currently whats happening is when I run the

SELECT * FROM cusMMElig WHERE PatientProfileId = '241742' and AppDate = '08/04/2007' I get back three rows. I am getting back three rows because the client changed the ApptStatus 3 times. The problem with this code is that its pulling the first apptstatus as the apptstatus, when in fact I would want the most recent one.

This is the Join causing the issue:

LEFT OUTER JOIN dbo.cusCRIMedLists crml4 ON E.EligAppStatMId = crml4.MedListsId

Essentially, I want the most recent row not the first created row.
SET NOCOUNT ON

SELECT PatientName = CASE WHEN RTRIM(pp.Last + ' ' + ISNULL(pp.Suffix, '')) <> ''
                           THEN RTRIM(RTRIM(pp.Last + ' ' + ISNULL(pp.Suffix, ''))
                                      + ', ' + ISNULL(pp.First, '') + ' '
                                      + ISNULL(pp.Middle, ''))
                           ELSE RTRIM(ISNULL(pp.First, '') + ' '
                                      + ISNULL(pp.Middle, ''))
                      END,
        pp.PatientProfileId,
        pp.PatientId,
        pp.Address1,
        pp.Address2,
        pp.Suffix,
        pp.City,
        pp.State,
        pp.Zip,
        pp.Phone1,
        pp.Phone1Type,
        pp.Phone2,
        pp.Phone2Type,
        pp.EMailAddress,
        EligName = CASE WHEN RTRIM(E.ELP_Last + ' ' + ISNULL(E.ELP_Suffix, '')) <> ''
                        THEN RTRIM(RTRIM(E.ELP_Last + ' '
                                         + ISNULL(E.ELP_Suffix, '')) + ', '
                                   + ISNULL(E.ELP_First, '') + ' '
                                   + ISNULL(E.ELP_Middle, ''))
                        ELSE RTRIM(ISNULL(E.ELP_First, '') + ' '
                                   + ISNULL(E.ELP_Middle, ''))
                   END,
        E.ELP_Address1,
        E.ELP_Address2,
        E.ELP_City,
        E.ELP_State,
        E.ELP_Zip,
        E.ELP_Phone1,
        E.ELP_Phone1Type,
        E.ELP_Phone2,
        E.ELP_Phone2Type,
        E.ELP_SSN,
        E.ELP_Birthdate,
        E.AppDte,
        E.EligDecisionDte,
        E.EligPerStart,
        E.EligPerEnd,
        E.IncRptLastDte,
        E.IncRptDueDte,
        crml2.Description AS IncRptRcvd,
        E.RILstDte,
        E.RIAnnDueDte,
        crml3.Description AS RIRcvd,
        E.RIClrdDte,
        E.IncRptClrdDte,
        crml4.Description AS AppStatus,
        crml1.Description AS Decision,
        wrkr.Description AS Worker,
        aid.Description AS AidType
FROM    dbo.cusMMElig E
        INNER JOIN dbo.PatientProfile pp ON E.PatientProfileID = pp.PatientProfileId
        LEFT OUTER JOIN dbo.cusCRIMedLists aid ON E.EligTCMSAidTypeMId = aid.MedListsId
        LEFT OUTER JOIN dbo.cusCRIMedLists crml1 ON E.EligDecisionMId = crml1.MedListsId
        LEFT OUTER JOIN dbo.cusCRIMedLists crml4 ON E.EligAppStatMId = crml4.MedListsId
        LEFT OUTER JOIN dbo.cusCRIMedLists wrkr ON E.EligAssignedWrkMId = wrkr.MedListsId
        LEFT OUTER JOIN dbo.cusCRIMedLists crml3 ON E.EligRIRcvdMId = crml3.MedListsId
        LEFT OUTER JOIN dbo.cusCRIMedLists crml2 ON E.EligIncRptRcvdMId = crml2.MedListsId
WHERE   

	crml1.Description LIKE 'Pend%'
        AND crml4.Description NOT IN ( 'Discontinued' )
        AND ( E.EligDecisionDte IS NULL
              OR E.EligDecisionDte >= ISNULL(NULL, '1/1/1900')
              AND E.EligDecisionDte < DATEADD(d, 1, ISNULL(NULL, '1/1/3000'))
            )

Open in new window

0
Comment
Question by:Jeff S
  • 2
4 Comments
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 35098124
so which date (on which table) determines the latest information?
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 35098196
use this

SELECT PatientProfileId, max(AppDate) MostRecentDate
FROM cusMMElig
group by PatientProfileId

then if you join this with original table

create view v_cusMMElig as
select * from cusMMElig c inner join (
SELECT PatientProfileId, max(AppDate) MostRecentDate
FROM cusMMElig
group by PatientProfileId
) m on c.PatientProfileId=m.PatientProfileId and c.AppDate=m.MostRecentDate

this view will give you most recent records per PatientProfileId. then use this view instead of table on your original post...
0
 
LVL 7

Author Comment

by:Jeff S
ID: 35098230
LowFatSpread -

Love the name btw. LastModifiedDate on cusMMElig
0
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 500 total points
ID: 35098398
so add this subquery...

you may need to extend the correlation data if profileid isn't the correct level of "Key" columns
SET NOCOUNT ON

SELECT PatientName = CASE WHEN RTRIM(pp.Last + ' ' + ISNULL(pp.Suffix, '')) <> ''
                           THEN RTRIM(RTRIM(pp.Last + ' ' + ISNULL(pp.Suffix, ''))
                                      + ', ' + ISNULL(pp.First, '') + ' '
                                      + ISNULL(pp.Middle, ''))
                           ELSE RTRIM(ISNULL(pp.First, '') + ' '
                                      + ISNULL(pp.Middle, ''))
                      END,
        pp.PatientProfileId,
        pp.PatientId,
        pp.Address1,
        pp.Address2,
        pp.Suffix,
        pp.City,
        pp.State,
        pp.Zip,
        pp.Phone1,
        pp.Phone1Type,
        pp.Phone2,
        pp.Phone2Type,
        pp.EMailAddress,
        EligName = CASE WHEN RTRIM(E.ELP_Last + ' ' + ISNULL(E.ELP_Suffix, '')) <> ''
                        THEN RTRIM(RTRIM(E.ELP_Last + ' '
                                         + ISNULL(E.ELP_Suffix, '')) + ', '
                                   + ISNULL(E.ELP_First, '') + ' '
                                   + ISNULL(E.ELP_Middle, ''))
                        ELSE RTRIM(ISNULL(E.ELP_First, '') + ' '
                                   + ISNULL(E.ELP_Middle, ''))
                   END,
        E.ELP_Address1,
        E.ELP_Address2,
        E.ELP_City,
        E.ELP_State,
        E.ELP_Zip,
        E.ELP_Phone1,
        E.ELP_Phone1Type,
        E.ELP_Phone2,
        E.ELP_Phone2Type,
        E.ELP_SSN,
        E.ELP_Birthdate,
        E.AppDte,
        E.EligDecisionDte,
        E.EligPerStart,
        E.EligPerEnd,
        E.IncRptLastDte,
        E.IncRptDueDte,
        crml2.Description AS IncRptRcvd,
        E.RILstDte,
        E.RIAnnDueDte,
        crml3.Description AS RIRcvd,
        E.RIClrdDte,
        E.IncRptClrdDte,
        crml4.Description AS AppStatus,
        crml1.Description AS Decision,
        wrkr.Description AS Worker,
        aid.Description AS AidType
FROM    dbo.cusMMElig E
        INNER JOIN dbo.PatientProfile pp ON E.PatientProfileID = pp.PatientProfileId
        LEFT OUTER JOIN dbo.cusCRIMedLists aid ON E.EligTCMSAidTypeMId = aid.MedListsId
        LEFT OUTER JOIN dbo.cusCRIMedLists crml1 ON E.EligDecisionMId = crml1.MedListsId
        LEFT OUTER JOIN dbo.cusCRIMedLists crml4 ON E.EligAppStatMId = crml4.MedListsId
        LEFT OUTER JOIN dbo.cusCRIMedLists wrkr ON E.EligAssignedWrkMId = wrkr.MedListsId
        LEFT OUTER JOIN dbo.cusCRIMedLists crml3 ON E.EligRIRcvdMId = crml3.MedListsId
        LEFT OUTER JOIN dbo.cusCRIMedLists crml2 ON E.EligIncRptRcvdMId = crml2.MedListsId
WHERE   

	crml1.Description LIKE 'Pend%'
        AND crml4.Description NOT IN ( 'Discontinued' )
        AND ( E.EligDecisionDte IS NULL
              OR E.EligDecisionDte >= ISNULL(NULL, '1/1/1900')
              AND E.EligDecisionDte < DATEADD(d, 1, ISNULL(NULL, '1/1/3000'))
            )
            and e.LastModifiedDate =(select max(LastModifiedDate) from dbo.cusMMElig as x
                                     where E.PatientProfileID=x.PatientProfileID)

Open in new window

0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

832 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