在 Oracle 中将列数据设为全年 Header 列

Make Column Data as Column Header whole year in Oracle

您好,我正在使用这样的示例数据在 oracle 上进行查询 [

预期结果但全年 即使那个月没有数据也会显示其他月份

我已经在互联网上搜索过这个问题,但找不到任何与我的问题相关的内容

这是我尝试过的方法,但我认为我的方法不对

select * from(
select fd.empid,fd.employeename,to_char(fd.payrolldate,'Month') as Month,fd.for_deduction15th,d.DEDUCTED15TH,fd.for_deduction30th,d.DEDUCTED30TH from 
VW_MTS_FOR_DEDUCTION FD
full outer join VW_MTS_DEDUCTED D
on FD.EMPID = D.EMPID
and FD.PAYROLLDATE = D.PAYROLLDATE
)
pivot(max(Month) for Month in ('March' ,'April','February' ))

您的 PIVOT 很接近,但您需要使用数据透视表中的“扣除”列。由于未共享表中的实际数据,因此下面是一个使用假定数据的示例查询。

--Setup of sample data
WITH
    VW_MTS_FOR_DEDUCTION (empid,
                          employeename,
                          payrolldate,
                          for_deduction15th,
                          for_deduction30th)
    AS
        (SELECT 233, 'HERNANDEZ, CECILIA ALVANO', DATE '2021-3-1', NULL, 2 FROM DUAL
         UNION ALL
         SELECT 233, 'HERNANDEZ, CECILIA ALVANO', DATE '2021-4-1', NULL, 1778 FROM DUAL
         UNION ALL
         SELECT 233, 'HERNANDEZ, CECILIA ALVANO', DATE '2021-4-1', 1, NULL FROM DUAL
         UNION ALL
         SELECT 40079, 'ALLASAS, DERRICK AMANTE', DATE '2021-4-1', NULL, 1 FROM DUAL),
    VW_MTS_DEDUCTED (empid,
                     payrolldate,
                     DEDUCTED15TH,
                     DEDUCTED30TH)
    AS
        (SELECT 233, DATE '2021-3-1', NULL, 1 FROM DUAL
         UNION ALL
         SELECT 233, DATE '2021-4-1', NULL, 1777 FROM DUAL
         UNION ALL
         SELECT 233, DATE '2021-4-1', 1, NULL FROM DUAL
         UNION ALL
         SELECT 40079, DATE '2021-4-1', NULL, 1 FROM DUAL)
  --Actual query
  SELECT *
    FROM (SELECT fd.empid,
                 fd.employeename,
                 TRIM (TO_CHAR (fd.payrolldate, 'Month'))     AS mon,
                 fd.for_deduction15th,
                 d.DEDUCTED15TH,
                 fd.for_deduction30th,
                 d.DEDUCTED30TH                               as
            FROM VW_MTS_FOR_DEDUCTION FD
                 LEFT JOIN VW_MTS_DEDUCTED D ON FD.EMPID = D.EMPID AND FD.PAYROLLDATE = D.PAYROLLDATE)
         PIVOT (MAX (for_deduction15th) AS for_deduction15th,
                MAX (DEDUCTED15TH) AS DEDUCTED15TH,
                MAX (for_deduction30th) AS for_deduction30th,
                MAX (DEDUCTED30TH) AS DEDUCTED30TH
               FOR mon
               IN ('March' AS march, 'April' AS april))
ORDER BY empid;
   EMPID                 EMPLOYEENAME    MARCH_FOR_DEDUCTION15TH    MARCH_DEDUCTED15TH    MARCH_FOR_DEDUCTION30TH    MARCH_DEDUCTED30TH    APRIL_FOR_DEDUCTION15TH    APRIL_DEDUCTED15TH    APRIL_FOR_DEDUCTION30TH    APRIL_DEDUCTED30TH
________ ____________________________ __________________________ _____________________ __________________________ _____________________ __________________________ _____________________ __________________________ _____________________
     233 HERNANDEZ, CECILIA ALVANO                                                                              2                     1                          1                     1                       1778                  1777
   40079 ALLASAS, DERRICK AMANTE                                                                                                                                                                                  1                     1