Search code examples
sqlsql-server-2008sql-server-2008-r2northwind

why the following code shows the error? and what was the alternate?


my Question is---> Each Sale made by an employee will get him 10% commission of each total sale amount, find out which employee has so far earned more commission with very less number of orders [Refer Northwind database]

i create a column 'com' to calculate commission percentage.

select distinct(EmployeeID) from orders where MIN(count(employeeid))and max(com)

when i run the sqlserver query it shows the error like

"An expression of non-boolean type specified in a context where a condition is expected, near 'and'."


Solution

  • There are several issues with your query. Briefly, you cannot leave the max() function without comparing it to some other expression. You also cannot specify the aggregation functions inside the where clause: for that purpose you will need to enclose them into (select max()....) subquery. But for your purpose, where you need to check the sum() and max() over the queried table, you need to specify them in the having as below:

    SELECT EmployeeID
    FROM orders 
    GROUP BY EmployeeID
    HAVING SUM(debet)=
        (SELECT MAX (sum(com)) from orders GROUP BY EmployeeID)
    AND COUNT(ledger_id)=
        (SELECT MIN(COUNT(EmployeeID)) FROM orders GROUP BY EmployeeID)