Solved

Query to obtain count from one table based on another table

Posted on 2014-07-20
2
159 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
[X]
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
2 Comments
 
LVL 49

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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how the fundamental information of how to create a table.

636 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