Sql 使用子查询选择“选择不工作”
我一直在这段代码中发现错误。我没有使用EXISTS。请帮助 当子查询未引入EXISTS时,只能在选择列表中指定一个表达式 SQL:Sql 使用子查询选择“选择不工作”,sql,sql-server,tsql,Sql,Sql Server,Tsql,我一直在这段代码中发现错误。我没有使用EXISTS。请帮助 当子查询未引入EXISTS时,只能在选择列表中指定一个表达式 SQL: 问题在于查询的这一部分: (select case when datepart(hh,convert(datetime,grp.StartDateTime)) > 12 then datepart(hh,convert(datetime,grp.StartDateTime))-12
问题在于查询的这一部分:
(select case when datepart(hh,convert(datetime,grp.StartDateTime)) > 12
then datepart(hh,convert(datetime,grp.StartDateTime))-12
else case when datepart(hh,convert(datetime,grp.StartDateTime)) = 0
then '12' else datepart(hh,convert(datetime,grp.StartDateTime))
end
end as starthour,
首先,您根本不需要选择。其次,您缺少右括号()
)。我建议:
(case when datepart(hh,convert(datetime,grp.StartDateTime)) > 12
then datepart(hh,convert(datetime,grp.StartDateTime))-12
else (case when datepart(hh,convert(datetime,grp.StartDateTime)) = 0
then '12'
else datepart(hh,convert(datetime,grp.StartDateTime))
end)
end) as starthour,
请发布完整的查询,否则无法说出问题所在。如果我不得不猜测你在WHERE语句中使用了IN或NOT IN子句,该子句有一个子查询返回多个列。对不起,不,我没有使用,IN认为不需要。Hanks根据你的建议修复了代码,这似乎是一个很好的小错误,这是最糟糕的部分coding@Henry . . . 我非常小心地缩进代码并使用括号来帮助防止这些类型的错误。
(case when datepart(hh,convert(datetime,grp.StartDateTime)) > 12
then datepart(hh,convert(datetime,grp.StartDateTime))-12
else (case when datepart(hh,convert(datetime,grp.StartDateTime)) = 0
then '12'
else datepart(hh,convert(datetime,grp.StartDateTime))
end)
end) as starthour,