运行sql查询时出现“无效列名'2017-09'..”的错误

时间:2018-01-31 10:53:14

标签: sql sql-server

当我在查询下方运行时,它会给我错误Invalid column name '2017-09'., 我尝试了一些谷歌搜索,但仍然没有得到它的结果,任何人都可以请看看这个查询,并帮助我可能是什么问题

SELECT 
COUNT(CASE when (SOC = "1" and MonthStart="2017-09") then 1 ELSE NULL END) as SOCCount_2017_09, 
COUNT(CASE when (Recert = "1" and MonthStart="2017-09") then 1 ELSE NULL END) as RecertCount_2017_09, 
COUNT(CASE when (Recert = "1" and MonthStart="2017-09") then 1 ELSE NULL END) as RecertPer1_2017_09, 
COUNT(CASE when (MonthStart="2017-09") then 1 ELSE NULL END) as TotalEpisode_2017_09, 
ROUND(AVG(CASE when (FullHHRG > "0" and MonthStart="2017-09") then FullHHRG ELSE NULL END),2) as AvgFullHHRG_2017_09,
ROUND(AVG(CASE when (CMW > "0" and MonthStart="2017-09") then CMW ELSE NULL END),3) as AvgCMW_2017_09, 
COUNT(CASE when (LUPA = "1" and MonthEnd="2017-09") then 1 ELSE NULL END) as LUPAs_2017_09, 
COUNT(CASE when (LUPA = "1" and MonthEnd="2017-09") then 1 ELSE NULL END) as LUPAPer_2017_09, 
COUNT(CASE when ((MonthEnd="2017-09" and Status = "Closed") ) then 1 ELSE NULL END) as TotalEndEpisode_2017_09, 
ROUND(AVG(CASE when (SN_Ep = "1" and MonthEnd="2017-09") then SN_Visits ELSE NULL END),1) as SNVisitsSNEps_2017_09, 
ROUND(AVG(CASE when (THVisits > "0" and MonthEnd="2017-09") then THVisits ELSE NULL END),1) as THVisitsTHEps_2017_09, 
ROUND(AVG(CASE when (Status = "Closed" and MonthEnd="2017-09") then TotVisits ELSE NULL END),1) as AvgTotVisits_2017_09, 
ROUND(AVG(CASE when (Status = "Closed" and MonthEnd="2017-09") then SNVisits ELSE NULL END),1) as AvgSNVisits_2017_09, 
ROUND(AVG(CASE when (Status = "Closed" and MonthEnd="2017-09") then TotTherapy ELSE NULL END),1) as AvgTHVisits_2017_09, 
ROUND(AVG(CASE when (NetHHRG > "0" and MonthEnd="2017-09") then NetHHRG ELSE NULL END),2) as AvgNetHHRG_2017_09, 
SUM(CASE when (Status = "Closed" and MonthEnd="2017-09") then TH_Ep ELSE NULL END) as TherapyPer_2017_09 
FROM tb_Episode WHERE CustID = "27" AND PayerType = "Ep" AND BranchID IN (241) 

1 个答案:

答案 0 :(得分:3)

SQL-Server中,您必须在使用Single Quotes(')时使用Double Quotes(")而不是Strings

试试这个:

SELECT 
COUNT(CASE when (SOC = '1' and MonthStart='2017-09') then 1 ELSE NULL END) as SOCCount_2017_09, 
COUNT(CASE when (Recert = '1' and MonthStart='2017-09') then 1 ELSE NULL END) as RecertCount_2017_09, 
COUNT(CASE when (Recert = '1' and MonthStart='2017-09') then 1 ELSE NULL END) as RecertPer1_2017_09, 
COUNT(CASE when (MonthStart='2017-09') then 1 ELSE NULL END) as TotalEpisode_2017_09, 
ROUND(AVG(CASE when (FullHHRG > '0' and MonthStart='2017-09') then FullHHRG ELSE NULL END),2) as AvgFullHHRG_2017_09,
ROUND(AVG(CASE when (CMW > '0' and MonthStart='2017-09') then CMW ELSE NULL END),3) as AvgCMW_2017_09, 
COUNT(CASE when (LUPA = '1' and MonthEnd='2017-09') then 1 ELSE NULL END) as LUPAs_2017_09, 
COUNT(CASE when (LUPA = '1' and MonthEnd='2017-09') then 1 ELSE NULL END) as LUPAPer_2017_09, 
COUNT(CASE when ((MonthEnd='2017-09' and Status = 'Closed') ) then 1 ELSE NULL END) as TotalEndEpisode_2017_09, 
ROUND(AVG(CASE when (SN_Ep = '1' and MonthEnd='2017-09') then SN_Visits ELSE NULL END),1) as SNVisitsSNEps_2017_09, 
ROUND(AVG(CASE when (THVisits > '0' and MonthEnd='2017-09') then THVisits ELSE NULL END),1) as THVisitsTHEps_2017_09, 
ROUND(AVG(CASE when (Status = 'Closed' and MonthEnd='2017-09') then TotVisits ELSE NULL END),1) as AvgTotVisits_2017_09, 
ROUND(AVG(CASE when (Status = 'Closed' and MonthEnd='2017-09') then SNVisits ELSE NULL END),1) as AvgSNVisits_2017_09, 
ROUND(AVG(CASE when (Status = 'Closed' and MonthEnd='2017-09') then TotTherapy ELSE NULL END),1) as AvgTHVisits_2017_09, 
ROUND(AVG(CASE when (NetHHRG > '0' and MonthEnd='2017-09') then NetHHRG ELSE NULL END),2) as AvgNetHHRG_2017_09, 
SUM(CASE when (Status = 'Closed' and MonthEnd='2017-09') then TH_Ep ELSE NULL END) as TherapyPer_2017_09 
FROM tb_Episode WHERE CustID = '27' AND PayerType = 'Ep' AND BranchID IN (241)