Solved

Query to obtain count from one table based on another table

Posted on 2014-07-20
2
145 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now