Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select from nested select T-SQL

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
)
like image 574
zapoo Avatar asked Aug 08 '11 20:08

zapoo


People also ask

What is nested SELECT in SQL?

A nested SELECT is a query within a query, i.e. when you have a SELECT statement within the main SELECT.

Can you SELECT from a subquery?

For example, you can use subqueries in the SELECT, FROM, WHERE, or HAVING clauses. A subquery may return either a single value or multiple rows. A single value is also known as a scalar value.

Can you have a subquery within a subquery?

A subquery can be nested inside the WHERE or HAVING clause of an outer SELECT , INSERT , UPDATE , or DELETE statement, or inside another subquery.

How do you write a nested SELECT query?

Example -1 : Nested subqueries SELECT job_id,AVG(salary) FROM employees GROUP BY job_id HAVING AVG(salary)< (SELECT MAX(AVG(min_salary)) FROM jobs WHERE job_id IN (SELECT job_id FROM job_history WHERE department_id BETWEEN 50 AND 100) GROUP BY job_id); The above code is executed in Oracle 11g Express Edition.


2 Answers

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
like image 193
Dalen Avatar answered Sep 18 '22 00:09

Dalen


Why not just do this instead?

SELECT COUNT(cvs)
    FROM Applications
like image 27
Joe Stefanelli Avatar answered Sep 22 '22 00:09

Joe Stefanelli