dimanche 22 octobre 2017

need help modifying the query

I am new to sql and having a tough time working on queries that are long and tough.

Query -

select Unique_Item_Id,Postcode_Area,Postcode_District,Postcode_Sector,Postcode_Unit from            
(SELECT         
Unique_Item_Id          
,CASE           
WHEN  Post_Code IS NOT NULL             
AND (First_Character BETWEEN 'A' AND 'Z')           
AND ((Second_Character  BETWEEN '0' AND '9') OR (Third_Character  BETWEEN '0' AND '9'))         
AND Special_Char =0             
AND (First_Occurence+3<>Fourth_Occurence)               
THEN  Post_code ELSE NULL           
END AS Valid_Postcode,          
CASE WHEN Valid_Postcode IS NOT NULL            
THEN SUBSTR(Valid_Postcode,1,First_Occurence-1)             
END AS Postcode_Area ,          
CASE WHEN Valid_Postcode IS NOT NULL AND Post_Length >4         
THEN CASE           
WHEN   (First_Occurence+1=Second_Occurence) AND (First_Occurence+2<>Third_Occurence) THEN  SUBSTR(Valid_Postcode,First_Occurence,1)             
WHEN   (First_Occurence+1<>Second_Occurence) THEN  SUBSTR(Valid_Postcode,First_Occurence,2)             
WHEN   ((First_Occurence+1=Second_Occurence) AND (First_Occurence+2=Third_Occurence)) THEN SUBSTR(Valid_Postcode,First_Occurence,2)         
END         
WHEN  Post_Length =2 THEN  SUBSTR(Valid_Postcode,2,1)           
WHEN   Post_Length IN (3,4) THEN  SUBSTR(Valid_Postcode,Post_Length-1,2)            
ELSE NULL           
END Postcode_District,          
CASE WHEN  Valid_Postcode IS NOT NULL AND Post_Length >4 THEN           
CASE            
WHEN (First_Occurence+1=Second_Occurence AND First_Occurence+2<>Third_Occurence) THEN SUBSTR(Valid_Postcode,Second_Occurence,1)             
WHEN (First_Occurence+2=Second_Occurence )  THEN SUBSTR(Valid_Postcode,Second_Occurence,1)          
WHEN ((First_Occurence+1=Second_Occurence) AND (First_Occurence+2=Third_Occurence)) THEN  SUBSTR(Valid_Postcode,Third_Occurence,1)          
END             
ELSE NULL           
END AS  Postcode_Sector,            
CASE WHEN Post_Length >4 AND Valid_Postcode IS NOT NULL THEN            
CASE            
WHEN  First_Occurence+1=Second_Occurence AND First_Occurence+2<>Third_Occurence THEN SUBSTR(Valid_Postcode,Second_Occurence+1,2)            
WHEN  (First_Occurence+1<>Second_Occurence AND Second_Occurence <>0) THEN SUBSTR(Valid_Postcode,Second_Occurence+1,2)           
WHEN   (First_Occurence+2=Third_Occurence) THEN SUBSTR(Valid_Postcode,Third_Occurence+1,2)          
ELSE NULL           
END         
END AS Unit,            
CASE            
WHEN  (SUBSTR(Unit,1,1)  BETWEEN 'A' AND 'Z') AND (SUBSTR(Unit,2,1)  BETWEEN 'A' AND 'Z')           
THEN  Unit          
ELSE NULL           
END AS Postcode_Unit            
FROM            
(           
SELECT Unique_Item_Id,          
CAST(OREPLACE(Delivery_Point_Postcode,' ','' )AS VARCHAR(10)) AS  Post_Code ,           
SUBSTR(TRIM(Post_Code),1,1) First_Character,            
SUBSTR(TRIM(Post_Code),2,1) Second_Character,           
SUBSTR(TRIM(Post_Code),3,1) Third_Character,            
CHAR_LENGTH((Post_Code)) AS Post_Length,            
REGEXP_INSTR(Post_Code,'[0-9]',1,1,0,'i') AS First_Occurence ,          
REGEXP_INSTR(Post_Code,'[0-9]',1,2,0,'i') AS Second_Occurence ,         
REGEXP_INSTR(Post_Code,'[0-9]',1,3,0,'i') Third_Occurence,          
REGEXP_INSTR(Post_Code,'[0-9]',1,4,0,'i') Fourth_Occurence,         
REGEXP_INSTR(Post_code,'[-!,.\\!//;]',1,1,0,'i') AS Special_Char            
from BASE_MPE           
where scver_upd_dttm between '2017-01-25 00:00:00' and '2017-02-07 23:59:59'and  UPU_Tracking_Num is null           
and unique_item_id is not null and unique_Item_id <>'' and (One_d_barcode ='' OR one_d_barcode is null)             

QUALIFY ROW_NUMBER() OVER(PARTITION BY Unique_Item_Id ORDER BY scan_dttm)=1         
) POST_IN where Postcode_Area IS NOT NULL           
)A

Details about query -

List of Source Tables: BASE_MPE

Target Table: TRAFFIC_SHIPPER_PIECE

Insert Update Logic: Where a Unique_Item_ID exists in BASE_PRE_ADV_ITEM_DETAIL, then UPDATE the properties of the existing TRAFFIC_PIECE_ID

Else

INSERT a new record into TRAFFIC_SHIPPER_PIECE for every DISTINCT combination of ((One_D_Barcode and barcode_creation_dt) or (Unique_Item_ID))

Filter Condition: Consider all records

Requirement - how can i modify the query to have o/p having combination of distinct values IN fields currently i am getting duplicates in the o/p

Aucun commentaire:

Enregistrer un commentaire