Tablea custnum acctnum opendate 123abcd789 546787123 1/5/2023 785bdef234 578541216…
- User
Table_a cust_num acct_num open_date 123abcd789 546787123 1/5/2023 785bdef234 578541216 2/20/2023 546weyt548 213054623 3/21/2023 987wyte412 702598124 3/21/2023 762wwer880 675478953 3/27/2023
Table_b cust_num status snapshot_date 123abcd789 student 1/3/2023 123abcd789 PR 1/4/2023 123abcd789 student 1/5/2023 123abcd789 worker 1/6/2023 785bdef234 student 2/18/2023 785bdef234 student 2/19/2023 785bdef234 worker 2/20/2023 785bdef234 worker 2/21/2023 785bdef234 PR 2/22/2023 546weyt548 student 3/17/2023 546weyt548 student 3/18/2023 546weyt548 student 3/19/2023 546weyt548 student 3/20/2023 546weyt548 worker 3/21/2023 546weyt548 worker 3/22/2023 546weyt548 PR 3/23/2023 546weyt548 PR 3/24/2023 546weyt548 PR 3/25/2023 762wwer880 student 3/21/2023 762wwer880 student 3/22/2023 762wwer880 student 3/23/2023 762wwer880 student 3/24/2023 762wwer880 student 3/25/2023 762wwer880 PR 3/26/2023 762wwer880 PR 3/27/2023 762wwer880 worker 3/28/2023 762wwer880 worker 3/29/2023 762wwer880 worker 3/30/2023
WITH Sales_Performance AS ( SELECT DISTINCT CAST(a.acct_num AS BIGINT) AS acct_num, CAST(a.prim_own_cif_id AS BIGINT) AS cust_cid, SUBSTR(TRIM(a.opn_dt), 1, 7) AS year_month, a.opn_dt AS open_date, a.type AS products, CASE WHEN d.immigration_cat_cd IN (‘000’,‘999’,‘C1’,‘FC1’,‘FC2’,‘FC3’,‘EN2’,‘PV1’,‘PV2’,‘SW1’,‘SW2’,‘SW3’,‘SE2’,‘STP’,‘C2’,‘NV5’) THEN ‘PR’ WHEN d.immigration_cat_cd IN (‘FW’,‘FW0’) THEN ‘FW’ WHEN d.immigration_cat_cd IN (‘S’,‘S00’) THEN ‘IS’ ELSE ‘Regular_Customers’ END AS MCB_Category FROM table_a a LEFT JOIN ( SELECT DISTINCT CAST(party_id AS BIGINT) AS party_id, immigration_cat_cd, start_date, date_sub(coalesce(LEAD(start_date) OVER (PARTITION BY party_id ORDER BY start_date ), current_date),1) AS end_date FROM (SELECT party_id, immigration_cat_cd, businesseffectivedate AS start_date, RANK() OVER (PARTITION BY party_id, immigration_cat_cd ORDER BY businesseffectivedate) AS rank FROM table_b ) d WHERE rank = 1 ) d ON lpad(a.prim_own_cif_id,15,‘0’) = lpad(d.party_id,15,‘0’) WHERE trim(a.channel) NOT IN (‘Branch’,‘CCC’) AND trim(a.type) NOT IN (‘US$’,‘GTSP’) AND a.opn_dt > ‘2022-12-31’ AND a.opn_dt < ‘2023-04-01’ AND (d.party_id IS NULL OR (a.opn_dt BETWEEN d.start_date AND d.end_date) OR d.start_date IS NULL) ) SELECT * FROM Sales_Performance;
Now use my above query and above tables and get me the output
- Assistant
Based on the SQL query and table information you've provided, there seems to be a mismatch in the fields and table schemas. The query references columns and tables that don't exist in the schema of
table_aandtable_byou've described. For instance,prim_own_cif_id, , , , , , , etc., are not present in the provided tables.