3

I have a query:

select 
      index_imsid
    , SUM(weighted_value) sumWeightedValue
    , (
        select 
              top 1 percentof
            , [Plan_Name_OR_Payment_Type] 
        from [v_PlanPerProvider1] 
        where [PLAN_RANK]=1
      ) plan1
from [v_PlanPerProvider1]
where plan_rank between 1 and 10
group by index_imsid
order by 1

and I am getting this error:

Msg 116, Level 16, State 1, Line 3
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.

Can you please help me to understand why I am getting this error?

It appears that it does not like the select statement as one of the columns?

3
  • 1
    Why do so many people dislike the return / enter key? Are horizontal scrollbars that cool-looking? Commented Sep 11, 2012 at 21:51
  • Because you have to hit 8 spaces after the return and the code will look more zigzagged? Commented Sep 11, 2012 at 23:12
  • 2
    I've edited the code and didn't hit the space 8 times... You can always copy the code, edit in an editor and paste it back. Faster, cleaner and no horizontal scrolling. What's more, in this case it is clear for the first glance already that too many values are selected in the subquery. Commented Sep 12, 2012 at 3:44

2 Answers 2

9

It does not like you selecting two columns in the subquery. You can only select one column at a time.

  select index_imsid, SUM(weighted_value) sumWeightedValue,
    (select top 1 percentof from [v_PlanPerProvider1] where [PLAN_RANK]=1) percentof
    (select top 1 [Plan_Name_OR_Payment_Type] from [v_PlanPerProvider1] where [PLAN_RANK]=1) Plan_Name_OR_Payment_Type
  from [v_PlanPerProvider1]
  where plan_rank between 1 and 10
  group by index_imsid
  order by 1
Sign up to request clarification or add additional context in comments.

Comments

3

Or you can do this:

select  
    t1.index_imsid, SUM(t1.weighted_value) sumWeightedValue, t2.percentof, t2.[Plan_Name_OR_Payment_Type]
from [v_PlanPerProvider1] t1
cross join (select top 1 percentof, [Plan_Name_OR_Payment_Type] from [v_PlanPerProvider1] where [PLAN_RANK]=1) t2 
where t1.plan_rank between 1 and 10 
group by t1.index_imsid, t2.percentof, t2.[Plan_Name_OR_Payment_Type]
order by 1 

4 Comments

cool!! can you explain please to me how the cross join works?
It means that every row of the joined table (in this case there will be only one) is joined to the restult set. So, all of the columns are available. If you guarantee only one row, it will not explode your result set, but if there were multiple rows, then your resultset would have its count multiplied by that number.
thanks so much! what is the diff between that and full outer join?
A Full outer join has join criteria (an ON clause). But, it will return all rows from both tables (left and right sides) even if the join condition is false (there will be NULLs for columns from the "other" table). Left Joins and Right Joins only return all of the non-matching rows from one side. A CROSS JOIN has no joining condition, it just adds all columns of all rows of the joined table to every row of the result set.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.