Link to home
Start Free TrialLog in
Avatar of sharris_glascol
sharris_glascolFlag for United States of America

asked on

sql count if

I am needing to look at a table and see if columns have a match.  I want to see if multiple employees have been clocked onto the same work order and operation.  I have the columns odd_no, Oper_no and empid.  for example I may have the work order number 123456 with operation number 10.  I want to COUNT how many different employees worked on that operation..
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

SELECT COUNT(DISTINCT empid)
FROM your_table
WHERE odd_no = 123456 AND oper_no = 10

Open in new window

You'll want to look at the full data set just to make sure the above is correct, and any duplicate values are being handled

SELECT odd_no, oper_no, emp_id
FROM your_table
WHERE odd_no = 123456 AND oper_no = 10
ORDER BY odd_no, oper_no, emp_id

Open in new window

Avatar of sharris_glascol

ASKER

what if I want to look at all work orders and operations to see how many employees where clocked into the same one and not just one  exact work order?
Not entirely sure what you mean by 'the same one', but give this a whirl..

List...

SELECT odd_no, oper_no, emp_id
FROM your_table
ORDER BY odd_no, oper_no, emp_id

Open in new window

Count...

SELECT odd_no, oper_no, COUNT(emp_id) as empl_id_count
FROM your_table
GROUP BY odd_no, oper_no
ORDER BY odd_no, oper_no

Open in new window

Here is what i need to count

work order     operation number     employee number     count
12345                  10                                2321                           2
2468                      20                               1234                          2
12345                     10                               1234                         2
2468                       20                                2321                        2
13579                    10                                 2321                        1

This is kinda what I am looking for....
Nice mockup data.  Next time please add that to your original question, so we don't have to spend time flushing out requirements.

Based on the set above, give this a whirl, and add an ORDER BY clause to sort it by whatever is most readable..
SELECT 
   odd_no as work_order, 
   oper_no as operation_number, 
   emp_id as employee_number, 
   COUNT(emp_id) as empl_id_count
FROM your_table
GROUP BY odd_no, oper_no, emp_id

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of PortletPaul
PortletPaul
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial