Count after joining multiple tables and counting multiple column values

Please help me with the problem below.

table 1 employee information

emp name          empno.
---------------------------------
John               1234

Joe                6789

table 2 appointment of an employee

empno  assignmentstartdate assignmentenddate assignmentID  empassignmentID
-----------------------------------------------------------------------------
1234      01JAN2017            02JAN2017         A1            X1

6789      01jan2017            02JAN2017         B1            Z1

table 3 employee assignment property

empassignmentID   assignmentID  propertyname        propertyvalue
-------------------------------------------------------------------
X1                  A1           COMPLETED           true

X1                  A1           STARTED             true

Z1                  B1           STARTED             true

Z1                  B1           COMPLETED           false

Required result: (number of completed and begun for each employee)

emp name   emp no.  COMPLETED   STARTED
------------------------------------------
John       1234      1           1

Joe        6789      0           1

Currently, with my request, it does not correctly assign the propertyvalue to the account, if I run for one employee, it works correctly, but not for several employees. Please help.

 SELECT empno ,
  empname     ,
 (SELECT COUNT(A.propertyvalue)
 FROM employeedetails C ,
 employees_ASSIGNMENT RCA,
 employee_assignment_property A
 WHERE TRUNC(startdate) >= '14jun2017'
 AND TRUNC(endate)        <= '20jun2017'
 AND RCA.empno             = C.empno
 AND RCA.empassignmetid    = A.empassignmetid
 AND rca.EMPNO            IN ('1234','6789')
  AND RCA.assignmentid      = A.assignmentid
  AND A.Name                = 'COMPLETED'
  AND A.propertyvalue       = 'true') ,
  (SELECT COUNT(A.propertyvalue)
   FROM employeedetails C ,
   employees_ASSIGNMENT RCA,
  employee_assignment_property A
  WHERE TRUNC(startdate) >= '14jun2017'
  AND TRUNC(endate)        <= '20jun2017'
  AND RCA.empno             = C.empno
  AND RCA.empassignmetid    = A.empassignmetid
 AND rca.EMPNO            IN ('1234','6789')
 AND RCA.assignmentid      = A.assignmentid
 AND A.Name                = 'STARTED'
 AND A.propertyvalue       = 'true')FROM employeedetails  WHERE EMPNO IN 
 ('1234','6789') GROUP BY C.empno ,
  C.EMPNAME
+4
source share
4 answers

if you want the result to be the result of a query without a CTE, this should work:

  select empName,
       empNo,   
        (select employee_details.empNo, count(employee_assignment.assId) 
        from employee_details as t1
        join employee_assignment on (t1.empno = employee_assignment.empno)
        join employee_assignment_property on (employee_assignment.assId = employee_assignment_property.assId)
        where employee_assignment.ptop = 'COMPLETED'
                and t.empNo = t1.empNo
        group by t1.empNo ) as [COMPLETED],

        (select employee_details.empNo, count(employee_assignment.assId) 
        from employee_details as t1
        join employee_assignment on (t1.empno = employee_assignment.empno)
        join employee_assignment_property on (employee_assignment.assId = employee_assignment_property.assId)
        where employee_assignment.ptop = 'STARTED'
                and t.empNo = t1.empNo
        group by t1.empNo ) as [STARTED],       
from employee_details as t
+1
source

I think you are just looking for this:

SELECT      DET.empname     
,           COUNT(CASE WHEN PROP.propertyname = 'COMPLETED' THEN 1 END) COMP_COUNT
,           COUNT(CASE WHEN PROP.propertyname = 'STARTED' THEN 1 END) START_COUNT
FROM        employeedetails DET
INNER JOIN  employees_ASSIGNMENT ASS
        ON  ASS.empno = DET.empno
INNER JOIN  employee_assignment_property PROP
        ON  PROP.empassignmentID = ASS.empassignmentID   
        AND PROP.assignmentID  = ASS.assignmentID
GROUP BY    DET.empname 

WHERE, .

+2

If you do not want to execute a dirty query consisting of subqueries, you can try to create one view(if your database allows it).

What this means: I will be useless before that . Thus, the view is a temporary table.

Hope this helps

0
source

this should work using CTE: Using common table expressions

with numComplet()
as 
(
select tbl1.empNo, count(tbl2.assId) 
from tbl1 
join tbl2 on (tbl1.empno = tbl2.empno)
join tbl3 on (tbl2.assId = tbl3.assId)
where tbl2.ptop = 'COMPLETED'
group by tbl1.empNo
),


with numStarted()
as 
(
select tbl1.empNo, count(tbl2.assId) 
from tbl1 
join tbl2 on (tbl1.empno = tbl2.empno)
join tbl3 on (tbl2.assId = tbl3.assId)
where tbl2.ptop = 'STARTED'
group by tbl1.empNo
)

select * 
from tbl1 
join numComplet on (tbl1.empNo = numComplet.empNo)
join numStarted on (tbl1.empNo = numStarted.empNo)

I specify the table names as tbl [1 | 2 | 3]

0
source

Source: https://habr.com/ru/post/1680120/


All Articles