在同一个主题上有几篇帖子,我已经通读了它们......但尽可能地尝试,我不能将它们应用到我的问题中。我确定这是我忽视的蠢事。
我要做的是向特定日期和时间的病人展示病人。每个患者都有一个入院和出院日期,所以我用这些来确定他们是否在那个特定的日期和床上。时间。如果我列出所有内容,那就很有效。现在我正在尝试做一个摘要,所以它只显示在这个位置的给定日期的3个住院病人和1个门诊病人,等等。但是我在标题中收到错误消息。
SELECT DISTINCT TSM950_STATION.loc_ext_id AS STATION,
convert(varchar(10),DATEADD(DAY,-7,GETDATE()),101) AS CENSUS_DATE,
TSM180_CAT.cod_dtl_ds,
COUNT(TPM300_PAT_VISIT.vst_ext_id)
FROM TPM300_PAT_VISIT INNER JOIN
TSM040_PERSON_HDR ON TPM300_PAT_VISIT.psn_int_id = TSM040_PERSON_HDR.psn_int_id INNER JOIN
TSM950_LOCATION_REF AS TSM950_ROOM ON TPM300_PAT_VISIT.loc_lvl_4_id = TSM950_ROOM.loc_int_id INNER JOIN
TSM950_LOCATION_REF AS TSM950_BED ON TPM300_PAT_VISIT.loc_lvl_5_id = TSM950_BED.loc_int_id INNER JOIN
TSM950_LOCATION_REF AS TSM950_STATION ON TPM300_PAT_VISIT.loc_lvl_3_id = TSM950_STATION.loc_int_id INNER JOIN
TSM180_MST_COD_DTL AS TSM180_CAT ON TPM300_PAT_VISIT.pat_cat_cd = TSM180_CAT.cod_dtl_int_id
WHERE (TSM950_STATION.loc_ext_id IN ('MS', 'OB', 'SCU', 'NURS')) AND
(convert(datetime,convert(varchar(10),TPM300_PAT_VISIT.adm_ts,101) +' 00:00:00'))<=convert(datetime,convert(varchar(10),DATEADD(DAY,-7,GETDATE()),101) +' 23:58:00') AND
(convert(datetime,convert(varchar(10),TPM300_PAT_VISIT.dschrg_ts,101) +' 00:00:00'))>=convert(datetime,convert(varchar(10),DATEADD(DAY,-7,GETDATE()),101) +' 23:59:00')
GROUP BY TSM950_STATION.loc_ext_id,
convert(varchar(10),DATEADD(DAY,-7,GETDATE()),101),
TSM180_CAT.cod_dtl_ds
答案 0 :(得分:6)
问题是以下表达式是常量:
convert(varchar(10),DATEADD(DAY,-7,GETDATE()),101),
只需从您的群组中移除此内容,如下所示:
GROUP BY TSM950_STATION.loc_ext_id, TSM180_CAT.cod_dtl_ds
答案 1 :(得分:2)
您不需要同时DISTINCT
和GROUP BY
。根据定义,记录将是不同的,因为所有类似的记录都在同一组......
此外,您不需要GROUP BY
常量。
这给...
SELECT TSM950_STATION.loc_ext_id AS STATION,
convert(varchar(10),DATEADD(DAY,-7,GETDATE()),101) AS CENSUS_DATE,
TSM180_CAT.cod_dtl_ds,
COUNT(TPM300_PAT_VISIT.vst_ext_id)
FROM TPM300_PAT_VISIT INNER JOIN
TSM040_PERSON_HDR ON TPM300_PAT_VISIT.psn_int_id = TSM040_PERSON_HDR.psn_int_id INNER JOIN
TSM950_LOCATION_REF AS TSM950_ROOM ON TPM300_PAT_VISIT.loc_lvl_4_id = TSM950_ROOM.loc_int_id INNER JOIN
TSM950_LOCATION_REF AS TSM950_BED ON TPM300_PAT_VISIT.loc_lvl_5_id = TSM950_BED.loc_int_id INNER JOIN
TSM950_LOCATION_REF AS TSM950_STATION ON TPM300_PAT_VISIT.loc_lvl_3_id = TSM950_STATION.loc_int_id INNER JOIN
TSM180_MST_COD_DTL AS TSM180_CAT ON TPM300_PAT_VISIT.pat_cat_cd = TSM180_CAT.cod_dtl_int_id
WHERE (TSM950_STATION.loc_ext_id IN ('MS', 'OB', 'SCU', 'NURS'))
AND (convert(datetime,convert(varchar(10),TPM300_PAT_VISIT.adm_ts,101) +' 00:00:00'))<=convert(datetime,convert(varchar(10),DATEADD(DAY,-7,GETDATE()),101) +' 23:58:00')
AND (convert(datetime,convert(varchar(10),TPM300_PAT_VISIT.dschrg_ts,101) +' 00:00:00'))>=convert(datetime,convert(varchar(10),DATEADD(DAY,-7,GETDATE()),101) +' 23:59:00')
GROUP BY TSM950_STATION.loc_ext_id,
TSM180_CAT.cod_dtl_ds