我有两个表,我想在它们之间进行左联接,但是我希望仅在第一个表中包含相同联接列值的记录计数为大于或等于第二张表中包含相同联接列值的记录数
我尝试的方法:
首先,我要对每个组的记录计数
然后我将条件过滤( a.cnt> = b.cnt )
这是代码:
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
);
但是我的尝试不起作用,因为先进行连接然后进行条件处理(因此,第二个表中的cnt
的值在完成连接后将保持不变)
任何想法如何使它起作用?
答案 0 :(得分:0)
您想编写引用$url = "https://api.envato.com/v3/market/user/collection?id=8104310";
子句中的“种子表”的查询。然后,您可以从两个表中计算行数并将它们相互比较。像这样:
FROM
我不确定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 a.*, b.*,
FROM (SELECT 1 AS DummyCol) AS dt -- "seed" table; not sure the Presto equivalent
CROSS JOIN ( -- Get all qualifying rows from "disc_band"
SELECT *, count(*) OVER(PARTITION BY groupid) AS RowCount
FROM work.disc_band
WHERE tbl_dt = 20191003
) a
LEFT JOIN ( -- Get all qualifying rows from "alltable1"
SELECT *, count(*) OVER(PARTITION BY groupid) AS RowCount
FROM work.alltable1
WHERE tbl_dt = 20191003
) b ON a.groupid = b.groupid AND a.RowCount >= b.RowCount
) src
逻辑如何,因此您可以根据需要将其重新添加。
我尚未对其进行测试,您可能需要修改语法才能使其与seqnum
一起使用。试一试,让我知道。