Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Error Msg: 116

Author  Topic 

AngHell
Starting Member

2 Posts

Posted - 2012-01-01 : 15:29:55
Hi all,
I'm a student and we've just started SQL. (using Northwind..)

this is the query we need to create: (and how I tried to solve it..)

-- Which employee's sales exceeded 20K$ in 1996?

select * --e.FirstName 'First Name', e.LastName 'Last Name', e.EmployeeID 'Employee ID'
from Employees e, [Order Details] od, Orders o
where(select e.EmployeeID, SUM(od.Quantity*od.UnitPrice)'total sales per employee in 1996'
from Employees e, [Order Details] od, Orders o
where e.EmployeeID = o.EmployeeID and YEAR(o.OrderDate)=1996 and o.OrderID=od.OrderID
group by e.EmployeeID
)>20000
group by e.FirstName
order by e.EmployeeID

This is the error msg I've recieved:
Msg 116, Level 16, State 1, Line 8
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.

what does it mean and how can it be fixed?
thx on advance!!
Eyal :)

Best Regards,
Eyal David :)

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2012-01-01 : 16:27:09
That stuff inside your where clause can only return one value, or sql will try to figure out what
EmployeeID,TotalSalesPerEmployeein1996 > 20000 means. What you really want is a HAVING clause
select e.FirstName as 'First Name', e.LastName as 'Last Name', e.EmployeeID as 'Employee ID'
from Employees e
inner join [Order Details] od
on e.EmployeeID = o.EmployeeID
inner join Orders o on o.OrderID=od.OrderID
where o.orderdate >= '19960101' and o.orderdate <='19961231'
group by e.FirstName, e.LastName, e.EmployeeID-- if it's in your select list, it has to be in your --group by
having SUM(od.Quantity*od.UnitPrice) > 20000
order by e.EmployeeID

Jim


Everyday I learn something that somebody else already knew
Go to Top of Page

AngHell
Starting Member

2 Posts

Posted - 2012-01-01 : 17:24:40
TY VM Jim!
I will try to understand it 1st thing on the morning tommorow :)



Best Regards,
Eyal David :)
Go to Top of Page
   

- Advertisement -