Skip to content
Advertisement

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.

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? 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’

Advertisement

Answer

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) );
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement