Here is my query - Right now it gets me 7981 records instead of 8244 records which is…
- User
Here is my query - Right now it gets me 7981 records instead of 8244 records which is basically sale. After joiniing it comes down to 7981. I used the Left Outer Join and Full Join too but I am still getting 7981 records instead of 8244. Here is what My understanding is, because I am saying in the where clause to get all the record between Nov 1 and Nov 30, and I am also using where it say opn_dt between start and end date. Since I am using AND statement, the output looks into both conditions and get me lower records. One of the reason is, if the acct open in sales table or table somwehere in november then it gets me the data from the table b. but if the table b showing the same customer number in some next months which does not qualify the condition and therefore exclude the records and showing 7981. I would like to know from you - how do I ensure to get all 8244 records as well as the opn_dt should also match between start and end date and if they do not - then still provide the full data regardless of data is available in table b for the same month or not.
Here is my query fix this in hive sql:
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 > '2023-10-31' AND a.opn_dt < '2023-12-01' AND (d.party_id IS NULL OR (a.opn_dt BETWEEN d.start_date AND d.end_date)) ) SELECT * FROM Sales_Performance;
- Assistant
- User
FAILED: SemanticException [Error 10004]: Line 37:25 Invalid table alias or column reference 'start_date': (possible column names are: acct_num, cust_cid, year_month, open_date, products, immigration_cat_cd) (state=42000,code=10004)
- Assistant
- User
I am still getting same results 7981
- Assistant
- User
convert this into cte and count(*) will give more numbers as prim_own_cif_id can have multiple records - instead use distinct
- Assistant
- User
yes i ma geting 8244 records in table 1
- Assistant