I have tow table and I want do left join between them , But I want to left join happen only if the count of records that contain the same value of join column in the first table is more or equal the count of records that contain the same value of join column in the second table
what i try :
first i do the count of records per the each group
and then i put condition to filter
(a.cnt>=b.cnt)
this is the code :
insert into work.discount_tmp
select
SubsID , MSISDN , EppAcc, User_Name , Bill_Cycle, Tariff , Pack ,Discount_Qual_ID ,Discount_ID , Qualification_Type,Discount_Desc,Sequence,a.GroupID,RuleID,dib_band_id,dib_band_end,dib_charge_ref,DIB_DIS0,dib_disc_type,dib_limit_disc,DIB_MAX_,cd_class_type,ClassID,Class,dgh_group_id ,dgh_inclusion_from,dgh_inclusion_to,20191003
from
(
(
select *, row_number() over (partition by GroupID order by Discount_ID ) as seqnum,
COUNT(*) over (partition by GroupID order by GroupID ) as cnt
from
work.disc_band
where tbl_dt=20191003
order by Discount_ID
) a
left join
(
select *, row_number() over (partition by GroupID order by cd_class_type,try(cast(coalesce(classid,'0') as bigint)) ) as seqnum,
count(*) over (partition by GroupID order by GroupID ) as cnt
from
work.alltable1
where tbl_dt=20191003
) b
on
a.GroupID=b.GroupID and a.cnt>=b.cnt and a.seqnum=b.seqnum
)
But my try not work because the join done first and then the condtion (so the value of cnt in the second table will not stay the same after join done )
any idea how to make that work ?
What I have tried:
insert into work.discount_tmp select SubsID , MSISDN , EppAcc, User_Name , Bill_Cycle, Tariff , Pack ,Discount_Qual_ID ,Discount_ID , Qualification_Type,Discount_Desc,Sequence,a.GroupID,RuleID,dib_band_id,dib_band_end,dib_charge_ref,DIB_DIS0,dib_disc_type,dib_limit_disc,DIB_MAX_,cd_class_type,ClassID,Class,dgh_group_id ,dgh_inclusion_from,dgh_inclusion_to,20191003 from
((select *, row_number() over (partition by GroupID order by Discount_ID ) as seqnum,
COUNT(*) over (partition by GroupID order by GroupID ) as cnt from work.disc_band where tbl_dt=20191003 order by Discount_ID) a
left join
(select *, row_number() over (partition by GroupID order by cd_class_type,try(cast(coalesce(classid,'0') as bigint)) ) as seqnum,
count(*) over (partition by GroupID order by GroupID ) as cnt
from work.alltable1 where tbl_dt=20191003 ) b
on a.GroupID=b.GroupID and a.cnt>=b.cnt and a.seqnum=b.seqnum )