简体   繁体   English

SQL 子查询作为表达式

[英]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.

相关问题 SQL子查询缺少表达式 - SQL missing expression on subquery SQL子查询表达式 - SQL subquery expression SQL错误:当子查询遵循=,!=,&lt;,&lt;=,&gt;,&gt; =或将子查询用作表达式时,不允许这样做 - SQL ERROR: This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression SQL错误-子查询后跟=,!=,&lt;,&lt;=,&gt;,&gt; =或将子查询用作表达式时,不允许这样做 - Error in SQL - This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression 如何避免将多个子查询作为表达式(SQL优化) - How to avoid multiple subquery as expression (SQL optimization) 将SQL CTE表达式转换为普通子查询 - Convert SQL CTE expression to normal subquery Oracle 中使用子查询和文字表达式的 SQL 性能 - SQl performance with subquery and literal expression in Oracle SQL Server-替代包含聚合或子查询的表达式的函数的替代方法 - SQL Server - Alternatives to aggregate functions for an expression containing an aggregate or subquery 子查询时只能指定表达式…SQL Server - Only expression can be specified when subquery… SQL server SQL Server无法对包含聚合或子查询的表达式执行聚合函数 - SQL Server Cannot perform an aggregate function on an expression containing an aggregate or a subquery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM