I am newbie with oracle pl/sql,
right now using oracle sqldeveloper,
i need help for my homework,
how to make the column number 2, 3, 4, 5, 6, … count based on first value column, the DISTINCT OWNER ?
can anyone help me? or suggest me with some hint?
SELECT DISTINCT OWNER as SCHEMA, (SELECT COUNT(DISTINCT OBJECT_NAME) FROM DBA_OBJECTS where object_type = 'TABLE') "TABLE", (SELECT COUNT(DISTINCT OBJECT_NAME) FROM DBA_OBJECTS where object_type = 'VIEW') "VIEW", (SELECT COUNT(DISTINCT OBJECT_NAME) FROM DBA_OBJECTS where object_type = 'MATERIALIZED_VIEW') "MATERIALIZED VIEW", (SELECT COUNT(DISTINCT OBJECT_NAME) FROM DBA_OBJECTS where object_type = 'INDEX') "INDEX", (SELECT COUNT(DISTINCT OBJECT_NAME) FROM DBA_OBJECTS where object_type = 'SEQUENCE') "SEQUENCE", (SELECT COUNT(DISTINCT OBJECT_NAME) FROM DBA_OBJECTS where object_type = 'TYPE') "TYPE", (SELECT COUNT(DISTINCT OBJECT_NAME) FROM DBA_OBJECTS where object_type = 'SYNONYM') "SYNONYM", (SELECT COUNT(DISTINCT OBJECT_NAME) FROM DBA_OBJECTS where object_type = 'DATABASE LINK') "DATABASE LINK", (SELECT COUNT(DISTINCT OBJECT_NAME) FROM DBA_OBJECTS where object_type = 'LOB') "LOB", (SELECT COUNT(DISTINCT OBJECT_NAME) FROM DBA_OBJECTS where object_type = 'JOB') "JOB", (SELECT COUNT(DISTINCT OBJECT_NAME) FROM DBA_OBJECTS where object_type = 'TRIGGER') "TRIGGER", (SELECT COUNT(DISTINCT OBJECT_NAME) FROM DBA_OBJECTS where object_type = 'PROCEDURE') "PROCEDURE", (SELECT COUNT(DISTINCT OBJECT_NAME) FROM DBA_OBJECTS where object_type = 'FUNCTION') "FUNCTION", (SELECT COUNT(DISTINCT OBJECT_NAME) FROM DBA_OBJECTS where object_type = 'PACKAGE') "PACKAGE", (SELECT COUNT(DISTINCT OBJECT_NAME) FROM DBA_OBJECTS where object_type = 'PACKAGE BODY') "PACKAGE BODY" FROM DBA_OBJECTS WHERE OWNER IN ('BERKAS', 'BIAYAPERJALANAN', 'DASHBOARD', 'DIKLAT', 'FLATKKP', 'INVENT', 'KKPWEB', 'KKPWEBAUDITOR', 'KONTEN', 'LP2B', 'LPPB', 'MITRAKERJA', 'MOBILE', 'OLTPUSER', 'ONEDEE', 'PEGAWAI', 'PERENCANAAN', 'PETA', 'PM', 'PNBP', 'PNBPTRAIN', 'SIMPEG_1703', 'SIMPEG_2702', 'SIPT', 'SKP', 'SURAT', 'SURATBDG', 'TEMATIK', 'TIMKENDALI', 'WEBAPIDATA', 'WILAYAH') ORDER BY OWNER;
this is wrong, i need the count n other column based on the first column what should i do ?
Advertisement
Answer
You should use conditional aggregation as following:
SELECT OWNER as SCHEMA, COALESCE(SUM(CASE WHEN object_type = 'TABLE' THEN 1 END), 0) "TABLE", COALESCE(SUM(CASE WHEN object_type = 'VIEW' THEN 1 END), 0) "VIEW", COALESCE(SUM(CASE WHEN object_type = 'MATERIALIZED_VIEW' THEN 1 END), 0) "MATERIALIZED VIEW", COALESCE(SUM(CASE WHEN object_type = 'INDEX' THEN 1 END), 0) "INDEX", COALESCE(SUM(CASE WHEN object_type = 'SEQUENCE' THEN 1 END), 0) "SEQUENCE", COALESCE(SUM(CASE WHEN object_type = 'TYPE' THEN 1 END), 0) "TYPE", COALESCE(SUM(CASE WHEN object_type = 'SYNONYM' THEN 1 END), 0) "SYNONYM", COALESCE(SUM(CASE WHEN object_type = 'DATABASE LINK' THEN 1 END), 0) "DATABASE LINK", COALESCE(SUM(CASE WHEN object_type = 'LOB' THEN 1 END), 0) "LOB", COALESCE(SUM(CASE WHEN object_type = 'JOB' THEN 1 END), 0) "JOB", COALESCE(SUM(CASE WHEN object_type = 'TRIGGER' THEN 1 END), 0) "TRIGGER", COALESCE(SUM(CASE WHEN object_type = 'PROCEDURE' THEN 1 END), 0) "PROCEDURE", COALESCE(SUM(CASE WHEN object_type = 'FUNCTION' THEN 1 END), 0) "FUNCTION", COALESCE(SUM(CASE WHEN object_type = 'PACKAGE' THEN 1 END), 0) "PACKAGE", COALESCE(SUM(CASE WHEN object_type = 'PACKAGE BODY' THEN 1 END), 0) "PACKAGE BODY" FROM DBA_OBJECTS WHERE OWNER IN ('BERKAS', 'BIAYAPERJALANAN', 'DASHBOARD', 'DIKLAT', 'FLATKKP', 'INVENT', 'KKPWEB', 'KKPWEBAUDITOR', 'KONTEN', 'LP2B', 'LPPB', 'MITRAKERJA', 'MOBILE', 'OLTPUSER', 'ONEDEE', 'PEGAWAI', 'PERENCANAAN', 'PETA', 'PM', 'PNBP', 'PNBPTRAIN', 'SIMPEG_1703', 'SIMPEG_2702', 'SIPT', 'SKP', 'SURAT', 'SURATBDG', 'TEMATIK', 'TIMKENDALI', 'WEBAPIDATA', 'WILAYAH') GROUP BY OWNER ORDER BY OWNER;
Cheers!!