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.
| 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 owhere(select e.EmployeeID, SUM(od.Quantity*od.UnitPrice)'total sales per employee in 1996'from Employees e, [Order Details] od, Orders owhere e.EmployeeID = o.EmployeeID and YEAR(o.OrderDate)=1996 and o.OrderID=od.OrderID group by e.EmployeeID)>20000group by e.FirstNameorder by e.EmployeeIDThis is the error msg I've recieved: Msg 116, Level 16, State 1, Line 8Only 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 clauseselect e.FirstName as 'First Name', e.LastName as 'Last Name', e.EmployeeID as 'Employee ID'from Employees einner join [Order Details] odon e.EmployeeID = o.EmployeeIDinner join Orders o on o.OrderID=od.OrderIDwhere 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 byhaving SUM(od.Quantity*od.UnitPrice) > 20000 order by e.EmployeeIDJimEveryday I learn something that somebody else already knew |
 |
|
|
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 :) |
 |
|
|
|
|
|
|
|