Возможно, существует более простое решение, но оно должно помочь вам начать работу -
WITH A AS (
SELECT
*
FROM
(
SELECT
CUSTOMER,
HOTEL,
BOOKED_STATUS
FROM
TABLE1
)
PIVOT ( COUNT ( HOTEL )
FOR HOTEL
IN ( 'ROYAL PALMS' AS "ROYAL PALMS",'BEVERLY HILLS' AS "BEVERLY HILLS",'RITZ-CARLTON' AS "RITZ-CARLTON" )
)
),B AS (
SELECT
CUSTOMER,
"ROYAL PALMS",
"BEVERLY HILLS",
"RITZ-CARLTON",
SUM("ROYAL PALMS" + "BEVERLY HILLS" + "RITZ-CARLTON") AS "GRAND TOTAL"
FROM
A
GROUP BY
CUSTOMER,
"ROYAL PALMS",
"BEVERLY HILLS",
"RITZ-CARLTON"
),C AS (
SELECT
CUSTOMER,
SUM("ROYAL PALMS" + "BEVERLY HILLS" + "RITZ-CARLTON") AS NBOOK
FROM
A
WHERE
BOOKED_STATUS IS NOT NULL
GROUP BY
CUSTOMER
),D AS (
SELECT
B.CUSTOMER,
SUM("ROYAL PALMS") AS "ROYAL PALMS",
SUM("BEVERLY HILLS") AS "BEVERLY HILLS",
SUM("RITZ-CARLTON") AS "RITZ-CARLTON",
SUM("GRAND TOTAL") AS "GRAND TOTAL",
NVL(C.NBOOK,0) AS NBOOK
FROM
B,
C
WHERE
B.CUSTOMER = C.CUSTOMER (+)
GROUP BY
B.CUSTOMER,
NVL(C.NBOOK,0)
) SELECT
*
FROM
D
UNION
SELECT
'GRAND TOTAL' AS CUSTOMER,
SUM("ROYAL PALMS"),
SUM("BEVERLY HILLS"),
SUM("RITZ-CARLTON"),
SUM("GRAND TOTAL") AS "GRAND TOTAL",
SUM(NBOOK) AS NBOOK
FROM
D
ORDER BY 5,1;
Вывод -
"CUSTOMER","ROYAL PALMS","BEVERLY HILLS","RITZ-CARLTON","GRAND TOTAL","NBOOK"
"MARILYN LAWSON",1,1,0,2,0
"JOHN SMITH",1,1,1,3,1
"SARAH ELLIOT",2,0,1,3,2
"GRAND TOTAL",4,2,2,8,3