Hello everyone,
I have a problem with using the pivot command (well, I've several, but this is the one that's stopping me).
I want to get a pivot of users, service requests and how many they have undertaken. I would like a total per user and a total per service request.
I can create a query that will give me a result set that has each user (one per row) and each service request (one per column) and the total number of each service requests per user.
However when I try to change my SQL using any of the examples I have to generate a total it all goes to pot.
Here is my working code:-
COLUMN concatList new_value str_column_list noprint
SELECT LISTAGG(''''||ID_SERVICE_REQUEST_TYPE||'''',',')
WITHIN GROUP(ORDER BY ID_SERVICE_REQUEST_TYPE DESC) as concatList
FROM SERVICE_REQUEST_TYPE
WHERE ARCHIVE_FLAG = 'N'
;
COLUMN concatTotal new_value str_column_add noprint
SELECT LISTAGG(''''||ID_SERVICE_REQUEST_TYPE||'''','+')
WITHIN GROUP(ORDER BY ID_SERVICE_REQUEST_TYPE DESC) as concatTotal
FROM SERVICE_REQUEST_TYPE
WHERE ARCHIVE_FLAG = 'N'
;
SELECT * -- SELECT TEAM_MEMBER, &str_column_list, &str_column_add
FROM
(SELECT
(USER_DETAILS.FIRST_NAME||' '||USER_DETAILS.LAST_NAME) AS TEAM_MEMBER,
SERVICE_REQUEST_TYPE.ID_SERVICE_REQUEST_TYPE AS SRQ_TYPE,
1 AS COUNTER
FROM SERVICE_REQUEST,
SERVICE_REQUEST_TYPE,
USER_DETAILS
WHERE SERVICE_REQUEST.STATUS = 110
AND USER_DETAILS.STATUS <> 105
AND SERVICE_REQUEST.ID_SERVICE_REQUEST_TYPE=SERVICE_REQUEST_TYPE.ID_SERVICE_REQUEST_TYPE
AND SERVICE_REQUEST.COMPLETED_BY=USER_DETAILS.ID_USER_DETAILS
)
PIVOT
(
SUM(COUNTER)
FOR SRQ_TYPE in (&str_column_list)
)
;
My working results are (slightly truncated for the sake of brevity)
TEAM_MEMBER '70' '27' '26' '21' '20' '17' '16' '15' '10' '9' '8' '7' '6' '5' '4' '3' '2'
--------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
Rod 1 7 1 2 3
Jane 43 1 4 3 2
Fred 4 519 509 221 31 1 233 120 107
Joshua 1 5 27
Samantha 2 2
Because service_request_type is a dynamic (and long) list I've got the first part of the code to generate &str_column_list. I generated &str_column_total with the idea of changing the SELECT statement in line 13 to generate the per row total, but when I do that all I get in its place is the title of the SERVICE_REQUEST_TYPE_ID for each row. (you can see the altered code commented out in line 13).
This is what I get.
TEAM_MEMBER '70' '27' '26' '21' '20' '17' '16' '15' '10' '9' '8' '7' '6' '5' '4' '3' '2' (Edited column name)
--------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------------------
Rod '70' '27' '26' '21' '20' '17' '16' '15' '10' '9' '8' '7' '6' '5' '4' '3' '2' 819
Jane '70' '27' '26' '21' '20' '17' '16' '15' '10' '9' '8' '7' '6' '5' '4' '3' '2' 819
Fred '70' '27' '26' '21' '20' '17' '16' '15' '10' '9' '8' '7' '6' '5' '4' '3' '2' 819
Joshua '70' '27' '26' '21' '20' '17' '16' '15' '10' '9' '8' '7' '6' '5' '4' '3' '2' 819
Samantha '70' '27' '26' '21' '20' '17' '16' '15' '10' '9' '8' '7' '6' '5' '4' '3' '2' 819
(Edited column name) is '70'+'27'+.....+'2', which is what I would expect as it's &str_column_add.
Anyone have any pointers as to where am I going wrong?
The reason I've got numbers rather than text for the row titles is that the text titles I have are too large to use for column titles. That's a different problem I'm going to work on. I've also not got a per column total working yet but that's my next trial.
Any help would be gratefully appreciated.