0

我有一个简单的查询,它返回我的基本数据:

SELECT b.MRID, r.GID, b.VDATE
FROM BAC b
LEFT JOIN BAR r ON b.MRID = r.RID

哪个输出:

MRID           GID  VDATE
John Smith     com  15-Oct-18
John Smith     com  NULL
Joe Bloggs     ad   02-Jun-18
Joe Bloggs     ad   14-Jul-18
Homer Simpson  bil  17-Oct-18
Homer Simpson  bil  NULL
Rick Grimes    zee  12-Nov-18
Rick Grimes    zee  NULL

我要输出的是动态 PIVOT:

MRID           GID  Current Month   Oct-18  Sep-18  Aug-18  Jul-18  Jun-18  May-18  Total
John Smith     com  0               1      0        0       0       0       0       1
Joe Bloggs     ad   0               0      0        0       1       1       0       2
Homer Simpson  bil  0               1      0        0       0       0       0       1
Rick Grimes    zee  1               0      0        0       0       0       0       1

我希望这随着月份与当前月份和总数的变化而动态变化。

4

1 回答 1

1

动态更改列名将很困难。有几个解决方案,但没有一个是你真正想要的

1) 执行“pivot XML”,在要旋转的值列表中指定 ANY。但它会给你一个 XML 的结果......不理想

2)做一个流水线函数接受一个将为您创建枢轴的光标。但它需要创建类型和存储过程

如果您接受将列名更改为 MONTH-1 之类的通用名称...您可以轻松地执行以下操作:

CREATE TABLE BAC ( MRID VARCHAR2(30), VDATE DATE );
CREATE TABLE BAR ( RID VARCHAR2(30), GID VARCHAR2(3));

INSERT INTO BAC VALUES ( 'John Smith',TO_DATE('15-Oct-18','DD-MON-YY'));
INSERT INTO BAC VALUES ( 'John Smith',null);
INSERT INTO BAC VALUES ( 'Joe Bloggs',TO_DATE('02-Jun-18','DD-MON-YY'));
INSERT INTO BAC VALUES ( 'Joe Bloggs',TO_DATE('14-Jul-18','DD-MON-YY'));
INSERT INTO BAC VALUES ( 'Homer Simpson',TO_DATE('17-Oct-18','DD-MON-YY'));
INSERT INTO BAC VALUES ( 'Homer Simpson',null);
INSERT INTO BAC VALUES ( 'Rick Grimes',TO_DATE('12-Nov-18','DD-MON-YY'));
INSERT INTO BAC VALUES ( 'Rick Grimes',null);

INSERT INTO BAR VALUES ( 'John Smith','com');
INSERT INTO BAR VALUES ( 'Joe Bloggs','ad');
INSERT INTO BAR VALUES ( 'Homer Simpson','bil');
INSERT INTO BAR VALUES ( 'Rick Grimes','zee');

WITH MONTHS AS 
(
    SELECT ADD_MONTHS(TRUNC(SYSDATE,'MONTH'),-LEVEL+1) AS MONTH, DECODE(LEVEL,1,'CURRENT_MONTH','MONTH_MINUS_'||(LEVEL-1)) AS MONTH_NAME FROM DUAL CONNECT BY LEVEL <=7
)
SELECT 
        MRID, 
        GID,
        NVL(CURRENT_MONTH,0) AS CURRENT_MONTH,
        NVL(MONTH_MINUS_1,0) AS MONTH_MINUS_1,
        NVL(MONTH_MINUS_2,0) AS MONTH_MINUS_2,
        NVL(MONTH_MINUS_3,0) AS MONTH_MINUS_3,
        NVL(MONTH_MINUS_4,0) AS MONTH_MINUS_4,
        NVL(MONTH_MINUS_5,0) AS MONTH_MINUS_5,
        NVL(MONTH_MINUS_6,0) AS MONTH_MINUS_6,
        TOTAL 
FROM (
    SELECT 
        DISTINCT B.MRID, R.GID, MONTH_NAME, 
        COUNT(*) OVER (PARTITION BY B.MRID, R.GID, TRUNC(VDATE,'MONTH') ORDER BY VDATE ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS CNT, 
        COUNT(*) OVER (PARTITION BY B.MRID, R.GID ORDER BY VDATE ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS TOTAL
    FROM MONTHS M
    JOIN BAC B ON M.MONTH=TRUNC(B.VDATE,'MONTH')
    LEFT JOIN BAR R ON b.MRID = r.RID 
) PIVOT 
( SUM(CNT) 
    FOR MONTH_NAME IN 
        ('CURRENT_MONTH' AS CURRENT_MONTH ,
        'MONTH_MINUS_1' AS MONTH_MINUS_1,
        'MONTH_MINUS_2' AS MONTH_MINUS_2,
        'MONTH_MINUS_3' AS MONTH_MINUS_3,
        'MONTH_MINUS_4' AS MONTH_MINUS_4,
        'MONTH_MINUS_5' AS MONTH_MINUS_5,
        'MONTH_MINUS_6' AS MONTH_MINUS_6)
);

MRID                           GID CURRENT_MONTH MONTH_MINUS_1 MONTH_MINUS_2 MONTH_MINUS_3 MONTH_MINUS_4 MONTH_MINUS_5 MONTH_MINUS_6      TOTAL
------------------------------ --- ------------- ------------- ------------- ------------- ------------- ------------- ------------- ----------
John Smith                     com             0             1             0             0             0             0             0          1
Rick Grimes                    zee             1             0             0             0             0             0             0          1
Homer Simpson                  bil             0             1             0             0             0             0             0          1
Joe Bloggs                     ad              0             0             0             0             1             1             0          2
于 2018-11-28T13:35:09.303 回答