Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
werhardt
Constraint Violating Yak Guru
270 Posts |
Posted - 2008-03-10 : 09:32:53
|
| Hi,Not sure if you could help or not, but I need to pull the most recent effective date for this report I am trying to run, but I am getting know where. If someone can take a look at this, it would be great.Example….pcs number 00004344 effective dates 5/1/2006 and 5/1/2007. I need it to be the most recent effective date which would be, 5/1/2007 date. Can someone help me?USE [Impact_PROD]GO/****** Object: StoredProcedure [dbo].[p_PrepareMalPracticeReportDataBYCPTCODES] Script Date: 03/10/2008 09:18:56 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOALTER PROCEDURE [dbo].[p_PrepareMalPracticeReportDataBYCPTCODES] ASBEGIN SET NOCOUNT ON DECLARE @STARTTIME DATETIME, @ENDTIME DATETIMESET @STARTTIME = GetDate()PRINT 'SP started on: ' + CAST(@StartTIME as varchar)PRINT ''DECLARE @PKey varchar(16), @pcsi_id1 varchar(8), @pcsi_id2 varchar(4) /**@pcsi_id3 varchar(4),@Lpcsi_id3 varchar(4)**/DECLARE @LplID varchar(12), @LTrm Datetime, @Eff Datetime, @Trm Datetime, @Gap int, @Corrected bit DECLARE @CTrm DATETIME, @i varchar(8), @LastID varchar(8), @LEff Datetime, @FinalEff DATETIMESET @i = 0IF OBJECT_ID('tempdb..#pcsiData') IS NOT NULL DROP TABLE #pcsiDataIF OBJECT_ID('tempdb..#HoldKey') IS NOT NULL DROP TABLE #HoldKeyIF OBJECT_ID('tempdb..#HoldKey2') IS NOT NULL DROP TABLE #HoldKey2SET DATEFORMAT mdy;SELECT pcsi_id1 + pcsi_id2 AS pcsi_pkey, pcsi_id1, pcsi_id2, pcsi_eff1, pcsi_trm1INTO #pcsiData FROM pcsi pWHERE (SELECT COUNT(pcsi_id1 + pcsi_id2) FROM pcsi WHERE pcsi_id1 = p.pcsi_id1) > 1 --AND p.pcsi_prd = 'dgh'ORDER BY p.pcsi_id1 + p.pcsi_id2 ASC, p.pcsi_eff1 ASC--SET TRM DATES TO NULL WHERE DATE IS 1977-03-23 00:00:00.000 --(IMPACT XSQL process uses that date in place of null!)UPDATE #pcsiDataSET pcsi_trm1 = nullWHERE pcsi_trm1 = '1977-03-23 00:00:00.000'SELECT pcsi_id1 + pcsi_id2 as Pkey, (COUNT(pcsi_id1 + pcsi_id2)) AS DupCount --pcsi_eff1, pcsi_trm1, COUNT(pcsi_id1 + pcsi_id2) AS DupCountINTO #holdkeyFROM #pcsiDataGROUP BY pcsi_id1 + pcsi_id2HAVING count(*) = 1IF EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[dbo].[NonDuppcsiDataForMalPracticeReport]') AND OBJECTPROPERTY(id, N'IsTable') = 1)DROP TABLE NonDuppcsiDataForMalPracticeReportSELECT pcsi_id1, pcsi_id2, pcsi_eff1, pcsi_trm1 INTO NonDuppcsiDataForMalPracticeReportFROM #pcsiDataWHERE pcsi_id1 + pcsi_id2 IN(SELECT pkey from #Holdkey) DELETE FROM #pcsiDataWHERE pcsi_id1 + pcsi_id2 IN (SELECT pkey from #HoldKey)DROP TABLE #HoldKeySELECT pcsi_id1, pcsi_id2, pcsi_eff1, pcsi_trm1, count(*) as NoofDup INTO #HoldKey2FROM #pcsiDataGROUP BY pcsi_id1, pcsi_id2, pcsi_eff1, pcsi_trm1HAVING count(*) > 1SET NOCOUNT OFFDELETE #pcsiData FROM #pcsiData, #holdkey2WHERE #pcsiData.pcsi_id1 = #holdkey2.pcsi_id1AND #pcsiData.pcsi_id2 = #holdkey2.pcsi_id2 drop table #holdkey2SET NOCOUNT ONIF EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[dbo].[pcsiDataForMalPracticeReport]') AND OBJECTPROPERTY(id, N'IsTable') = 1)DROP TABLE pcsiDataForMalPracticeReport--CREATE TABLE pcsiDataForMalPracticeReport (pcsi_pkey varchar(16) PRIMARY KEY, pcsi_id1 varchar(8), pcsi_id2 varchar(4), pcsi_id3 varchar(4), pcsi_eff1 varchar(8), pcsi_trm1 varchar(8), Corrected bit)SELECT pcsi_id1 + pcsi_id2 as pkey, pcsi_id1, pcsi_id2, pcsi_eff1, pcsi_trm1INTO pcsiDataForMalPracticeReportFROM #pcsidata pWHERE pcsi_eff1 = (SELECT MIN(pcsi_eff1) FROM #pcsidata WHERE pcsi_id1 = p.pcsi_id1 AND pcsi_id2 = p.pcsi_id2)DECLARE cur CURSOR FAST_FORWARD FORSELECT pcsi_pkey, pcsi_id1, pcsi_id2, pcsi_eff1, pcsi_trm1FROM #pcsiData--group by pcsi_pkeyOrder By pcsi_id1 + pcsi_id2, pcsi_eff1 ASCOPEN CurFETCH NEXT FROM cur INTO @pkey, @pcsi_id1, @pcsi_id2, @eff, @trmSET @lplID = @pcsi_id1 + @pcsi_id2SET @LEff = @EffSET @Ltrm = @TrmFETCH NEXT FROM cur INTO @pkey, @pcsi_id1, @pcsi_id2, @eff, @trmSET @i = 2DELETE FROM tmppcsiDatesWithGaps --Clear table used for debuggingWHILE @@FETCH_STATUS = 0BEGIN --Begin While LoopIF @pcsi_id1 + @pcsi_id2 = @lplIDBEGIN --If current record is for the same provider location as the last then...SET @Gap = DATEDIFF(day, @Ltrm, @Eff)IF @Gap > 2BEGIN --If there is a gap greater than 1 day...PRINT ''PRINT 'GAP between fetch ' + str(@i - 1) + ' and ' + str(@i) + ' (' + @pcsi_id1 + ' ' + @pcsi_id2 + ' ' + '): ' + str(@gap) + ' days! 'PRINT 'Last Trm: ' + CAST(@LTrm AS varchar) + ' Eff: ' + CAST(@eff AS Varchar)PRINT ''--IF EXISTS (SELECT * FROM pcsiDataForMalPracticeReport WHERE pcsi_id1 = @pcsi_id1 AND pcsi_id2 = @pcsi_id2)--IF @pcsi_id1 + @pcsi_id2 NOT IN (SELECT pcsi_id1 + pcsi_id2 FROM tmppcsiDatesWithGaps)--BEGIN --Begin if effective date was not already updated--IF @Leff > @EffUPDATE pcsiDataForMalPracticeReportSET pcsi_eff1 = @Eff-- pcsi_Ltrm = @LTrmWHERE pcsi_id1 = @pcsi_id1 AND pcsi_id2 = @pcsi_id2--ELSE--UPDATE pcsiDataForMalPracticeReport--SET pcsi_id3 = @lpcsi_id3, pcsi_eff1 = @LEff-- pcsi_Ltrm = @LTrm--WHERE pcsi_id1 = @pcsi_id1 AND pcsi_id2 = @pcsi_id2--ELSE--INSERT INTO pcsiDataForMalPracticeReport (pcsi_pkey, pcsi_id1, pcsi_id2, pcsi_id3, pcsi_eff1, pcsi_trm1)--VALUES (@pcsi_id1 + @pcsi_id2 + @pcsi_id3, @pcsi_id1, @pcsi_id2, @lpcsi_id3, @LEff, @Ltrm)INSERT INTO tmppcsiDatesWithGaps (pcsi_id1, pcsi_id2, lpcsiid, EffectiveDate) VALUES (@pcsi_id1, @pcsi_id2, @lplid, @Eff)--END --End if effective date was not already updatedEND --End if there is a gap greater than 1 dayEND --End if the provider location is different than the last row--Set current rows data in last rows variables...SET @lplID = @pcsi_id1 + @pcsi_id2SET @LEff = @EffSET @Ltrm = @Trm--Get next row of dataFETCH NEXT FROM cur INTO @pkey, @pcsi_id1, @pcsi_id2, @eff, @trmSET @i = @i + 1 --increment iPRINT 'Iteration #' + str(@i) + ' -- ' + @pkey + ' Eff: ' + Cast(@Eff as varchar) + ' ' + ' Trm: ' + cast(@trm as varchar)END --End While LoopINSERT INTO pcsiDataForMalPracticeReportSELECT distinct pcsi_id1 + pcsi_id2, pcsi_id1, pcsi_id2, pcsi_eff1, pcsi_trm1FROM NonDuppcsiDataForMalPracticeReport/***UPDATE #pcsidataSET pcsi_trm1 = '20470101'WHERE pcsi_trm1 is null OR pcsi_trm1 = ''/**SELECT p.pcsi_id1, p.pcsi_id2, MAX(p.pcsi_trm1) INTO #HoldKey2FROM #pcsidata pgroup by p.pcsi_id1, p.pcsi_id2ORDER BY p.pcsi_id1UPDATE R SET pcsi_trm1 = I.pcsi_trm1 FROM pcsiDataForMalPracticeReport R INNER JOIN #HoldKey I ON r.pcsi_id1 = I.pcsi_id1AND r.pcsi_id2 = I.pcsi_id2**/Print ''Print ''Print ''Print 'SETTING MAX TERM VALUES NOW....(This may take a while)'Print ''Print ''UPDATE pcsiDataForMalPracticeReportset pcsi_trm1 = jp.MaxTrmFROM pcsi p JOIN (SELECT pcsi_id1, pcsi_id2, MAX(pcsi_trm1) as maxtrm FROM pcsi p2--WHERE p2.pcsi_id1 + p2.pcsi_id2 = p.pcsi_id1 + p.pcsi_id2GROUP BY p2.pcsi_id1, p2.pcsi_id2) jp ON (jp.pcsi_id1 + jp.pcsi_id2 = p.pcsi_id1 + p.pcsi_id2)DECLARE @NotTermed intSET @NotTermed = (SELECT COUNT(*) FROM pcsiDataForMalPracticeReport WHERE pcsi_trm1 = '20470101')PRINT''PRINT 'Total non-duplicate records not termed: ' + str(@NotTermed)--UPDATE pcsiDataForMalPracticeReport--SET pcsi_trm1 = NULL --WHERE pcsi_trm1 = '20470101'**/UPDATE pcsiDataForMalPracticeReportSET pcsi_trm1 = NULLPRINT ''PRINT 'STEP TWO.......................'PRINT 'Preparing the table names...tmpMalPracticeEffectiveDates'PRINT''--This step updates tmpMalPracticeEffectiveDates with the desired effective date and most recent termination date--if there are no current records with a termination date = NULLTRUNCATE TABLE tmpMalPracticeEffectiveDatesSET NOCOUNT OFFPRINT 'Inserting new data into tmpMalPracticeEffectiveDates'INSERT INTO tmpMalPracticeEffectiveDates (pcsi_id1, pcsi_id2, pcsi_eff1) SELECT DISTINCT pcsi_id1, pcsi_id2, pcsi_eff1FROM pcsi pWHERE p.pcsi_eff1 = (SELECT MIN(pcsi_eff1) FROM pcsi p2 WHERE p2.pcsi_id1 = p.pcsi_id1 AND p2.pcsi_id2 = p.pcsi_id2)ORDER BY pcsi_id1, pcsi_id2--------------------------------Set temp bogus date to distinguis which records are current in--subsequent statementPRINT 'Setting bogus date to distinguish pcsi records that are not termed'UPDATE tmpMalPracticeEffectiveDatesSET tmpMalPracticeEffectiveDates.pcsi_trm1 = '12/21/2049'WHERE '03/23/1977' IN (SELECT pcsi_trm1 FROM pcsi p WHERE p.pcsi_id1 = tmpMalPracticeEffectiveDates.pcsi_id1 AND p.pcsi_id2 = tmpMalPracticeEffectiveDates.pcsi_id2)-------------------------------PRINT 'Setting most recent term date for pcsi records that are not currently active'UPDATE tmpMalPracticeEffectiveDatesSET tmpMalPracticeEffectiveDates.pcsi_trm1 = (SELECT MAX(pcsi_trm1) FROM pcsi p WHERE p.pcsi_id1 = tmpMalPracticeEffectiveDates.pcsi_id1 AND p.pcsi_id2 = tmpMalPracticeEffectiveDates.pcsi_id2)WHERE tmpMalPracticeEffectiveDates.pcsi_trm1 is NULL-------------------------------PRINT 'Setting bogus dates back to NULL'UPDATE tmpMalPracticeEffectiveDatesSET tmpMalPracticeEffectiveDates.pcsi_trm1 = NULLWHERE pcsi_trm1 = '12/21/2049'---------------------------------CORRECT EFFECTIVE DATES WITH GAPS...PRINT 'Correcting Effective Dates for those records with gaps in credentialing records'UPDATE tmpMalPracticeEffectiveDatesSET tmpMalPracticeEffectiveDates.pcsi_eff1 = t.EffectiveDateFROM tmppcsiDatesWithGaps tWHERE tmpMalPracticeEffectiveDates.pcsi_id1 = t.pcsi_id1AND tmpMalPracticeEffectiveDates.pcsi_id2 = t.pcsi_id2----END OF SP---DECLARE @Diff decimalSET @ENDTIME = getdate()PRINT ''PRINT ''DECLARE @GapCount intSET @GapCount = (SELECT COUNT(*) FROM tmppcsiDatesWithGaps)PRINT 'Total number of non-distinct provider locations: ' + Str(@i) + '.'PRINT 'Total number of gaps found: ' + Str(@GapCount) + '.'PRINT 'FINISHED ON: ' + cast(@ENDTIME as varchar)SET @Diff = CAST(DATEDIFF(second, @StartTime, @EndTIME) AS varchar)PRINT ''PRINT 'Time elapsed: ' + str(@Diff) + ' seconds.'PRINT ' = ' + str(@Diff/60) + ' Minutes!'END |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-03-10 : 09:50:21
|
| just take it like this:-SELECT [pcs number],MAX(EfectiveDate) AS EffDateFROM TableGROUP BY [pcs number]this wil give latest effective date for each pcs numberto use this in queries useSELECTFROM ....other objectsINNER JOIN (Above Query)qON q.[pcs number]= corresponding field from other table |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-03-10 : 09:51:36
|
Wow! A complete novel to read.Begin withSELECT PcsNumber, MAX(EffectiveDate) FROM Table1 GROUP BY PcsNumber E 12°55'05.25"N 56°04'39.16" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-03-10 : 09:53:47
|
quote: Originally posted by visakh16 SELECTFROM ....other objectsINNER JOIN (Above Query)qON q.[pcs number]= corresponding field from other table
Depending on business ruleSELECTFROM ....other objectsINNER JOIN (Above Query)qON q.[pcs number] = {corresponding field from other table} AND q.EffDate = {corresponding field from other table}or simplySELECTFROM ....other objectsINNER JOIN (Above Query)qON q.EffDate = {corresponding field from other table} E 12°55'05.25"N 56°04'39.16" |
 |
|
|
werhardt
Constraint Violating Yak Guru
270 Posts |
Posted - 2008-03-10 : 10:09:00
|
| Thanks for all of your help! |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-03-10 : 10:12:59
|
Also, there are so many flaws in your code!For example, you store pcsi_id1 + pcsi_id2 in a staging table and then delete.But what if pcsi_id1 + pcsi_id2 is 'abcde'?Then all keys'a' + 'bcde''ab' + 'cde''abc' + 'de''abcd' + 'e'will also fulfill the equal operation!Are you using SQL Server 2000 or SQL Server 2005? E 12°55'05.25"N 56°04'39.16" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-03-10 : 10:28:05
|
The complete first part down to CURSOR can be rewritten asALTER PROCEDURE dbo.p_PrepareMalPracticeReportDataBYCPTCODESASSET NOCOUNT ONDECLARE @STARTTIME DATETIME, @ENDTIME DATETIMESET @STARTTIME = GETDATE()PRINT 'SP started on: ' + CONVERT(VARCHAR(30), @StartTIME, 120)PRINT ''DECLARE @PKey varchar(16), @pcsi_id1 varchar(8), @pcsi_id2 varchar(4), @LplID varchar(12), @LTrm Datetime, @Eff Datetime, @Trm Datetime, @Gap int, @Corrected bit, @CTrm DATETIME, @i varchar(8), @LastID varchar(8), @LEff Datetime @FinalEff DATETIMESELECT pcsi_id1, pcsi_id2, pcsi_eff1, NULLIF(pcsi_trm1, '19770323')INTO #pcsiDataFROM pcsi as pINNER JOIN ( SELECT pcsi_id1, COUNT(*) FROM pcsi group by pcsi_id1 having count(*) > 1 ) AS x ON x.pcsi_id1 = p.pcsi_id1SELECT pcsi_id1, pcsi_id2INTO #holdkeyFROM #pcsiDataGROUP BY pcsi_id1, pcsi_id2HAVING COUNT(*) = 1IF EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[dbo].[NonDuppcsiDataForMalPracticeReport]') AND OBJECTPROPERTY(id, N'IsTable') = 1) DROP TABLE NonDuppcsiDataForMalPracticeReportSELECT p.pcsi_id1, p.pcsi_id2, p.pcsi_eff1, p.pcsi_trm1INTO NonDuppcsiDataForMalPracticeReportFROM #pcsiData AS pWHERE exists (select * from #Holdkey as h where h.pcsi_id1 = p.pcsi_id1 and h.pcsi_id2 = p.pcsi_id2) DELETE yFROM #pcsiData AS yWHERE exists (select * from #HoldKey AS h where h.pcsi_id1 = y.pcsi_id1 and h.pcsi_id2 = y.pcsi_id2)DROP TABLE #HoldKeySELECT pcsi_id1, pcsi_id2, pcsi_eff1, pcsi_trm1, count(*) as NoofDup INTO #HoldKey2FROM #pcsiDataGROUP BY pcsi_id1, pcsi_id2, pcsi_eff1, pcsi_trm1HAVING count(*) > 1SET NOCOUNT OFFDELETE gFROM #pcsiData AS gINNER JOIN #holdkey2 AS h ON h.pcsi_id1 = g.pcsi_id1WHERE g.pcsi_id2 = h.pcsi_id2drop table #holdkey2SET NOCOUNT ONIF EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[dbo].[pcsiDataForMalPracticeReport]') AND OBJECTPROPERTY(id, N'IsTable') = 1)DROP TABLE pcsiDataForMalPracticeReport--CREATE TABLE pcsiDataForMalPracticeReport (pcsi_pkey varchar(16) PRIMARY KEY, pcsi_id1 varchar(8), pcsi_id2 varchar(4), pcsi_id3 varchar(4), pcsi_eff1 varchar(8), pcsi_trm1 varchar(8), Corrected bit)SELECT pcsi_id1, pcsi_id2, pcsi_eff1, pcsi_trm1INTO pcsiDataForMalPracticeReportFROM #pcsidata AS pinner join ( SELECT pcsi_id1, pcsi_id2, min(pcsi_eff1) AS pcsi_eff1 FROM #pcsidata AS p group by pcsi_id1, pcsi_id2 ) as y on y.pcsi_id1 = p.pcsi_id1where p.pcsi_id2 = y.pcsi_id2 and p.pcsi_eff1 = y.pcsi_eff1 And your CURSOR things can be rewritten as set-based. Which is far more efficient. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
werhardt
Constraint Violating Yak Guru
270 Posts |
Posted - 2008-03-10 : 11:03:01
|
| Thanks again for your help |
 |
|
|
|
|
|
|
|