swenri
Yak Posting Veteran
72 Posts |
Posted - 2013-11-21 : 11:04:56
|
I've a query I need to use like statement where I need to look for "BLUserFreeText"."UserFreeText" LIKE '%SMA%' OR "BLUserFreeText"."UserFreeText" LIKE '%SMN%' . But, the problem is the query is taking a long time to run. If I use UNION the query runs faster but, I'm getting duplicates. Can anybody let me know how to make the query to run faster avoid duplicates using like statement.This is urgent and thank you. USE [DatamartDB2]GO/****** Object: StoredProcedure [dbo].[MMC_SP_MMC_SP_OBClinicLabsOrders] Script Date: 03/01/2013 18:49:40 ******/IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[MMC_SP_OBClinicLabsOrders]') AND type in (N'P', N'PC'))DROP PROCEDURE [dbo].[MMC_SP_OBClinicLabsOrders]GO USE [DatamartDB2]GO/****** Object: StoredProcedure [dbo].[MMC_SP_OBClinicLabsOrders] Script Date: 11/12/2013 13:38:09 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE PROC [dbo].[MMC_SP_OBClinicLabsOrders] ASBEGINSET NOCOUNT ONSET TRANSACTION ISOLATION LEVEL READ UNCOMMITTEDPRINT 'FOOD AND NUTRITION DIET CHANGE ORDERS'DECLARE @Location varchar(255)if (ISNULL(@Location,'')='') begin /* Declare Date Parameters */--DECLARE @FromDate datetime--DECLARE @ReAdmitDate datetime--SET @FromDate = (CONVERT (varchar, Getdate()-120, 101) + ' 00:00:01') --SET @ReAdmitDate = (CONVERT (varchar, Getdate()-1, 101) + ' 23:59:59') Declare @FromDate datetimeDeclare @ToDate datetimeDeclare @RptTimeFrameout VARCHAR(50) --/* Hardcode for testing */ --SET @FromDate = '2013/11/14' --SET @ToDate = '2013/11/16'SET @RptTimeFrameout='1' --dailyif @RptTimeFrameout='1' -- DailyBeginselect @FromDate = CONVERT(datetime,ltrim(CONVERT(char(30),DATEADD(d,-1,getdate()),101))+ '00:00:00 AM')select @ToDate = CONVERT(DATETIME,ltrim(CONVERT(char(30),GETDATE() -1,101))+'23:59:59 PM')End SELECT DISTINCT "BLSession_Extended"."sessionid",("BLSession_Extended"."LastName" + ' ' + "BLSession_Extended"."firstname") AS Patient_Name, "BLSession_Extended"."MRN", "MO_Demographics"."AccountNumber", (select top 1 "SVOrdersLog"."DrugName"from "SVOrdersLog" where "BLSession_Extended"."sessionid" = "SVOrdersLog"."sessionid" AND "SVOrdersLog"."DrugName" IN ('Order AFP Maternal Serum','Order Maternal Serum AFP','Maternal AFP4 (Quad) screen for risk of T21, T18, NTD') AND "SVOrdersLog"."DoneSigTime" BETWEEN @FromDate AND @ToDateAND "SVOrdersLog"."itemstatus" = 'Done'AND "SVOrdersLog"."DoneSigTime" IS NOT NULLorder by "SVOrdersLog"."DoneSigTime" ASC) AS OrderAFP, (select top 1 "SVOrdersLog"."DoneSigTime"from "SVOrdersLog" where "BLSession_Extended"."sessionid" = "SVOrdersLog"."sessionid" AND "SVOrdersLog"."DrugName" IN ('Order AFP Maternal Serum','Order Maternal Serum AFP')AND "SVOrdersLog"."DoneSigTime" BETWEEN @FromDate AND @ToDateAND "SVOrdersLog"."itemstatus" = 'Done'AND "SVOrdersLog"."DoneSigTime" IS NOT NULLorder by "SVOrdersLog"."DoneSigTime" ASC) AS OrderAFPTime, (select top 1 "SVOrdersLog"."DrugName"from "SVOrdersLog" where "BLSession_Extended"."sessionid" = "SVOrdersLog"."sessionid"AND "SVOrdersLog"."DrugName"='Order Alfa Fetoprotein Profile Four (AFP4)'AND "SVOrdersLog"."DoneSigTime" BETWEEN @FromDate AND @ToDateAND "SVOrdersLog"."itemstatus" = 'Done'AND "SVOrdersLog"."DoneSigTime" IS NOT NULLorder by "SVOrdersLog"."DoneSigTime" ASC) AS OrderAFP4, (select top 1 "SVOrdersLog"."DrugName"from "SVOrdersLog" where "BLSession_Extended"."sessionid" = "SVOrdersLog"."sessionid"AND "SVOrdersLog"."DrugName" = 'Order CF Carrier Screen (CFCS clinic)'AND "SVOrdersLog"."DoneSigTime" BETWEEN @FromDate AND @ToDateAND "SVOrdersLog"."itemstatus" = 'Done'order by "SVOrdersLog"."DoneSigTime" ASC) AS OrderCFCS, (select top 1 "SVOrdersLog"."DrugName"from "SVOrdersLog" where "BLSession_Extended"."sessionid" = "SVOrdersLog"."sessionid"AND "SVOrdersLog"."DrugName" IN ('Order Fragile X DNA (FRXDNA clinic)','Order Fragile X DNA (FRXDNA hospital)','Order Fragile X DNA (FRXDNA)','Order Fragile-X Screen')AND "SVOrdersLog"."DoneSigTime" BETWEEN @FromDate AND @ToDateAND "SVOrdersLog"."itemstatus" = 'Done'AND "SVOrdersLog"."DoneSigTime" IS NOT NULLorder by "SVOrdersLog"."DoneSigTime" ASC) AS OrderFragileDNA, (select top 1 "SVOrdersLog"."DrugName"from "SVOrdersLog" where "BLSession_Extended"."sessionid" = "SVOrdersLog"."sessionid"AND "SVOrdersLog"."DrugName"='Order Sequential Screen, Part 1'AND "SVOrdersLog"."DoneSigTime" BETWEEN @FromDate AND @ToDateAND "SVOrdersLog"."itemstatus" = 'Done'order by "SVOrdersLog"."DoneSigTime" ASC) AS OrderSeqScreen1, (select top 1 "SVOrdersLog"."DrugName"from "SVOrdersLog" where "BLSession_Extended"."sessionid" = "SVOrdersLog"."sessionid"AND "SVOrdersLog"."DrugName"='Order Sequential Screen, Part 2'AND "SVOrdersLog"."DoneSigTime" BETWEEN @FromDate AND @ToDateAND "SVOrdersLog"."itemstatus" = 'Done'AND "SVOrdersLog"."DoneSigTime" IS NOT NULLorder by "SVOrdersLog"."DoneSigTime" ASC) AS OrderSeqScreen2, ISNULL((select top 1 "BLUserFreeText"."UserFreeText"from "BLUserFreeText" where "BLSession_Extended"."sessionid" = "BLUserFreeText"."sessionid"AND "BLUserFreeText"."UserFreeText" LIKE '%SMN%' OR "BLUserFreeText"."UserFreeText" LIKE '%SMA%' AND "SVOrdersLog"."DoneSigTime" BETWEEN @FromDate AND @ToDateAND "SVOrdersLog"."itemstatus" = 'Done'AND "SVOrdersLog"."itemstatus" IS NOT NULLorder by "SVOrdersLog"."DoneSigTime" DESC),' ')AS OrderSMN,@FromDate AS FromDt,@ToDate AS ToDt FROM (("DatamartDB2"."dbo"."BLSession_Extended" "BLSession_Extended" INNER JOIN "DatamartDB2"."dbo"."SVOrdersLog" "SVOrdersLog" ON "BLSession_Extended"."sessionID"="SVOrdersLog"."sessionID" ) INNER JOIN "DatamartDB2"."dbo"."MO_Demographics" "MO_Demographics" ON "BLSession_Extended"."sessionID"="MO_Demographics"."SessionID"INNER JOIN "BLUserFreeText" ON "BLSession_Extended"."sessionid" = "BLUserFreeText"."sessionid" AND ("BLUserFreeText"."UserFreeText" LIKE '%SMN%') OR ("BLUserFreeText"."UserFreeText" LIKE '%SMA%')) WHERE "BLSession_Extended"."FacilityID"=1AND "SVOrdersLog"."DoneSigTime" BETWEEN @FromDate AND @ToDateAND "SVOrdersLog"."DrugName" IN ('Order Alfa Fetoprotein Profile Four (AFP4)','Order AFP Maternal Serum','Order Maternal Serum AFP','Order CF Carrier Screen (CFCS clinic)','Order Fragile X DNA (FRXDNA)', 'Order Sequential Screen, Part 1', 'Order Sequential Screen, Part 2') AND "SVOrdersLog"."itemstatus" = 'Done'AND "SVOrdersLog"."DoneSigTime" IS NOT NULLAND "BLSession_Extended"."LastName" NOT LIKE ('%Test%')AND "BLSession_Extended"."MRN" NOT LIKE ('%@@Versi%')ENDENDSET NOCOUNT OFFSET ANSI_NULLS OFF GOGOGRANT EXECUTE ON [dbo].[MMC_SP_OBClinicLabsOrders] TO [Public] |
|