我们有 2 个表,分别称为 tbl_1、tbl_2 和 tbl_3。它包含 Vis_ID、Cust_ID 等列。在某些情况下,一个 Vis_ID 将与多个 Cust_ID 相关联。
例如,如果客户登录一个网站,每次访问该网站都会生成一个唯一的 Vis_ID。
一次访问,多个客户可以登录他们的帐户并进行个人购买。在某些情况下,访问将与多个客户 ID 相关联。如果实例超过 2 个,请在此列中附加任何其他零售客户 ID 实例。
例如有访问,该访问附加了 100 个 Cust_ID。
例如,如果 1 次访问中有 5 个 Cust_ID,对于客户 1,它应该有第一个客户 1。对于客户 2,我们需要显示第二个 Cust_ID。
对于 3rd 到 5th Cust_IDs ,我需要以逗号分隔显示这些记录。
with CTE as (
SELECT
Vis_ID,
B.pg_no::INT AS pg_no,
CUST_ID,
dense_rank()over( PARTITION BY Vis_ID order by CUST_ID) as rank
from
db_name.schema_name.tbl_1 A
JOIN db_name.schema_name.tbl_2 B
ON B.id_column = A.id_column
JOIN db_name.schema_name.tbl_3 C
ON CAST(C.xid as VARCHAR)= A.CUST_ID
WHERE flg_col = '0'
AND so_cd NOT IN ('0','1','2','3')
AND DATE_COL = '2022-01-17'
and Vis_ID='12345'
ORDER BY pg_no
)
select Vis_ID, arr[0], arr[1], array_to_string( array_slice(arr, 2, 99999), ', ')
from (
select Vis_ID, array_agg(distinct CUST_ID) within group(order by CUST_ID) arr
from CTE
group by 1
);
When I run the below query that I have included inside CTE separately, I'm getting records which are duplicates.
SELECT Vis_ID, B.pg_no::INT AS pg_no, CUST_ID, dense_rank()over( PARTITION BY Vis_ID order by CUST_ID) 作为 db_name.schema_name.tbl_1 A JOIN db_name.schema_name.tbl_2 B ON B.id_column = A.id_column 的排名JOIN db_name.schema_name.tbl_3 C ON CAST(C.xid as VARCHAR)= A.CUST_ID WHERE flg_col = '0' AND so_cd NOT IN ('0','1','2','3') AND DATE_COL = '2022-01-17' 和 Vis_ID='12345' ORDER BY pg_no
以下重复结果:
Row VISIT_ID CUSTOMER_ID VISITPG_NBR RANK
**1 12345 100 1 1**
2 12345 100 2 1
3 12345 100 3 1
4 12345 100 4 1
5 12345 100 5 1
**6 67891 101 6 2**
7 67891 101 7 2
8 67891 101 8 2
9 67891 101 9 2
10 67891 101 10 2
**11 78910 102 11 3**
12 78910 102 12 3
13 78910 102 13 3
14 78910 102 14 3
Is there any logic to display the distinct results in the CTE temp table?
最终结果应显示如下。
VIS_ID First_Customer Second_Customer Other_Customers
1 100 101 102,103,104,105,106
2 200 201 202,203,204,205
我还希望结果按 pg_no 排序
请指导。