空结果由多部分查询集(Empty Result Set from Multi-Part Query)

2019-10-18 06:11发布

我有在我得到大约从两个不同的查询遭遇不同的信息查询,第三个查询将增加。

对于查询中的每一个创建该查询的记录被插入到表中,并在最后我创造一种法师回答表,在这里我想从不同的表中的所有记录插入到它的。

一个人可能没有被返回的结果为所有查询这是可能的。

我想我搞乱了我的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导致我从两人谁对这个帖子的评论更好的方向代码调整。

Answer 1:

我敢肯定,当你做

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



文章来源: Empty Result Set from Multi-Part Query