在特定月份没有交易时,一个月不打印

时间:2019-07-16 13:34:59

标签: postgresql

我写了一个代码来获取员工流失的详细信息,显示员工在月份,开始,加入,离开和结束的次数。

这里的问题是,如果以上四列中没有任何值,则系统不会生成月份。

请提出解决方案。

输出:

 yyear | mmonth | charmonth | opening | incoming | relived | closing 
-------+--------+-----------+---------+----------+---------+---------
  2018 |      4 | Apr-18    |      14 |        2 |       0 |      16
  2018 |      5 | May-18    |      16 |        1 |       0 |      17
  2018 |      8 | Aug-18    |      17 |        3 |       0 |      20
  2018 |      9 | Sep-18    |      20 |        1 |       0 |      21
  2018 |     10 | Oct-18    |      21 |       23 |       4 |      40
  2018 |     11 | Nov-18    |      40 |        5 |       1 |      44
  2018 |     12 | Dec-18    |      44 |        2 |       0 |      46
  2019 |      1 | Jan-19    |      46 |        1 |       0 |      47
  2019 |      2 | Feb-19    |      47 |        1 |       0 |      48
  2019 |      3 | Mar-19    |      48 |        6 |       1 |      53
  2019 |      4 | Apr-19    |      53 |        1 |       0 |      54
  2019 |      5 | May-19    |      54 |        3 |       1 |      56
  2019 |      6 | Jun-19    |      56 |        2 |       0 |      58
(13 rows)

如果您看到6月18日,7月18日的月份序列丢失。

代码:

WITH table_1 AS ( 
    select 
        startdate as ddate, 
        enddate as lastday,
        extract('month' from startdate) as mmonth, 
        extract('year' from startdate) as yyear,
        to_char(to_timestamp(startdate),'Mon-YY') as months 
    from shr_period 
    where startdate >= DATE('2018-01-01') 
        and enddate <= DATE('2019-07-01') 
        and ad_org_id = 'C9D035B52FAF46329D9654B1ECA0289F' 
) 
SELECT 
    table_1.yyear, 
    table_1.mmonth, 
    table_1.months as charmonth, 
    (SELECT 
         COUNT(*) 
     FROM shr_emp_job OPENING 
     WHERE OPENING.dateofjoining < table_1.ddate 
         and OPENING.relieveddate is null 
         and OPENING.ad_org_id = 'C9D035B52FAF46329D9654B1ECA0289F'
    ) AS OPENING,  
    count(*) as incoming, 
    (select count(*) 
     from shr_emp_job rel 
     where rel.relieveddate is not null 
         and rel.dateofjoining <= table_1.lastday 
         and rel.dateofjoining >= table_1.ddate 
         and rel.ad_org_id = 'C9D035B52FAF46329D9654B1ECA0289F'
     ) as relived, 
     (SELECT COUNT(*) 
      FROM shr_emp_job CLOSING 
      WHERE CLOSING.dateofjoining <= table_1.lastday 
          and relieveddate is null 
          and CLOSING.ad_org_id = 'C9D035B52FAF46329D9654B1ECA0289F'
     ) AS CLOSING 
FROM 
   shr_emp_job       
JOIN table_1 ON table_1.mmonth = extract('month' from shr_emp_job.dateofjoining) 
    AND table_1.yyear = extract('year' from shr_emp_job.dateofjoining)
where shr_emp_job.ad_org_id = 'C9D035B52FAF46329D9654B1ECA0289F' 
GROUP BY table_1.mmonth, table_1.yyear, table_1.ddate, table_1.lastday, charmonth 
ORDER BY table_1.yyear, table_1.mmonth; 

1 个答案:

答案 0 :(得分:0)

快速浏览一下,尝试将JOIN从内部联接更改为外部联接。所以代替

FROM 
   shr_emp_job       
JOIN table_1 ON 

FROM 
   shr_emp_job       
RIGHT OUTER JOIN table_1 ON 

这告诉Postgres即使在左边提到的表(shr_emp_job)中没有匹配的值,也要保留右边提到的表(table_1)中的选定列。对于这些条件,缺少的值将提供NULL。