连接多个表并计算多个列值后计数

时间:2017-06-26 13:52:29

标签: sql oracle12c

请帮助我解决以下问题。

表1员工详细信息

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

Joe                6789

表2员工分配

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

6789      01jan2017            02JAN2017         B1            Z1

表3员工转让财产

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

X1                  A1           STARTED             true

Z1                  B1           STARTED             true

Z1                  B1           COMPLETED           false

需要的结果:(每位员工的已完成和已开始计数)

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

Joe        6789      0           1

目前我的查询并没有为propertyvalue正确计算,如果我为一个员工运行它正常工作但不适用于多个员工。 请帮忙。

 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 个答案:

答案 0 :(得分:2)

我认为你只是在寻找这个:

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子句。

答案 1 :(得分:1)

如果您希望将结果作为没有CTE的查询,则应该起作用:

  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

答案 2 :(得分:0)

如果您不想进行由子查询组成的脏查询,可以尝试创建view(如果您的数据库允许)。

这是什么意思:我在this面前毫无用处。总之,视图是一个临时表。

希望这有帮助

答案 3 :(得分:0)

这应该使用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)

我把表名称为tbl [1 | 2 | 3]