Solved

Help with case statement

Posted on 2008-10-14
20
177 Views
Last Modified: 2012-05-05
I'm sure there are many ways to skin a cat and probably many ways to do what I'm trying to do in the SQL below.

Can someone show me an easier, FASTER, way to do this?
This will be part of a larger SP returning ptEncounter records based on criteria passed to the SP. I am open to using temp tables or variables to store then return the results.

Essentially, I will be providing indicators in the UI based on the values returned in the DictationState column
returned from the stored procedure based on this criteria:

"        Green  An entry in ptDictation for that MRN/DOS with a status of approved.
"        Grey   ptSchedule records linked to that Encounter do contain an OV, but these is no entry in ptDictation
          for that MRN/DOS or if there is an entry where the status is not approved.
"        White  ptSchedule records linked to that Encounter do not contain an OV.

an OV is same as having ptSchedule.ApptTypeID = 5
DOS = DateOfService

I have 2 ptEncounter records that meet the criteria of MRN = '5079' and DateOfService = '07/5/2004'
I have 2 ptSchedule records that meet the criteria of MRN = '5079' and StartDtTm = '07/5/2004'
I have 9 ptDictation records that meet the criteria of MRN = '5079' and DOS = '07/5/2004'
I should only have two values returned.

Schemas below.

select CASE
       WHEN EXISTS (SELECT d.MRN FROM ptDictation d 
                    INNER JOIN ptEncounter ee 
                        ON ee.MRN = d.MRN AND d.DOS = e.DateOfService 
                    WHERE d.Status = 'Approved')          THEN 2
       WHEN EXISTS (SELECT s.MRN FROM ptSchedule s 
                    INNER JOIN ptEncounter eee 
                        ON eee.MRN = s.MRN 
                        AND CONVERT(varchar,s.StartDtTm, 10) = CONVERT(varchar,e.DateofService, 10)
                    WHERE s.ApptTypeID = 5) THEN 1
       ELSE 0
       END as DictationState
FROM ptEncounter e
WHERE e.MRN = '5079' 
AND   e.DateOfService = '07/5/2004'
 
----------------- Schemas -----------------
CREATE TABLE [dbo].[ptEncounter](
        [EncounterID] [int] IDENTITY(1,1) NOT NULL,
        [BillNumber] [int] NOT NULL,
        [ClaimNumber] [nvarchar](20) NULL,
        [LatestRec] [bit] NOT NULL,
        [MRN] [varchar](25) NULL,
        [SubscriberContactID] [int] NULL,
        [LocationID] [int] NULL,
        [AuthID] [int] NULL,
        [BillerUserID] [int] NULL,
        [PostingDtTm] [datetime] NULL,
        [SiteID] [int] NULL,
        [DeptID] [int] NULL,
        [InsuranceID] [int] NULL,
        [RefPhysID] [int] NULL,
        [ProviderID] [int] NULL,
        [DateOfService] [datetime] NULL,
        [DateOfIllness] [datetime] NULL,
        [TotalAmt] [decimal](18, 2) NULL,
        [PaidAmt] [decimal](18, 2) NULL,
        [OpenAmt] [decimal](18, 2) NULL,
        [PaidInFull] [bit] NULL,
        [ClaimSubmissionDtTm] [datetime] NULL,
        [ClaimStatus] [int] NULL,
        [Diag1] [nvarchar](10) NULL,
        [Diag2] [nvarchar](10) NULL,
        [Diag3] [nvarchar](10) NULL,
        [Diag4] [nvarchar](10) NULL,
        [StatementMessage1] [nvarchar](500) NULL,
        [StatementMessage2] [nvarchar](500) NULL,
        [BillComment1] [nvarchar](500) NULL,
        [BillComment2] [nvarchar](500) NULL,
        [AdmitDtTm] [datetime] NULL,
        [DischargeDtTm] [datetime] NULL,
        [AutoAccidentRelated] [bit] NULL,
        [EmploymentAccidentRelated] [bit] NULL,
        [OtherAccidentRelated] [bit] NULL,
        [State] [nvarchar](2) NULL,
        [PreAuthNumber] [nvarchar](50) NULL,
        [CreatedByUserID] [int] NULL,
        [CreatedDtTm] [datetime] NULL,
        [ModifiedByUserID] [int] NULL,
        [ModifiedDtTm] [datetime] NULL,
        [DeletedByUserID] [int] NULL,
        [DeletedDtTm] [datetime] NULL,
        [FirstOnsetDtTm] [datetime] NULL,
        [AcceptAssignment] [bit] NULL,
        [Errors] [varchar](max) NULL,
        [BucketID] [int] NULL,
)
 
CREATE TABLE [dbo].[ptDictation](
        [ID] [int] IDENTITY(1,1) NOT NULL,
        [MRN] [varchar](25) NOT NULL,
        [DOS] [datetime] NULL,
        [DocumentNum] [int] NULL,
        [SectionTitle] [nvarchar](50) NULL,
        [SectionOrder] [decimal](10, 2) NULL,
        [Status] [varchar](50) NULL,
        [Doctor] [varchar](50) NULL,
        [SystemTime] [timestamp] NULL,
        [Text] [varchar](7800) NULL,
        [UserID] [char](10) NULL,
        [Deleted] [int] NULL,
        [DeletedBy] [char](10) NULL,
        [DeletedDate] [datetime] NULL,
        [Signed] [char](10) NULL,
        [Printed] [int] NULL,
        [DictationDoc] [nvarchar](30) NULL,
        [OrgDocNum] [int] NULL,
        [TimeStamp] [datetime] NULL,
        [PrintedDate] [datetime] NULL,
        [Location] [varchar](40) NULL,
)
 
CREATE TABLE [dbo].[ptSchedule](
        [ScheduleId] [int] IDENTITY(1,1) NOT NULL,
        [MRN] [varchar](25) NULL,
        [ApptTypeID] [int] NULL,
        [ResourceId] [int] NULL,
        [StartDtTm] [datetime] NULL,
        [EndDtTm] [datetime] NULL,
        [Description] [nvarchar](1000) NULL,
        [Notes] [nvarchar](100) NULL,
        [Status] [int] NULL,
        [CheckedInDtTm] [datetime] NULL,
        [CheckedOutDtTm] [datetime] NULL,
        [Flow] [nvarchar](50) NULL,
        [ModifiedDtTm] [datetime] NULL,
        [ModifiedByUserID] [int] NULL,
        [WaitList] [int] NULL,
        [ApptBlockID] [int] NULL,
        [temp] [nvarchar](1000) NULL,
        [EncounterID] [int] NULL,
)

Open in new window

0
Comment
Question by:EddieShipman
  • 9
  • 9
20 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 22711640
Does this return 2 records?

SELECT *
FROM ptEncounter e
WHERE e.MRN = '5079'
AND   e.DateOfService = '07/5/2004'

My guess is that it does not.
0
 
LVL 26

Author Comment

by:EddieShipman
ID: 22711671
Yes, it does
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 22711682
How many rows does this return?

select CASE
       WHEN EXISTS (SELECT d.MRN FROM ptDictation d
                    INNER JOIN ptEncounter ee
                        ON ee.MRN = d.MRN AND d.DOS = e.DateOfService
                    WHERE d.Status = 'Approved')          THEN 2
       WHEN EXISTS (SELECT s.MRN FROM ptSchedule s
                    INNER JOIN ptEncounter eee
                        ON eee.MRN = s.MRN
                        AND CONVERT(varchar,s.StartDtTm, 10) = CONVERT(varchar,e.DateofService, 10)
                    WHERE s.ApptTypeID = 5) THEN 1
       ELSE 0
       END as DictationState
FROM ptEncounter e
WHERE e.MRN = '5079'
AND   e.DateOfService = '07/5/2004'

0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 26

Author Comment

by:EddieShipman
ID: 22711822
Two. But, this is going to be part of a larger stored procedure that returns the value for each record in ptEncounter and it takes 6 secs to return all rows.
There are currently 340412 records in ptEncounter and it takes 18 seconds to run only the
SQL above with no WHERE clause.
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 22711888
OK, I follow you now.  There are a few things...first, make sure there are indexes on any field in which you're joining tables or in which you're specifying criteria.  next, see if there is any way you can get aroudn this statement:

AND CONVERT(varchar,s.StartDtTm, 10) = CONVERT(varchar,e.DateofService, 10)
0
 
LVL 26

Author Comment

by:EddieShipman
ID: 22712011
No way to get around that date convert because StartDtTm has the TIME part and DateOfService does not.

On ptEncounter there is an index on MRN but not on DateOfService.
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 22712035
Add the index to see if it helps.  Are there indexes on ptSchedule and ptDictation?
0
 
LVL 26

Author Comment

by:EddieShipman
ID: 22712169
ptDictation, yes one MRN and and one on DOS
   Should I create one on both columns?

ptSchedule, not on either.
   Just created one on both columns
   I also created one on EncounterID, but the data I currently have has no EncounterIDs as it is
   legacy data and was imported incorrectly and We cannot tie the appointments back to the
   particular encounter.

ptEncounter MRN only, not on DateOfService.
   Just created one on both columns
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 22712292
>> Should I create one on both columns?
No

Is it faster?
0
 
LVL 26

Author Comment

by:EddieShipman
ID: 22712329
Haven't tried it yet, will do so now.
0
 
LVL 26

Author Comment

by:EddieShipman
ID: 22712342
19 secs but still unacceptable.
0
 
LVL 60

Accepted Solution

by:
chapmandew earned 500 total points
ID: 22712349
You've done just about all you can do for this query...consider snapshotting the data into a table every few hours and query from the table.
0
 
LVL 26

Author Comment

by:EddieShipman
ID: 22723246
What, no other options?
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 22723269
That is correct.
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 22741687
I am sorry you didn't get the answer that you wanted to hear, but it is the correct answer.  You can't just delete the question like that.
0
 
LVL 26

Author Comment

by:EddieShipman
ID: 22741848
I've gotten other options from SQLServer Central so, apparently, you are not correct.
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 22741882
OK...post them.
0
 
LVL 26

Author Comment

by:EddieShipman
ID: 22795739
GO ahead and leave it open. I am almost ready to post the answer.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQl server restarts itself 6 37
insert query with value having 's 2 51
Is there any Easy way to copy CSV to SQL Server using C# 3 80
Need help debbuging stored procedure 21 41
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…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

772 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