GROUP 列的组合,而不是排列

发布于 2024-11-01 10:52:40 字数 574 浏览 5 评论 0原文

在我一直在开发的一个网站上,用户可以将数据提交到表中,当他们提交数据时,系统会要求他们提供合作伙伴名称,因为有两个人负责获取数据。我正在尝试创建一个高分表,列出顶级合作伙伴关系,又称表中最常出现的合作伙伴和提交者的组合(而不是排列)。

GROUP 命令对此非常有用,但我在组合/排列问题上遇到了一些障碍。目前,当我对它们进行分组时,它仅检查提交者和合作伙伴的排列,而不是组合。问题是,合作伙伴通常会选择在一个人作为提交者,另一个人作为合作伙伴之间交替,所以我实际上有两种可能的组排列可供选择。

目前,我的代码将拉出一个提交者和合作伙伴排列的高分表,以及另一个合作伙伴和提交者排列的高分表。当合作伙伴=提交者或提交者=合作伙伴时,我需要加入这些结果(可能未分组和未排序),然后将它们分组并按降序排列。

SELECT submitter, partner, COUNT(*) FROM submissions GROUP BY submitter, partner;

上面的代码将返回包含提交者和合作伙伴特定排列的计数的表,但是如果相同的两个人是合作伙伴和提交者,只是交换,则他们不被算作同一组。

有人知道这个的代码吗?

On a website I've been working on users are allowed to submit data into a table, and when they submit the data, they are asked for a partner name, since two people work on acquiring the data. I am attempting to create a high score table, listing the top partnerships, AKA the combination(not permutation) of partner and submitter that appears most frequently on the table.

The GROUP command works great for this, but I've hit a little snag with the combination/permutation issue. Currently, when I group them, it only checks for permutations of submitters and partners, not combinations. The issue is that often a partnership chooses to alternate between one person being the submitter, and the other being the partner, so I have actually two possible permutations of GROUPs which I can pull.

Currently I have code which will pull one high score table of Submitter and Partner permutations, and another high score table of Partner and Submitter permutations. I need to join these results(possibly ungrouped and non-ordered) when partner=submitter or submitter=partner, and then group them and order then in descending order.

SELECT submitter, partner, COUNT(*) FROM submissions GROUP BY submitter, partner;

The above code will return the table with the counts of the specific permutation of submitter and partner, but if the same two people are partner and submitter, just switched, they are not counted as the same group.

Anyone know the code for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别在捏我脸啦 2024-11-08 10:52:40

这不是最漂亮的解决方案,但它确实为您提供了您想要的答案:

SELECT Person1, Person2, COUNT(*) FROM (
    SELECT 
        CASE WHEN submitter < partner THEN submitter ELSE partner END AS Person1,
        CASE WHEN submitter >= partner THEN submitter ELSE partner END AS Person2
    FROM submissions
) Q
GROUP BY Person1, Person2

Not the most pretty solution, but it does provide you the answer you want:

SELECT Person1, Person2, COUNT(*) FROM (
    SELECT 
        CASE WHEN submitter < partner THEN submitter ELSE partner END AS Person1,
        CASE WHEN submitter >= partner THEN submitter ELSE partner END AS Person2
    FROM submissions
) Q
GROUP BY Person1, Person2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文