我正在处理一个对表单进行审计的查询。有几页值得审核的问题。填写表格后,答案将按以下方式存储在两个表中:
Table 1: smsmir.obsv OBS
EPISODE NO | FORM USAGE | QUEST | ANSWER | ...
123456789 | ADMISSION | QUESTION 1 | YES | ...
123456789 | ADMISSION | QUESTION 2 | 150 | ...
...
Table 2: smsdss.QOC_vst_summ QOC
EPISODE NO | HT IND | WT IND | ADV DIR | ...
123456789 | 1 | 1 | 0 | ...
...
表 1:smsmir.obsv OBS
将信息存储在一个向量中,因此对于每个问题都有另一行。表 2:smsdss.QOC_vst_summ QOC
将答案存储在一行中,因此每次访问只有一行。表 3 相同,每个访问 id 只有一行。
我的查询从收集VISIT IDS
存储在表中的数据开始,然后传递到下一组以回答一些问题。我从另一个表中提取访问 ID 的原因是因为这是存储访问开始和结束日期的位置。该表看起来像这样:
Table 3: smsdss.BMH_PLM_PtAcct_V PAV
EPISODE NO | ADM DATE | ...
123456789 | 2013-08-01 | ...
...
我得到的我想要的输出是以下内容:
EPISODE NO | QUESTION 1 | QUESTION 2 | HT IND | WT IND | ADV DIR | ...
123456789 | 1 | 1 | 1 | 1 | 0 | ...
...
在上表中,1 表示问题已使用案例语句回答,0 表示未回答。查询已被重写,现在正在产生正确的结果,但速度非常慢,要取回 40 条记录需要 53 分 36 秒。由于查询当前未完成,仅返回 7 列,因此我必须将其扩展为总共 65 列。
我有子查询的原因是答案存储在一个向量中,每一行都是一个问题和答案,但是因为我想在列中显示答案和问题,所以我做了一个子查询。有没有更好的方法来加快速度?
这是查询:
-- THIS QUERY WILL PERFORM AN AUDIT OF THE ADMISSION ASSESSMENT AND
-- OTHER REQUIRED QUESTIONS BY NURSING INFORMATICS
-----------------------------------------------------------------------
-- VARIABLE DECLARATION AND INITIALIZATION. BY DECLARING A START AND
-- END DATE A USER CAN SIMPLY CHANGE THOSE PARAMETERS AND AUDIT ALL
-- INPATIENT ADMISSION ASSESSMENTS FOR THAT TIME PERIOD
DECLARE @SD DATETIME
DECLARE @ED DATETIME
SET @SD = '2013-08-01'
SET @ED = '2013-08-01'
-- QUERY 1
-- THIS QUERY CREATES A TABLE THAT WILL HOUSE ALL VISIT ID NUMBERS THAT
-- ARE GOING TO BE INCLUDED INSIDE OF THE ADMISSION ASSESSMENT AUDIT
-- TABLE DECLARATION ##################################################
DECLARE @T1 TABLE (
VISIT_ID VARCHAR(20))
-- ####################################################################
-- THESE ARE THE ITEMS THAT ARE GOING TO BE INSERTED INTO THE TABLE
INSERT INTO @T1
-- COLUMN SELECTION
SELECT A.PtNo_Num
-- DB(S) USED
FROM (SELECT DISTINCT PTNO_NUM
FROM smsdss.BMH_PLM_PtAcct_V
WHERE Adm_Date BETWEEN @SD AND @ED
AND Plm_Pt_Acct_Type = 'I') A
--+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
--+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
-----------------------------------------------------------------------
-- QUERY TWO. THIS QUERY WILL TAKE THE VISIT ID'S FROM QUERY 1 AND RUN
-- THEM THROUGH A SET OF RULES TO DECIDE WHEATHER OR NOT THE ADDMISISON
-- ASSESSMENT WAS PROPERLY DONE
-----------------------------------------------------------------------
-- COLUMN SELECTION
SELECT DISTINCT OBS.episode_no AS [VISIT ID]
-- CASE STATEMENT, IF PREFERRED LANGUAGE IS NOT 'NULL' THEN CONSIDER
-- THIS COMPLETE AND SCORE 1 ELSE CONSIDER INCOMPLETE AND SCORE 0
,
CASE
WHEN QOC.prim_lng IS NOT NULL THEN 1
ELSE 0
END AS [PREF LANG COMPLETE?],
QOC.ht_chtd_ind AS [HT IND],
QOC.wt_chtd_ind AS [WT IND],
QOC.adv_dir_ind AS [ADV DIRECTIVE]
-- A SEPERATE SELECT STATEMENT IS USED HERE BECAUSE RESULTS OF THE
-- ADMISSION CONSENT ARE STORED IN A VECTOR, SO IT IS NECESSARY TO
-- MAKE A SELECTION FROM THAT LIST, HERE A VALUE OF 1 = YES AND
-- 0 = NO
,
CASE
WHEN OBS.episode_no NOT IN (SELECT episode_no
FROM smsmir.obsv
WHERE form_usage = 'Admission') THEN 0
ELSE 1
END AS [ADMIT ASSESSMENT DONE],
CASE
WHEN OBS.episode_no NOT IN (SELECT episode_no
FROM smsmir.obsv
WHERE form_usage = 'Admission'
AND obsv_cd_ext_name = 'Admission consent signed:') THEN 0
ELSE 1
END AS [ADMIT CONSENT SIGNED?]
-- DB(S) USED ---------------------------------------------------------
FROM smsmir.obsv OBS
JOIN smsdss.QOC_vst_summ QOC
ON OBS.episode_no = QOC.episode_no
JOIN @T1 T1
ON OBS.episode_no = T1.VISIT_ID
-- FILTERS ------------------------------------------------------------
WHERE T1.VISIT_ID = OBS.episode_no
GROUP BY OBS.episode_no,
QOC.prim_lng,
QOC.ht_chtd_ind,
QOC.wt_chtd_ind,
QOC.adv_dir_ind,
OBS.obsv_cd_ext_name
--#####################################################################
-- END REPORT ...[]...[]...[]
你会注意到我使用了一个NOT IN
子句,原因是如果一个问题没有被问或回答,就没有记录,甚至没有 a NULL
,所以如果我不使用它,人可以完成所有其他事情但是如果不是那个特定的项目,那么它们将被排除在最终结果集中。
如果我需要澄清,请告诉我。
** QUERY 实际执行计划 XML ** 查询 exec 实际 xml
谢谢