Solved

Query to obtain count from one table based on another table

Posted on 2014-07-20
2
151 Views
Last Modified: 2014-07-25
Table 1:

EmpID  PositionID  CompanyID
12          1                123
13          1                 123
14          2                  123
15          3                  123

table 2

PositionID     test1eligibility     test2eligibility      test3eligibility
1                      1                           1                           0
2                       1                           0                          0
3                       0                             0                         1

I want to know how many empID's in table1 are eligible for test1 (that is test1eligibility =1), how many are eligible for test2 and how many are eligible for test3.
so the result i want is:

companyID     test1eligibility     test2eligibility      test3eligibility
123                      3                       2                              1

can you please help with the SQL query?
0
Comment
Question by:Angel02
2 Comments
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 40208267
SELECT
      t1.CompanyID
    , SUM(t2.test1eligibility) AS test1eligibility
    , SUM(t2.test2eligibility) AS test2eligibility
    , SUM(t2.test3eligibility) AS test3eligibility
FROM table1 t1
      INNER JOIN table2 t2
                  ON t1.PositionID = t2.PositionID
GROUP BY
      t1.CompanyID

Open in new window

{+ edit} nb: because you display zeros and not nulls in table2 don't use count(), better to use sum() in this situation
0
 

Author Comment

by:Angel02
ID: 40220504
Brilliant idea!
I just had to change boolean to integer for SUM. Thank you.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

809 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