Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL错误:关键字“FROM”附近的语法不正确_Sql_Sql Server - Fatal编程技术网

SQL错误:关键字“FROM”附近的语法不正确

SQL错误:关键字“FROM”附近的语法不正确,sql,sql-server,Sql,Sql Server,这是我的SQL查询,它在“FROM”部分周围/附近抛出语法错误。任何帮助都会很棒 with findlives AS ( select distinct a.ClientId, a.PolicyNo, a.Acc, a.Lives, a.FundTypeCmr from factInforceProfitByFundGb as a where TimePeriodId = 202006 and BenefitCode='66' and T

这是我的SQL查询,它在“FROM”部分周围/附近抛出语法错误。任何帮助都会很棒

with findlives AS 
(
    select distinct a.ClientId, a.PolicyNo, a.Acc, a.Lives, a.FundTypeCmr 
    from factInforceProfitByFundGb as a 
    where TimePeriodId = 202006 
      and BenefitCode='66' 
      and TerminationDate = '9999-12-31'
)
select sum(Lives) AS spousal 
from findlives 
from [GB_Msi_P1].[dbo].[factInforceProfitByFundGb] as a 
where TimePeriodId >= 201811  
  and BenefitCode in ('25', '26', '29', '46', '66')
group by TimePeriodId
order by TimePeriodId asc
这里是错误

信息156,15级,状态1,第22行 关键字“FROM”附近的语法不正确


正如穿刺者在评论中所说,每个SELECT只能有一个FROM子句。多个表需要连接在一起,或者使用逗号链接

也许您正试图将CTE与它所基于的表连接起来,但您希望CTE中的类型66记录与表中的其他类型记录相匹配?在这种情况下,您确实需要加入。也许是这个

with findlives AS 
(
    select distinct a.ClientId, a.PolicyNo, a.Acc, a.Lives, a.FundTypeCmr 
    from factInforceProfitByFundGb as a 
    where TimePeriodId = 202006 
      and BenefitCode='66' 
      and TerminationDate = '9999-12-31'
)
select a.TimePeriodId,sum(findlives.Lives) AS spousal 
from findlives 
JOIN [GB_Msi_P1].[dbo].[factInforceProfitByFundGb] as a 
   ON findlives.ClientID=a.clientID
where a.TimePeriodId >= 201811  
  and a.BenefitCode in ('25', '26', '29', '46', '66')
group by a.TimePeriodId
order by a.TimePeriodId asc

我还添加了额外的表别名以澄清问题,并在SELECT中添加了.TimePeriodId,因为否则您的输出将无法清楚哪个输出行与哪个组成员关联?

错误是什么?当遇到错误并且对错误有疑问时,您应该在问题中包含该错误文本。不要翻译或意译,逐字复制并粘贴完整的错误文本到问题中。使用该按钮以该文本更新您的问题。您只能有一个FROM子句。如果要联接多个表,可以使用join。