处理子群体时SQL查询语法错误

时间:2015-02-07 00:44:53

标签: sql sql-server oracle

我是一名软件测试人员,使用此SQL查询来检查数据库中的一些数据:

select * from (
select sum(case when qm.HEALTHY_TERM_NEWBORN='E' then 1 else 0 end) as num, -- choose measure status columns here as appropriate
sum(case when qm.HEALTHY_TERM_NEWBORN  in ('D','E') then 1 else 0 end) as denom,
-- summary level columns
cms.FACILITY_GROUP_ID,
-- time period columns
dt.USA_FISCAL_YEAR_BEGIN_DT
from F_MU_QM_EH_2014_IP_ADMSN qm
inner join CMS_MU_INFO cms on qm.DISCHARGE_CMS_MU_ID=cms.CMS_MU_ID
inner join DATE_DIMENSION dt on dt.CALENDAR_DT=qm.DISCHARGE_DATE
-- do filtering here if any
where cms.FACILITY_GROUP_ID='130170' AND dt.USA_FISCAL_YEAR_BEGIN_DT='10/01/2013'
group by -- summary level grouping
cms.FACILITY_GROUP_ID,
-- time period grouping
dt.USA_FISCAL_YEAR_BEGIN_DT
) x 
where denom is not null

该脚本从一列信息中返回聚合数据;但是,对于我的一些测试,我需要根据在第一列中定义子群的另一列过滤列。我试过这个:

select * from (
sum(case when qm.SCIPINF1='E' and qm.SCIPINF1_POPULATION_C=7 then 1 else 0 end) as num
sum(case when qm.SCIPINF1 in ('D','E') and qm.SCIPINF1_POPULATION_C=1 then 1 else 0 end) as denom
-- summary level columns
cms.FACILITY_GROUP_ID,
-- time period columns
dt.USA_FISCAL_YEAR_BEGIN_DT
from F_MU_QM_EH_2014_IP_ADMSN qm
inner join CMS_MU_INFO cms on qm.DISCHARGE_CMS_MU_ID=cms.CMS_MU_ID
inner join DATE_DIMENSION dt on dt.CALENDAR_DT=qm.DISCHARGE_DATE
-- do filtering here if any
where cms.FACILITY_GROUP_ID='130170' AND dt.USA_FISCAL_YEAR_BEGIN_DT='10/01/2013'
group by -- summary level grouping
cms.FACILITY_GROUP_ID,
-- time period grouping
dt.USA_FISCAL_YEAR_BEGIN_DT
) x 
where denom is not null

但是我在' sum'附近遇到了语法错误。据Oracle称,我根据MS SQL Server Management Studio缺少一个括号。你们这些美丽的网民都知道我做错了吗?

1 个答案:

答案 0 :(得分:2)

两列之间缺少commaselect

中的sub-select
SELECT *
FROM   (SELECT Sum(CASE   -- select missing here
                     WHEN qm.SCIPINF1 = 'E'
                          AND qm.SCIPINF1_POPULATION_C = 7 THEN 1
                     ELSE 0
                   END) AS num, --comma missing here
               Sum(CASE
                     WHEN qm.SCIPINF1 IN ( 'D', 'E' )
                          AND qm.SCIPINF1_POPULATION_C = 1 THEN 1
                     ELSE 0
                   END) AS denom, --comma missing here
               -- summary level columns
               cms.FACILITY_GROUP_ID,
               -- time period columns
               dt.USA_FISCAL_YEAR_BEGIN_DT