簡體   English   中英

SQL 子查詢作為表達式

[英]SQL Subquery as expression

我有以下兩個表:

Create Table #Claims (ClaimId int, FormType varchar(3), AdjustmentVersion int)

Create Table #Secondary (FeeId int, FeeName varchar(60))

我希望能夠返回所有屬於特定 FormType 和 AdjustmentVersion 的聲明。

鑒於以下查詢:

select * from #Claims where FormType = 'DEN' and AdjustmentVersion in (1,2,3,(select FeeId from #Secondary))

如何更正語法以同時匹配給定整數和子查詢上的 AdjustmentVersion? 目前,我收到錯誤“子查詢返回了多個結果”。

我嘗試使用和或與 AdjustmentVersion 但最終忽略了 FormType = 'DEN'

您正在嘗試將結果集傳遞到 IN 子句中。 你只能給它一個標量(例如你可以給它 TOP 1 行)。 應用此類條件的正確方法是在 WHERE 中添加EXISTS子句。 替代指南

如果您在閱讀這些內容后仍需要一些指導,請告訴我。

您可以將值分成兩組,文字和子查詢:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM