I have a query in which I am getting different information about encounters from two different queries, a third query will be added.
For each one of those queries I create a table for the records of that query to be inserted into, and at the end I create a sort of Master Answer Table, where I want to insert all records from the different tables into it.
It is possible that a person may not have a result being returned for all queries.
I think I am messing up my join
in the final part of the query as I am getting TWO results, which I know is impossible. I am using SQL-Server 2008.
Here is what I have written so far:
-- VARIABLE DECLARATION AND INITIALIZATION
SET ANSI_NULLS OFF
GO
DECLARE @SD DATETIME
DECLARE @ED DATETIME
-- THESE ARE PATIENT ADMIT DATES
SET @SD = '2013-01-01'
SET @ED = '2013-01-02'
-- @T1 ###############################################################
-- TABLE DECLARATION WHERE ALL RESULTS WILL GET DEPOSITED OF THE FIRST
-- QUERY WILL GET DEPOSITED. THIS TALBE WILL GET USED IN CONJUNCTION
-- WITH TWO OTHER TABLES IN ORDER TO COMPUTE THE FINAL
DECLARE @T1 TABLE (
ENCOUNTER_ID VARCHAR(200)
, MRN VARCHAR(200)
, [PT AGE] VARCHAR(200)
, [PT NAME] VARCHAR(500)
, [DAYS STAY] VARCHAR(200)
, [LACE DAYS SCORE] VARCHAR(100)
, [ACUTE ADMIT SCORE] VARCHAR(100)
)
--#####################################################################
-- @T1 RECORD INSERTIONS ##############################################
INSERT INTO @T1
SELECT
A.PT_NO
, A.MED_REC_NO
, A.PT_AGE
, A.PT_NAME
, A.DAYS_STAY
, A.LACE_DAYS_SCORE
, A.ACUTE_ADMIT_LACE_SCORE
--#####################################################################
-- DAYS STAY, ACUTE ADMIT AND RELATED SCORING -------------------------
FROM
(SELECT PT_NO
, Med_Rec_No
, Pt_Age
, Pt_Name
, Days_Stay
, CASE
WHEN Days_Stay < 1 THEN 0
WHEN Days_Stay = 1 THEN 1
WHEN Days_Stay = 2 THEN 2
WHEN Days_Stay = 3 THEN 3
WHEN Days_Stay BETWEEN 4 AND 6 THEN 4
WHEN Days_Stay BETWEEN 7 AND 13 THEN 5
WHEN Days_Stay >= 14 THEN 6
END AS LACE_DAYS_SCORE
, CASE
WHEN PLM_PT_ACCT_TYPE = 'I' THEN 3
END AS ACUTE_ADMIT_LACE_SCORE
FROM SMSDSS.BMH_PLM_PTACCT_V
WHERE DSCH_DATE BETWEEN @SD AND @ED
)A
--/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/--
-- @T2 ###############################################################
-- TABLE DECLARATION WHERE THE ER PORTION OF LACE SCORE FOR PATIENT
-- WILL GO.
DECLARE @T2 TABLE (
[MRN T2] VARCHAR(100)
, [PT NO] VARCHAR(20)
, [ER VISITS] VARCHAR (200)
, [ER VISITS LACE SCORE] VARCHAR(100)
)
--#####################################################################
-- @T2 RECORD INSERTIONS ##############################################
INSERT INTO @T2
SELECT B.MED_REC_NO
, B.Pt_No
, B.COUNT_MRN
, B.ER_LACE_SCORE
--#####################################################################
FROM
(
SELECT DISTINCT Med_Rec_No
, Pt_No
, COUNT(Pt_No) AS COUNT_MRN
, CASE
WHEN COUNT(MED_REC_NO) = 0 THEN 0
WHEN COUNT(MED_REC_NO) = 1 THEN 1
WHEN COUNT(MED_REC_NO) = 2 THEN 2
WHEN COUNT(MED_REC_NO) = 3 THEN 3
WHEN COUNT(MED_REC_NO) >= 4 THEN 4
END AS ER_LACE_SCORE
FROM SMSDSS.BMH_PLM_PTACCT_V
WHERE ADM_DATE >= '2013-01-01'
AND (
PLM_PT_ACCT_TYPE = 'I'
AND ADM_SOURCE NOT IN
('RA',
'RP'
)
)
OR PLM_PT_ACCT_TYPE = 'E'
GROUP BY MED_REC_NO, Pt_No
)B
--/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/--
-- FINAL TABLE DECLARATION WHERE ALL INTERMEDIATE RESULTS GET DEPOSITED
DECLARE @LT TABLE (
ENCOUNTER_ID VARCHAR(200)
, MRN VARCHAR(200)
, AGE VARCHAR(30)
, NAME VARCHAR(500)
, LACE_DAYS_SCORE VARCHAR(100)
, LACE_ACUTE_ADM VARCHAR(100)
-- , LACE_COMORBID_SCORE VARCHAR(100)
, LACE_ER_VISITS VARCHAR(100)
)
-- ####################################################################
INSERT INTO @LT
SELECT
Q1.ENCOUNTER_ID
, Q1.MRN
, Q1.[PT AGE]
, Q1.[PT NAME]
, Q1.[LACE DAYS SCORE]
, Q1.[ACUTE ADMIT SCORE]
, Q1.[ER VISITS LACE SCORE]
FROM
(
SELECT
DISTINCT T1.ENCOUNTER_ID
, T1.MRN
, T1.[PT AGE]
, T1.[PT NAME]
, T1.[LACE DAYS SCORE]
, T1.[ACUTE ADMIT SCORE]
, T2.[ER VISITS LACE SCORE]
FROM @T1 T1
JOIN @T2 T2
ON T1.ENCOUNTER_ID = T2.[PT NO]
)Q1
--#####################################################################
SELECT *
FROM @LT
I have updated the code at @T2 to join on the encounter number since any Med_rec_no can have a theoretically infinite amount of encounter numbers. The Med_rec_no therefore can show up multiple times in the original dbo table and this is not a problem. The @T2 table is trying to count how many times a Med_rec_no has been to a certain place in the last 6 months, so it is dynamic, meaning if I come in today, then how many times have I been in the last six months including today.
I have made the code adjustments based on commetns that lead me in a better direction from the two who have commented on this post.