多部分查询中的空结果集

时间:2013-06-17 14:15:45

标签: sql sql-server-2008

我有一个查询,其中我从两个不同的查询中获取有关遇到的不同信息,将添加第三个查询。

对于这些查询中的每一个,我都会为要插入的查询的记录创建一个表,最后我创建了一种主答案表,我想将不同表中的所有记录插入其中。

某人可能没有为所有查询返回结果。

我认为我在查询的最后部分弄乱了join因为我得到了两个结果,我知道这是不可能的。我正在使用SQL-Server 2008。

这是我到目前为止所写的内容:

-- 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

我更新了@T2上的代码以加入遭遇号码,因为任何Med_rec_no都可以拥有理论上无限量的遭遇号码。因此,Med_rec_no可以在原始dbo表中多次出现,这不是问题。 @T2表试图计算Med_rec_no在过去6个月中到达某个地方的次数,因此它是动态的,这意味着如果我今天进来,那么我在过去六个月里有多少次,包括今天。

我已经基于commetns进行了代码调整,这使得我对这篇帖子发表评论的两位人员有了更好的指导。

1 个答案:

答案 0 :(得分:2)

我很确定你什么时候做

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

可以有多个行具有相同的Med_Rec_No