sql count if

Posted on 2014-04-09
Last Modified: 2014-04-15
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..
Question by:sharris_glascol
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 65

Expert Comment

by:Jim Horn
ID: 39988988
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


Author Comment

ID: 39988993
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?
LVL 65

Expert Comment

by:Jim Horn
ID: 39989017
Not entirely sure what you mean by 'the same one', but give this a whirl..


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

Open in new window


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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.


Author Comment

ID: 39989044
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....
LVL 65

Expert Comment

by:Jim Horn
ID: 39989172
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..
   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

LVL 48

Accepted Solution

PortletPaul earned 500 total points
ID: 39990666
Both sample data and expected results are best practice when asking this type of question. At this point I think we have the expected result but can deduce the sample data.

Jim is ever so close but I believe you need


to match that expected result. Like this:
    CREATE TABLE Your_Table
    	([odd_no] int, [Oper_no] int, [empid] int) 
    INSERT INTO Your_Table
    	([odd_no], [Oper_no], [empid])
    	(12345, 10, 1234),
    	(12345, 10, 2321),
    	(13579, 10, 2321),
    	(2468, 20, 2321),
    	(2468, 20, 1234)

**Query 1**:

          odd_no                                           AS work_order
        , oper_no                                          AS operation_number
        , empid                                            AS employee_number
        , COUNT(empid) OVER (PARTITION BY odd_no, oper_no) AS empl_id_count
    FROM your_table

    |       2468 |               20 |            1234 |             2 |
    |       2468 |               20 |            2321 |             2 |
    |      12345 |               10 |            1234 |             2 |
    |      12345 |               10 |            2321 |             2 |
    |      13579 |               10 |            2321 |             1 |!3/4002c/1

Open in new window


Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question