17

I wish to modify data by selecting them in a inner query and count one of them modified.. It gives error..

select count(cvs) from
(
  select 
  cvs,
  (case Citycode when 123 then 'test' else 'other' end) as CityName ,
  (case ProductCode when '000' then 'test3' when 'ss' then 'xtr' else 'ddd' end) as CardName
  from Applications
)
3
  • 1
    If all you care about is the count, why are you selecting all of that other data? You're not grouping or aggregating any of the sub query data, so why not just select the count without the sub query? Commented Aug 8, 2011 at 20:39
  • 5
    Probably all you are missing is an alias. Select Count(*) from (...) A Commented Aug 8, 2011 at 20:39
  • my case is quite different just I simplify the question guys.. But it solved by using giving the alias of the query.. thx for help Commented Aug 8, 2011 at 20:48

4 Answers 4

80

you need to give an alias to the subquery:

select count(x.cvs) from
(
  select 
  cvs,
  (case Citycode when 123 then 'test' else 'other' end) as CityName ,
  (case ProductCode when '000' then 'test3' when 'ss' then 'xtr' else 'ddd' end) as CardName
  from Applications
) x
Sign up to request clarification or add additional context in comments.

Comments

1

Why not just do this instead?

SELECT COUNT(cvs)
    FROM Applications

1 Comment

I'm guessing (hoping) he has more logic he didn't include
0

It appears your query could be simplified to..

SELECT COUNT(cvs) FROM Applications

Is there a reason you have the select nested and you are ignoring the other columns being selected?

1 Comment

thx Quintin but my case was related by using alias for sub query
0

Two things I see off the bat:

1 - You don't need the nested subquery for what you are doing in the example. You could just as easily do:

SELECT COUNT(cvs) FROM application

2 - You need an alias for the subquery, like (<subquery>) as SubQ

Comments

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.