CREATE OR REPLACE FORCE EDITIONABLE VIEW "OTHQ_OWN"."ARCHIVE_OSEARCH_WEBSERVICESEXECUTION_RABI_MONTHLY_VW" ("STYPE", "UNIQUE_COUNT_SCOMPID", "REQUESTS", "PERIOD", "METADATA_SOURCE") AS
SELECT
    STYPE,
    DISTINCT_COUNT_SCOMPID AS UNIQUE_COUNT_SCOMPID,
    DOC_COUNT AS REQUESTS,
    "TIMESTAMP" AS PERIOD,
    'csv' AS METADATA_SOURCE
FROM ARCHIVE_OSEARCH_WEBSERVICESEXECUTION_RABI_MONTHLY_CSV_IMPORTED_TB

UNION ALL
SELECT 
    STYPE,
    COUNT(DISTINCT SCOMPID) AS UNIQUE_COUNT_SCOMPID,
    SUM(DOC_COUNT) AS REQUESTS,
    "TIMESTAMP" AS PERIOD,
    'osearch' AS METADATA_SOURCE
FROM ARCHIVE_OSEARCH_WEBSERVICESEXECUTION_RABI_MONTHLY_TB osearch
WHERE NOT EXISTS (SELECT 1 FROM ARCHIVE_OSEARCH_WEBSERVICESEXECUTION_RABI_MONTHLY_CSV_IMPORTED_TB csv WHERE csv.STYPE = osearch.STYPE AND csv."TIMESTAMP" = osearch."TIMESTAMP")
GROUP BY STYPE, "TIMESTAMP";
