我有一个Hive表,其中的数据如下所示-
每个客户都有对应的帐户,目标是建立内部客户对。 配对基于帐户的出生年份相同或名称的前三个字符相同。 例如-山姆和塞缪尔。
理想情况下,不应创建相同的帐户对,例如AA,XX等。 一对AC和CA也都相同,因此仅需要一对这样的对。可以在“姓名”和“出生年份”密钥上形成一对,但是这里也只需要一个条目(可以是任何人)。
我应该如何解决这个问题。 测试数据进行检查-
create table customer_account(
customer INT NOT NULL,
accounts VARCHAR(100) NOT NULL,
name VARCHAR(40) NOT NULL,
yob DATE,
);
INSERT INTO
customer_account(customer,accounts,name,yob)
VALUES
(1,"A","John",2001),
(1,"X","Tom",1996),
(1,"C","Harry",2001),
(2,"D","Sam",1994),
(2,"F","Samuel",1995),
(3,"Z","Jake",)1994,
(3,"G","Drake",1998),
(3,"H","Arnold",1993),
(3,"K","Yang",1990)
;
答案 0 :(得分:1)
您应该能够将子字符串用于HIVE语言。逻辑应该是合理的,尽管您可能需要根据需要对其进行调整。
您要尝试的是一元(或自我)联接。下面是可以传递的查询类型的示例。本质上,您要加入一个OR条件,并用一个case语句测试该条件以获取“ Pair_Key”。我使用了内部联接,假设您只希望发生匹配的实例。
SELECT
t1.customer as Customer1,
t2.customer as Customer2,
t1.Accounts as Accounts1,
t2.Accounts as Accounts2,
CONCAT(t1.Accounts, t2.Accounts) as Pair_No,
t1.Name as Name1,
t2.Name as Name2,
t1.YOB as YOB1,
t2.YOB as YOB2,
CASE
WHEN t1.YOB = t2.YOB THEN 'YOB'
WHEN SUBSTR(t1.Name, 3) = SUBSTR(t2.Name, 3) THEN 'Name'
else 'Issue'
END as Pair_Key
FROM (SELECT * FROM Table1) as t1
inner join (SELECT * FROM Table1) as t2 --instance 2 of the same table
on (SUBSTR(t1.Name, 3) = SUBSTR(t2.Name, 3) OR t1.YOB = t2.YOB)
没有测试数据,也没有更多关于您走过的距离的详细信息,这是一个开始。
如果客户编号需要相同,则只需调整为:
on (t1.Customer = t2.Customer) and (SUBSTR(t1.Name, 3) = SUBSTR(t2.Name, 3) OR t1.YOB = t2.YOB)
答案 1 :(得分:1)
这就是您所描述的:
select t1.*, t2.name, t2.yob
from t t1 join
t t2
on t2.customer = t1.customer and
(t2.yob = t1.yob or
substr(t2.name, 1, 3) = substr(t1.name, 1, 3)
) and
t2.account > t1.account;
无需两次提取customer
。如果需要“相同”对,则将最后一个条件更改为>=
。