Click here to Skip to main content
15,890,609 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Simple SQL query:
SQL
(select e.lastName as Name, count(o.orderid) as numOrd from
Employees e join Orders o on e.employeeid=o.employeeid
group by e.lastName)

and result
VB
Buchanan    42
Callahan    104
Davolio     123
Dodsworth   43

My question is how to achieve in SQL something like that:
SQL
let queryResult = 
(select e.lastName as Name, count(o.orderid) as numOrd from
Employees e join Orders o on e.employeeid=o.employeeid
group by e.lastName)


and after that to write something like this, which will be the output:
SQL
select AVG(qr.numOrd) from queryResult qr

Is it possible without creating any new tables?
Posted

The way you ask for can't work in SQL. You have to options:
1. Declare a table variable and insert the results of the first select into it, than use it in the second select
2. Use the first select in the from part - select * from (select field from table)
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900