[英]SQL Subquery as expression
I have the following two tables:我有以下两个表:
Create Table #Claims (ClaimId int, FormType varchar(3), AdjustmentVersion int)
Create Table #Secondary (FeeId int, FeeName varchar(60))
I want to be able to return all the claims that are a specific FormType and AdjustmentVersion.我希望能够返回所有属于特定 FormType 和 AdjustmentVersion 的声明。
Given the following query:鉴于以下查询:
select * from #Claims where FormType = 'DEN' and AdjustmentVersion in (1,2,3,(select FeeId from #Secondary))
How do I correct the syntax to match the AdjustmentVersion on the given integers and the subquery at the same time?如何更正语法以同时匹配给定整数和子查询上的 AdjustmentVersion? Currently, I am getting error 'Subquery returned more than one result'.
目前,我收到错误“子查询返回了多个结果”。
I tried using and OR with AdjustmentVersion but that ended up ignoring the FormType = 'DEN'我尝试使用和或与 AdjustmentVersion 但最终忽略了 FormType = 'DEN'
You're trying to pass in a result set into an IN clause.您正在尝试将结果集传递到 IN 子句中。 You can only give it a scalar (eg you could give it the TOP 1 row).
你只能给它一个标量(例如你可以给它 TOP 1 行)。 The right way to apply a condition like this is to add an EXISTS clause to your WHERE.
应用此类条件的正确方法是在 WHERE 中添加EXISTS子句。 Alternative guide .
替代指南。
If you still need some guidance after reading those, let me know.如果您在阅读这些内容后仍需要一些指导,请告诉我。
You can split the values into two sets, the literals and the subquery:您可以将值分成两组,文字和子查询:
select *
from #Claims
where FormType = 'DEN' and
( AdjustmentVersion in (1,2,3) or AdjustmentVersion in (select FeeId from #Secondary) );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.