This works for me - I tested on an 18.1 db but this functionality is already in 11g I believe
WITH test_data(dt, invoice, amt, type)
AS
(
SELECT '01-DEC-20', '10001' ,50 ,'TAKEAWAY' FROM DUAL UNION ALL
SELECT '01-DEC-20', '10002' ,50 ,'DELIVERY' FROM DUAL UNION ALL
SELECT '01-DEC-20', '10003' ,50 ,'DINEIN' FROM DUAL UNION ALL
SELECT '01-DEC-20', '10004' ,50 ,'TAKEAWAY' FROM DUAL UNION ALL
SELECT '01-DEC-20', '10005' ,50 ,'DELIVERY' FROM DUAL UNION ALL
SELECT '01-DEC-20', '10006' ,50 ,'DINEIN' FROM DUAL UNION ALL
SELECT '02-DEC-20', '10001' ,100 ,'TAKEAWAY' FROM DUAL UNION ALL
SELECT '02-DEC-20', '10002' ,100 ,'DELIVERY' FROM DUAL UNION ALL
SELECT '02-DEC-20', '10003' ,100 ,'DINEIN' FROM DUAL UNION ALL
SELECT '02-DEC-20', '10004' ,100 ,'TAKEAWAY' FROM DUAL UNION ALL
SELECT '02-DEC-20', '10005' ,100 ,'DELIVERY' FROM DUAL UNION ALL
SELECT '02-DEC-20', '10006' ,100 ,'DINEIN' FROM DUAL
)
SELECT
distinct dt,
SUM(AMT) OVER (PARTITION BY dt) as total_sales,
type,
SUM(AMT) OVER (PARTITION BY dt,type) as subtotal_type,
to_char(SUM(AMT) OVER (PARTITION BY dt,type)/SUM(AMT) OVER (PARTITION BY dt)*100,'990.00')||'%'
FROM test_data
ORDER BY dt;
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…