[英]rank over partition
I have a total of 3 tables. 我总共有3张桌子。 One table is called CONTACTINFO
with CNTCT_ID
as a primary index. 一个表被称为CONTACTINFO
与CNTCT_ID
作为主要指标。
Second table is CONTACTMAP
which links a customer( CUSTNUM
) to all the contactinfo (details suchs as name, position, phone number etc). 第二个表是CONTACTMAP
,它将客户( CUSTNUM
)链接到所有contactinfo(详细信息,例如姓名,职位,电话号码等)。
Final table is a complete customer list, with one column that has a revolving list of all our customers. 最终表是完整的客户列表,其中一列包含所有客户的循环列表。
My code below pulls back the most recent entry for custnum
84574. But I need it pull back results for all custnum
in the complete customer list custlist
. 我下面的代码回退了custnum
84574的最新条目。但是我需要它回退完整客户列表custlist
所有custnum
结果。
select
A. CUST_TYPE
,A.CINSERT_DT
,A.CNTCT_ID
from CONTACTINFO a
JOIN CONTACTMAP B
ON A.CNTCT_ID = B.CNTCT_ID
where b. CUSTNUM in (‘84574’)
and A. CUST_TYPE in (‘PAYER’)
qualify rank() over (partition by a. CUST_TYPE order by A. CINSERT_DT desc) = 1
Here are my tables CONTACTINFO TABLE
这是我的表格CONTACTINFO TABLE
CNTCT_ID CUST_TYPE
1 PAYER
2 OWNER
3 BUYER
CONTACTMAP Table
CNTCT_ID CUSTNUM
1 84574
2 99457
3 54187
TABLE3
CUSTNUM
84574
99457
54187
Simply join table #3 on CUSTNUM and change the PARTITION BY to CUSTNUM instead of CUST_TYPE: 只需在CUSTNUM上加入表#3,然后将PARTITION BY更改为CUSTNUM,而不是CUST_TYPE:
select
A. CUST_TYPE
,A.CINSERT_DT
,A.CNTCT_ID
from CONTACTINFO a
JOIN CONTACTMAP B
ON A.CNTCT_ID = B.CNTCT_ID
JOIN custlist as c
ON b.CUSTNUM = c. CUSTNUM
--maybe remove the condition and add CUST_TYPE to PARTITION BY if you want one row per type
where A.CUST_TYPE in (‘PAYER’)
qualify
rank()
over (partition by c.CUSTNUM
order by CINSERT_DT desc) = 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.