Oracle SQL Dynamic PIVOT Current month, last 6 Months and a total
I have a simple query which returns my base data:
SELECT b.MRID, r.GID, b.VDATE
FROM BAC b
LEFT JOIN BAR r ON b.MRID = r.RID
Which outputs:
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
What I want to output is a Dynamic 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
I want this to dynamically change as the months move on with current month and a total.
sql oracle pivot dynamic-pivot
add a comment |
I have a simple query which returns my base data:
SELECT b.MRID, r.GID, b.VDATE
FROM BAC b
LEFT JOIN BAR r ON b.MRID = r.RID
Which outputs:
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
What I want to output is a Dynamic 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
I want this to dynamically change as the months move on with current month and a total.
sql oracle pivot dynamic-pivot
add a comment |
I have a simple query which returns my base data:
SELECT b.MRID, r.GID, b.VDATE
FROM BAC b
LEFT JOIN BAR r ON b.MRID = r.RID
Which outputs:
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
What I want to output is a Dynamic 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
I want this to dynamically change as the months move on with current month and a total.
sql oracle pivot dynamic-pivot
I have a simple query which returns my base data:
SELECT b.MRID, r.GID, b.VDATE
FROM BAC b
LEFT JOIN BAR r ON b.MRID = r.RID
Which outputs:
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
What I want to output is a Dynamic 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
I want this to dynamically change as the months move on with current month and a total.
sql oracle pivot dynamic-pivot
sql oracle pivot dynamic-pivot
edited Nov 27 '18 at 13:33
Matt
asked Nov 27 '18 at 11:55
MattMatt
10.8k216898
10.8k216898
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Having your column name change dynamically will be difficult. There are a couple of solutions out there but none of them are really what you want
1) Doing an "pivot XML" with specifying ANY in the list of values you want to pivot ON. But it will give you the result as an XML which is ... not ideal
2) Doing a PIPELINED function accepting a cursor which will create the pivot for you. But it requires creating types and stored procedures
If you accept to change your column names to something Generic like MONTH-1 ... you can easily do it as below :
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
Hmm, getting an error on the first count lineORA-00907: missing right parenthesis
– Matt
Nov 28 '18 at 13:57
Weird. I just ran it on my 11.2 server and it works like a charm. what is your version ?
– LauDec
Nov 28 '18 at 14:03
My fault, typo :)
– Matt
Nov 28 '18 at 14:05
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53499132%2foracle-sql-dynamic-pivot-current-month-last-6-months-and-a-total%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Having your column name change dynamically will be difficult. There are a couple of solutions out there but none of them are really what you want
1) Doing an "pivot XML" with specifying ANY in the list of values you want to pivot ON. But it will give you the result as an XML which is ... not ideal
2) Doing a PIPELINED function accepting a cursor which will create the pivot for you. But it requires creating types and stored procedures
If you accept to change your column names to something Generic like MONTH-1 ... you can easily do it as below :
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
Hmm, getting an error on the first count lineORA-00907: missing right parenthesis
– Matt
Nov 28 '18 at 13:57
Weird. I just ran it on my 11.2 server and it works like a charm. what is your version ?
– LauDec
Nov 28 '18 at 14:03
My fault, typo :)
– Matt
Nov 28 '18 at 14:05
add a comment |
Having your column name change dynamically will be difficult. There are a couple of solutions out there but none of them are really what you want
1) Doing an "pivot XML" with specifying ANY in the list of values you want to pivot ON. But it will give you the result as an XML which is ... not ideal
2) Doing a PIPELINED function accepting a cursor which will create the pivot for you. But it requires creating types and stored procedures
If you accept to change your column names to something Generic like MONTH-1 ... you can easily do it as below :
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
Hmm, getting an error on the first count lineORA-00907: missing right parenthesis
– Matt
Nov 28 '18 at 13:57
Weird. I just ran it on my 11.2 server and it works like a charm. what is your version ?
– LauDec
Nov 28 '18 at 14:03
My fault, typo :)
– Matt
Nov 28 '18 at 14:05
add a comment |
Having your column name change dynamically will be difficult. There are a couple of solutions out there but none of them are really what you want
1) Doing an "pivot XML" with specifying ANY in the list of values you want to pivot ON. But it will give you the result as an XML which is ... not ideal
2) Doing a PIPELINED function accepting a cursor which will create the pivot for you. But it requires creating types and stored procedures
If you accept to change your column names to something Generic like MONTH-1 ... you can easily do it as below :
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
Having your column name change dynamically will be difficult. There are a couple of solutions out there but none of them are really what you want
1) Doing an "pivot XML" with specifying ANY in the list of values you want to pivot ON. But it will give you the result as an XML which is ... not ideal
2) Doing a PIPELINED function accepting a cursor which will create the pivot for you. But it requires creating types and stored procedures
If you accept to change your column names to something Generic like MONTH-1 ... you can easily do it as below :
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
answered Nov 28 '18 at 13:35
LauDecLauDec
47339
47339
Hmm, getting an error on the first count lineORA-00907: missing right parenthesis
– Matt
Nov 28 '18 at 13:57
Weird. I just ran it on my 11.2 server and it works like a charm. what is your version ?
– LauDec
Nov 28 '18 at 14:03
My fault, typo :)
– Matt
Nov 28 '18 at 14:05
add a comment |
Hmm, getting an error on the first count lineORA-00907: missing right parenthesis
– Matt
Nov 28 '18 at 13:57
Weird. I just ran it on my 11.2 server and it works like a charm. what is your version ?
– LauDec
Nov 28 '18 at 14:03
My fault, typo :)
– Matt
Nov 28 '18 at 14:05
Hmm, getting an error on the first count line
ORA-00907: missing right parenthesis– Matt
Nov 28 '18 at 13:57
Hmm, getting an error on the first count line
ORA-00907: missing right parenthesis– Matt
Nov 28 '18 at 13:57
Weird. I just ran it on my 11.2 server and it works like a charm. what is your version ?
– LauDec
Nov 28 '18 at 14:03
Weird. I just ran it on my 11.2 server and it works like a charm. what is your version ?
– LauDec
Nov 28 '18 at 14:03
My fault, typo :)
– Matt
Nov 28 '18 at 14:05
My fault, typo :)
– Matt
Nov 28 '18 at 14:05
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53499132%2foracle-sql-dynamic-pivot-current-month-last-6-months-and-a-total%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown