在 SELF JOIN (SQL) 中难以生成没有重复项的不同行
我对 SQL 还很陌生,我认为我可以使用它为我雇主的客户创建一个列表。不幸的是,大多数客户都有多个帐户,并且文件中每个帐户都有一个不同的行。
我试图使用自联接为每个客户创建一行,并为帐户创建多列。
SELECT DISTINCT A.Account_Number AS Account_1, B.Account_Number AS Account_2, A.Client_Name
FROM client_table AS A, client_table AS B
WHERE A.Account_Number <> B.Account_Number
AND A.Client_Name = B.Client_Name
ORDER BY A.Client_Name;
不幸的是,结果是这样的,我会得到一个如下所示的表:
Account_1 | Account_2 | Client_name |
---|---|---|
000001 | 000002 | Joe Shmo |
000001 | 000003 | Joe Shmo |
000002 | 000003 | Joe Shmo |
000002 | 000001 | Joe Shmo |
我知道对于两个以上的帐户,我需要超过两次加入,但我还没有知道怎么做了。
有没有办法防止重复输入?
顺便说一句,我正在使用 BigQuery。
I'm pretty new to SQL and thought I'd be able to use it to create a list for my employer's clients. Unfortunately, there the majority of clients have more than one account and the file has a distinct row for each account.
I was trying to use a self join to create one row for each client with multiple columns for the accounts.
SELECT DISTINCT A.Account_Number AS Account_1, B.Account_Number AS Account_2, A.Client_Name
FROM client_table AS A, client_table AS B
WHERE A.Account_Number <> B.Account_Number
AND A.Client_Name = B.Client_Name
ORDER BY A.Client_Name;
Unfortunately, the results were such that I would get a table that looks like:
Account_1 | Account_2 | Client_name |
---|---|---|
000001 | 000002 | Joe Shmo |
000001 | 000003 | Joe Shmo |
000002 | 000003 | Joe Shmo |
000002 | 000001 | Joe Shmo |
I understand that for more than two accounts, I'll need more than two joins, but I haven't figured out how to do it.
Is there a way to prevent double entry?
I'm using BigQuery btw.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
第一步对每个客户的账户进行编号。然后使用条件聚合为每个帐户获取一列。
In the first step number the accounts per client. Then use conditional aggregation to get one column per account.
您可以使用
LEFT JOIN
来完成此操作。根据需要对用户可能拥有的多个帐户重复此操作。You can do this with
LEFT JOIN
. Repeat as necessary for however many accounts a user may have.您可以使用
GROUP_CONCAT()
获取逗号分隔的列表。或者,您可以使用
RANK()
在CTE
中对您的帐户进行排序,然后列出它们。注意:任何第 7 个或以上的帐户都不会显示!
db<>fiddle 此处
请参阅有关 bigQuery 中 group_concat 的更多信息,请参阅以下帖子:BigQuery GROUP_CONCAT 和 ORDER BY
You can use
GROUP_CONCAT()
to fetch a comma seperated list.Or you can use
RANK()
to order your accounts in aCTE
and then list them.Attention : any 7th accounts or above will not be shown!
db<>fiddle here
Please see the following post for more information on group_concat in bigQuery: BigQuery GROUP_CONCAT and ORDER BY