Solved

SQL 2000 Query Assistance

Posted on 2011-03-10
4
199 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
[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
4 Comments
 
LVL 50

Expert Comment

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

Expert Comment

by:Huseyin KAHRAMAN
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

Independent Software Vendors: 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

Suggested Solutions

Title # Comments Views Activity
Tempdb Contention - SQL SERVER 10 38
Sql does not recognize null value from Oracle source 8 26
mssql 7 32
sql trace 4 24
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

739 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