-- SELECT count(*) FROM EXT_TITEL_INFO
-- SELECT * FROM EXT_TITEL_INFO
set timing on

-- SELECT count(*) FROM EXT_BEDPBED;

-- PROMPT No Hashing allowed...
-- SELECT /*+ NO_USE_HASH(bedp t_info) */
-- bedp.BEDARFNR,
-- bedp.BEDP_SEQUENZ,
-- bedp.BEDP_TITELNR,
-- bedp.BEDP_MAN,
-- bedp.BEDP_MENGE_BEDARF_VM,
-- t_info.MELDENUMMER,
-- t_info.MENGE_VORMERKER
-- FROM EXT_BEDPBED bedp
-- LEFT JOIN EXT_TITEL_INFO t_info
--     ON bedp.BEDP_TITELNR = t_info.TI_NUMMER
--     AND bedp.BEDP_MAN = t_info.MANDFUEHR;
-- PROMPT ####################################

-- PROMPT >>>>>>>>> All allowed
-- SELECT COUNT(*) FROM (
--     SELECT
--         bedp.BEDARFNR,
--         bedp.BEDP_SEQUENZ,
--         bedp.BEDP_TITELNR,
--         bedp.BEDP_MAN,
--         bedp.BEDP_MENGE_BEDARF_VM,
--         t_info.MELDENUMMER,
--         t_info.VERLAGSNR
--         t_info.MENGE_VORMERKER
--         t_info.MANDFUEHR
--     FROM EXT_BEDPBED bedp
--     LEFT JOIN EXT_TITEL_INFO t_info
--         ON bedp.BEDP_TITELNR = t_info.TI_NUMMER
-- );
    
-- -- WHERE bedp.BEDP_MAN IN (1, 90) AND t_info.MELDENUMMER != 26;
-- PROMPT ######################################
PROMPT #################################################
-- SELECT * FROM (
--     SELECT
--         bedp.BEDARFNR,
--         bedp.BEDP_SEQUENZ,
--         bedp.BEDP_TITELNR,
--         bedp.BEDP_MAN,
--         bedp.BEDP_MENGE_BEDARF_VM,
--         t_info.MELDENUMMER,
--         t_info.VERLAGSNR,
--         t_info.MENGE_VORMERKER,
--         t_info.MANDFUEHR
--     FROM EXT_BEDPBED bedp
--     LEFT JOIN EXT_TITEL_INFO t_info
--         ON bedp.BEDP_TITELNR = t_info.TI_NUMMER
-- ) view1
-- WHERE view1.VERLAGSNR IN (76008, 76070)
-- FETCH FIRST 100 ROWS ONLY;

-- ### default query for raw data ###
-- SELECT
--     bedp.BEDARFNR,
--     bedp.BEDP_SEQUENZ,
--     bedp.BEDP_TITELNR,
--     bedp.BEDP_MAN,
--     bedp.BEDP_MENGE_BEDARF_VM,
--     t_info.MELDENUMMER,
--     t_info.VERLAGSNR,
--     t_info.MENGE_VORMERKER,
--     t_info.MANDFUEHR
-- FROM EXT_BEDPBED bedp
-- LEFT JOIN EXT_TITEL_INFO_NEU t_info
--     ON bedp.BEDP_TITELNR = t_info.TI_NUMMER;


SELECT * FROM EXT_BESPBES_INFO besp
WHERE besp.BESP_TITELNR IN (
    6351156,
    5102709,
    8379474,
    2461414,
    5244007,
    236729,
    7776227,
    6820705,
    2032829,
    5087975,
    7306207,
    8030748,
    6722678,
    9514688,
    1059243,
    7831209,
    1778312,
    7738343,
    3920800,
    3506292,
    1985266,
    7168195,
    6235581,
    7981302,
    1136555,
    9875369
) AND 
besp.BES_DATUM > TO_DATE('2025-06-01', 'YYYY-MM-DD')
ORDER BY besp.BES_DATUM ASC;

-- SELECT
--     bedp.BEDARFNR,
--     bedp.BEDP_SEQUENZ,
--     bedp.BEDP_TITELNR,
--     bedp.BEDP_MAN,
--     bedp.BEDP_MENGE_BEDARF_VM,
--     t_info.MELDENUMMER,
--     t_info.VERLAGSNR,
--     t_info.MENGE_VORMERKER,
--     t_info.MANDFUEHR
-- FROM EXT_BEDPBED bedp
-- LEFT JOIN EXT_TITEL_INFO_NEU t_info
--     ON bedp.BEDP_TITELNR = t_info.TI_NUMMER;


SELECT COUNT(*) FROM (SELECT
    bedp.BEDARFNR,
    bedp.BEDP_SEQUENZ,
    bedp.BEDP_TITELNR,
    bedp.BEDP_MAN,
    bedp.BEDP_MENGE_BEDARF_VM,
    t_info.MELDENUMMER,
    t_info.VERLAGSNR,
    t_info.MENGE_VORMERKER,
    t_info.MANDFUEHR
FROM EXT_BEDPBED bedp
LEFT JOIN EXT_TITEL_INFO_NEU t_info
    ON bedp.BEDP_TITELNR = t_info.TI_NUMMER
WHERE bedp.BEDP_MAN IN (1,90) AND t_info.MELDENUMMER in (17, 18) AND 
bedp.BEDP_MENGE_BEDARF_VM = 0);


-- SELECT * FROM (
--     SELECT * FROM (
--         SELECT /*+ NO_USE_HASH(bedp t_info) */
--             view1.BEDP_TITELNR,
--             t_info.MELDENUMMER,
--             t_info.VERLAGSNR,
--             t_info.MANDFUEHR
--         FROM
--             (SELECT DISTINCT bedp.BEDP_TITELNR FROM EXT_BEDPBED bedp) view1
--         LEFT JOIN EXT_TITEL_INFO t_info
--             ON view1.BEDP_TITELNR = t_info.TI_NUMMER
--         ) sub1 WHERE sub1.MANDFUEHR IN (1,90) AND sub1.MELDENUMMER in (17, 18)
-- ) titles
-- JOIN EXT_AUFPAUF auf
--     ON titles.BEDP_TITELNR = auf.TITELNR
--     WHERE auf.AUFTRAGS_DATUM > TO_DATE('2025-12-05', 'YYYY-MM-DD')
-- FETCH FIRST 20 ROWS ONLY;

-- DESC EXT_AUFPAUF;

-- ##############################
-- SELECT AUFTRAGS_ART, COUNT(*) AS anzahl FROM (
--     SELECT * FROM (
--         SELECT * FROM (
--             SELECT /*+ NO_USE_HASH(bedp t_info) */
--                 view1.BEDP_TITELNR,
--                 t_info.MELDENUMMER,
--                 t_info.VERLAGSNR,
--                 t_info.MANDFUEHR
--             FROM
--                 (SELECT DISTINCT bedp.BEDP_TITELNR FROM EXT_BEDPBED bedp) view1
--             LEFT JOIN EXT_TITEL_INFO t_info
--                 ON view1.BEDP_TITELNR = t_info.TI_NUMMER
--             ) sub1 WHERE sub1.MANDFUEHR IN (1,90) AND sub1.MELDENUMMER in (17, 18)
--     ) titles
--     JOIN EXT_AUFPAUF auf
--         ON titles.BEDP_TITELNR = auf.TITELNR
--         WHERE auf.AUFTRAGS_DATUM > TO_DATE('2025-09-05', 'YYYY-MM-DD')
-- )
-- GROUP BY AUFTRAGS_ART
-- ORDER BY anzahl DESC;

-- PROMPT >>
-- SELECT EINGANGS_ART, COUNT(*) AS anzahl FROM (
--     SELECT * FROM (
--         SELECT * FROM (
--             SELECT /*+ NO_USE_HASH(bedp t_info) */
--                 view1.BEDP_TITELNR,
--                 t_info.MELDENUMMER,
--                 t_info.VERLAGSNR,
--                 t_info.MANDFUEHR
--             FROM
--                 (SELECT DISTINCT bedp.BEDP_TITELNR FROM EXT_BEDPBED bedp) view1
--             LEFT JOIN EXT_TITEL_INFO t_info
--                 ON view1.BEDP_TITELNR = t_info.TI_NUMMER
--             ) sub1 WHERE sub1.MANDFUEHR IN (1,90) AND sub1.MELDENUMMER in (17, 18)
--     ) titles
--     JOIN EXT_AUFPAUF auf
--         ON titles.BEDP_TITELNR = auf.TITELNR
--         WHERE auf.AUFTRAGS_DATUM > TO_DATE('2025-09-05', 'YYYY-MM-DD')
-- )
-- GROUP BY EINGANGS_ART
-- ORDER BY anzahl DESC;




-- EXPLAIN PLAN FOR 
-- SELECT COUNT(*) FROM (
--     SELECT /*+gather_plan_statistics*/ /*+ NO_USE_HASH(bedp t_info) */
--         bedp.BEDP_TITELNR,
--         t_info.MELDENUMMER,
--         t_info.VERLAGSNR,
--         t_info.MANDFUEHR
--         FROM EXT_BEDPBED bedp
--         LEFT JOIN EXT_TITEL_INFO t_info
--             ON bedp.BEDP_TITELNR = t_info.TI_NUMMER
-- );
-- SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY(format => 'ALL'));


-- SELECT * FROM EXT_TITEL_INFO t_info WHERE t_info.TI_NUMMER = 6132326;
-- SELECT * FROM EXT_TITEL_INFO t_info WHERE t_info.TI_NUMMER = 5641810;

-- SELECT t.TI_NUMMER, COUNT(*) cnt
-- FROM EXT_TITEL_INFO t
-- GROUP BY t.TI_NUMMER
-- HAVING COUNT(*) > 1;

-- SELECT 
--     (SELECT COUNT(*) FROM EXT_TITEL_INFO) AS total_rows,
--     (SELECT COUNT(DISTINCT TI_NUMMER) FROM EXT_TITEL_INFO) AS distinct_ids
-- FROM dual;

-- SELECT COUNT(*) 
-- FROM EXT_AUFPAUF auf 
-- WHERE auf.AUFTRAGS_DATUM > TO_DATE('2025-11-18', 'YYYY-MM-DD');

-- SELECT *
-- FROM EXT_AUFPAUF auf 
-- WHERE auf.AUFTRAGS_DATUM > TO_DATE('2025-11-18', 'YYYY-MM-DD');

-- system queries
-- SELECT owner, table_name
-- FROM all_tables
-- ORDER BY owner, table_name;

-- SELECT table_name
-- FROM user_tables
-- ORDER BY table_name;

-- SELECT view_name
-- FROM user_views
-- ORDER BY view_name;
-- SELECT owner, view_name
-- FROM all_views
-- WHERE owner = 'UMB'
-- ORDER BY view_name;

-- DESC all_views;
DESC EXT_AUFPAUF;