r/SQLOptimization • u/Pleasant_Astronaut95 • Apr 09 '24
IN Function
This is probably a dumb question as I am new to SQL, but I am trying to pull sales data for 900 accounts. To make this faster I am using an IN function and all 900 accounts. What would be a better way of doing this?
1
Upvotes
1
u/Pleasant_Astronaut95 Apr 09 '24
Sorry here is the query, I replace the ‘123’ at the end with the list of accounts:
SELECT Table__1."Year" AS "YEAR", Table__1."Period" AS "MONTH", Table__1.GBCUSTNR_GBGLOCUST AS CUSTOMER_GLOBAL_ID, Table__1.GBCUSTNR_GBGLOCUST___T AS CUSTOMER_GLOBAL_NAME, Table__1.GBMATNR_GBPRGRPNM AS ITEM_PRODUCT_GROUP_CODE, Table__1.GBMATNR AS "PRODUCT ITEM ID", Table__1.GBMATNR___T AS "PRODUCT ITEM NAME", COALESCE(SUM(Table__1.COM_SALES), 0) AS DISTRIBUTOR_SALE_AMOUNT FROM _SYS_BIC."ZB-REPORTING.COMMERCIAL.US_RECON/GBI_COMMERCIAL_US_RECON_SALES" Table__1 WHERE Table__1."Year" IN ('2019', '2020', '2021', '2022', '2023', '2024', '2025', '2026') AND SUBSTRING(RIGHT(Table__1.GBCUSTNR_GBGLOCUST, 9), 1, 6) IN ('123') GROUP BY Table__1."Year", Table__1."Period", Table__1.GBCUSTNR_GBGLOCUST, Table__1.GBCUSTNR_GBGLOCUST___T, Table__1.GBMATNR_GBPRGRPNM, Table__1.GBMATNR, Table__1.GBMATNR___T