带有连接的SQL Group By + Aggregate函数

时间:2017-12-23 06:29:01

标签: sql oracle join aggregate-functions

我有两张桌子: 1. EMPLOYEE_DATA及其列

EMP_ID  MANAGER_ID
9999      2222
8888      2222
7777      2222

另一张表是: 2.员工人数:

EMP_ID MANAGER_ID NO_OF_SWIPEINS ATTENDACE_DATE
9999   2222         1             23-DEC-2017
7777   2222         1             23-DEC-2017
9999   2222         1             23-DEC-2017
9999   2222         1             23-DEC-2017

我想要以下格式的数据:

EMP_ID        MANAGER_ID       TODAY_SWIPEINS
9999            2222                 3
8888            2222                 0
7777            2222                 1

我在下面尝试了查询:

    SELECT edata.EMP_ID, sum(rec.NO_OF_SWIPEINS) as TODAY_SWIPEINS,edata.MANAGER_ID FROM EMPLOYEE_RECORDS  rec 
right JOIN EMPLOYEE_DATA  edata
ON edata.MANAGER_ID = rec.MANAGER_ID 
and rec.MANAGER_ID='2222' and trunc(rec.ATTENDANCE_DATE)='23-DEC-2017'
group by edata.EMP_ID ,edata.MANAGER_ID;

但低于结果:

EMP_ID    TODAY_SWIPEINS      MANAGER_ID
8888           4                2222
7777           4                2222
9999           4                2222

有人请指导我做错了什么。

6 个答案:

答案 0 :(得分:1)

select EMP_ID,MANAGER_ID sum(NO_OF_SWIPEINS) as TODAY_SWIPEINS from EMPLOYEE_RECORDS group by EMP_ID;
  

您不需要像EMPLOYEE_RECORDS所包含的那样连接两个表   所有要操纵的数据。现在,因为您没有为Emp_ID 8888做准备   所以最后的查询不会显示8888。因此你需要   包括。因此,您的EMPLOYEE_RECORDS表必须按顺序显示   如上所述获得准确的输出。

EMP_ID MANAGER_ID NO_OF_SWIPEINS ATTENDACE_DATE
9999   2222         1             23-DEC-2017
7777   2222         1             23-DEC-2017
9999   2222         1             23-DEC-2017
9999   2222         1             23-DEC-2017
8888   2222         0             23-DEC-2017 

答案 1 :(得分:1)

您应该从employee_data中选择数据,然后加入表格2。

检查一下:

SELECT ED.MANAGER_ID, ED.EMP_ID, COUNT(NO_OF_SWIPEINS), IFNULL(ATTENDACE_DATE, '23-DEC-2017') as ATTENDACE_DATE  FROM EMPLOYEE_DATA ED
LEFT JOIN EMPLOYEE_RECORDS ER
ON ER.EMP_ID = ED.EMP_ID AND ATTENDACE_DATE = '23-DEC-2017'
WHERE ED.MANAGER_ID = 222 
GROUP BY ED.MANAGER_ID, ED.EMP_ID;

here isslqfiddle

答案 2 :(得分:0)

您只能加入MANAGER_ID而非加入EMP_ID。检查以下改进的查询。

SELECT edata.EMP_ID, edata.MANAGER_ID, sum(rec.NO_OF_SWIPEINS) as TODAY_SWIPEINS 
FROM 
EMPLOYEE_RECORDS rec right JOIN EMPLOYEE_DATA  edata
ON edata.MANAGER_ID = rec.MANAGER_ID and edata.EMP_ID = rec.EMP_ID
and rec.MANAGER_ID='2222' and trunc(rec.ATTENDANCE_DATE)='23-DEC-2017'
group by edata.EMP_ID ,edata.MANAGER_ID;

答案 3 :(得分:0)

以下是执行所需操作的查询,ATTENDANCE_DATE设置为当前日期。

SELECT B.EMP_ID, B.MANAGER_ID, COUNT(A.NO_OF_SWIPEINS) TODAY_SWIPEINS FROM 
(SELECT * FROM EMPLOYEE_RECORDS WHERE ATTENDACE_DATE=DATE_FORMAT(NOW(),'%d-%b-%Y')) A
RIGHT JOIN EMPLOYEE_DATA B
ON A.EMP_ID=B.EMP_ID
GROUP BY B.EMP_ID, B.MANAGER_ID;

以上是上述查询的SQL小提琴Demo

答案 4 :(得分:0)

你很亲密,但你为什么要加入manager_id?在计算NULL时需要SUM处理。此外,在与日期列TO_DATE

进行比较时,最好使用attendance_date
SELECT edata.emp_id,
       edata.manager_id,
       SUM(CASE
             WHEN rec.no_of_swipeins IS NULL THEN 0
             ELSE rec.no_of_swipeins
           END) AS TODAY_SWIPEINS
FROM   employee_records rec
       RIGHT JOIN employee_data edata
               ON edata.emp_id = rec.emp_id
                  AND rec.manager_id = '2222'
                  AND TRUNC(rec.attendance_date) =
                      To_date('23-DEC-2017', 'DD-MON-YYYY'
                      )
GROUP  BY edata.emp_id,
          edata.manager_id;  

DEMO

答案 5 :(得分:0)

SELECT edata.EMP_ID, nvl(sum(rec.NO_OF_SWIPEINS), 0) as TODAY_SWIPEINS,edata.MGR_ID 
FROM EMPONE  edata left JOIN MGRONE  rec
ON edata.EMP_ID = rec.EMP_ID 
group by edata.EMP_ID ,edata.MGR_ID