-2

I have two tables. Table Employees:

EmployeeID (employees)  LastName (employees)    FirstName (employees)
1                       Davolio                 Nancy

And Table Orders:

OrderID (orders)    CustomerID (orders) EmployeeID (orders)
10248               90                  5
10278               45                  1
10238               47                  1

I redacted the full listing because it's hundreds of rows.

In the table Employees, the EmployeeID can uniquely identify an employee, meaning it will not repeat in the Employee table. However in the Table 'Order' The employeeID can repeat several times because an employee can sell help with many orders.

Anyway, I can see here that in the Orders table, an employeeID will repeat several times, which means I need to use COUNT(EmployeeID)>=2 somewhere in my MySQL code.

This is what I'd like:

EmployeeID              Number of Orders
1                       2

As you can see, the EmployeeID shows up twice in the "orders" table. So he sold 2 items, and it links to his 1 Employee ID.

So this is what I tried:

SELECT EmployeeID, COUNT(EmployeeID) FROM
employees A inner join 
orders B
ON (A.EmployeeID=B.EmployeeID)
WHERE COUNT(B.EmployeeID >=2)

This is the output:

Error: Column 'EmployeeID' in field list is ambiguous — ERROR CODE 1052

I'm not sure how I would get this result in this scenario.

  • 2
    The error basically means that it doesn't know which EmployeeID you mean in your select as it is in both employees and orders. Changing it to A.EmployeeID would get rid of that error, but it also won't get the results as you want them, see the answer below. – Dijkgraaf Oct 14 '20 at 21:25
  • @Dijkgraaf thanks for the explanation there. I had a feeling that's what "ambiguous" meant –  Oct 14 '20 at 21:28
  • Please in code questions give a [mre]--cut & paste & runnable code, including smallest representative example input as code; desired & actual output (including verbatim error messages); tags & versions; clear specification & explanation. Give the least code you can that is code that you show is OK extended by code that you show is not OK. (Debugging fundamental.) For SQL that includes DBMS & DDL (including constraints & indexes) & input as code in table format. [ask] Pause work on the overall goal, chop code to the 1st expression not giving what you expect & say what you expect & why. – philipxy Oct 14 '20 at 22:59
  • This is a faq. Please before considering posting read your textbook and/or manual & google any error message or many clear, concise & precise phrasings of your question/problem/goal, with & without your particular strings/names & site:stackoverflow.com & tags; read many answers. Reflect your research. See [ask] & the voting arrow mouseover texts. If you post a question, use one phrasing as title. – philipxy Oct 14 '20 at 23:00
  • Does this answer your question? [1052: Column 'id' in field list is ambiguous](https://stackoverflow.com/questions/6638520/1052-column-id-in-field-list-is-ambiguous) – philipxy Oct 14 '20 at 23:01
  • There is never a reason to be rude, even if you think someone else was. The "does this answer" is an automated comment (poorly phrased, complain to the company like the rest of us) posted when one flags a post as duplicate. However there is no justification for thinking it must be meant negatively. Also you have jumped to (wrong) (negative) conclusions not only about that. Re my profile looking like Strawberry's we both added a hashtag to an empty profile during a SO promotion. (You could have just googled it.) – philipxy Oct 15 '20 at 01:40

2 Answers2

0

You want to build groups of orders that belong to the same employee, and then filter on the count of rows per group. For this, you can use group by and having:

select employeeid, count(*) cnt_orders
from employees e 
inner join orders o using(employeeid)
group by employeeid
having count(*) >= 2

Note that a join is not necessary here. You can get the result you want directly from the table of orders:

select employeeid, count(*) cnt_orders
from orders 
group by employeeid
having count(*) >= 2
GMB
  • 216,147
  • 25
  • 84
  • 135
0

There's no need to join with the employees table, you can get the employee ID from orders. You would only need to join if you also need other information from the employee table, such as their name.

You need GROUP BY employeeID to get a count for each employee.

>= 2 should not be inside the COUNT() function, you want to compare the result.

You need to use HAVING rather than WHERE. WHERE is used to select the rows to process before aggregating.

You should use COUNT(*) rather than COUNT(columnName) unless you need to exclude null values of the column from the count.

If you give an alias to the COUNT(*) result, you can use that alias in the HAVING clause rather than restating the function.

SELECT EmployeeID, COUNT(*) AS number_of_orders
FROM orders B
GROUP BY EmployeeID
HAVING number_of_orders >= 2

The reason for your error about the ambiguous column is because both tables have EmployeeID columns. In the SELECT list you need to specify A.EmployeeID or B.EmployeeID, just as you did in the ON clause.

Barmar
  • 741,623
  • 53
  • 500
  • 612