Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Getting a summation of results

User_OMEF8Jan 8 2021

Hello Everyone,
I have a working query and I am using a pivot. What I want to do is add another column that will sum my columns, but I am having trouble. Here is my query.

SELECT * FROM ( select upper(a.doc_type) as DOC_TYPE, to_char(a.date_time, 'yyyy-mm-dd') as DATES
         from tableA a
         where exists ( 
                select 1 from tableB b 
                where b.filename = 'ABCXYZ' and to_char(a.id) = b.id
                )
        ) pivot (  
             count(*)
             for(DATES) in ( '2021-01-01', '2021-01-02', '2021-01-03' )
            )
ORDER BY 1;

This is result that I get from the query.

DOC_TYPE | '2021-01-01' | '2021-01-02' | '2021-01-03'
TYPE_1   |           1  |           3  |           2
TYPE_2   |           10 |           20 |           5
TYPE_3   |          100 |          500 |          60

I would like to add the sum at the end and it should be a summation of 2021-01-01 through 2021-01-03.
Desired results would look like...

DOC_TYPE | '2021-01-01' | '2021-01-02' | '2021-01-03' | SUM_TOTAL
TYPE_1   |           1  |           3  |           2  | 6
TYPE_2   |           10 |           20 |           5  | 35
TYPE_3   |          100 |          500 |          60  | 660

I tried adding this to my query, but it gave me an error of "missing right parenthesis".

...
pivot (  
             count(*)
             for(DATES) in ( '2021-01-01', '2021-01-02', '2021-01-03' )
             sum( q'['2021-01-01']' +  q'['2021-01-02']' +  q'['2021-01-03']')
)
...

Also tried

...
pivot (  
             count(*)
             for(DATES) in ( '2021-01-01', '2021-01-02', '2021-01-03' )
             sum('''2021-01-01''' + '''2021-01-02''' + '''2021-01-03''')
)
...

I am assuming I am referencing the column wrong, but I am not sure how to reference it. Thanks in advance for all the help.

This post has been answered by Frank Kulash on Jan 8 2021
Jump to Answer

Comments

Post Details

Added on Jan 8 2021
10 comments
745 views