Solved

Query to obtain count from one table based on another table

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Remove () 10 41
Need more granular date groupings 4 45
SQLCMD question to have fields created as fixed length 2 36
VMware PVSCSI SQL Server 2016 AlwaysOn 2 37
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…
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.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

752 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