求集合中最大公共元素的SQL语句

发布于 2024-08-05 13:45:22 字数 272 浏览 4 评论 0原文

我有一个类似

id contact value
1  A       2
2  A       3
3  B       2
4  B       3
5  B       4
6  C       2

现在我想获取给定联系人组的共同最大值的表格。 例如: 如果我的联系人集是 {A,B} 它将返回 3; 对于集合 {A,C} 它将返回 2 对于集合 {B} 它将返回 4

哪些 SQL 语句可以做到这一点?

I have a table like

id contact value
1  A       2
2  A       3
3  B       2
4  B       3
5  B       4
6  C       2

Now I would like to get the common maximum value for a given set of contacts.
For example:
if my contact set was {A,B} it would return 3;
for the set {A,C} it would return 2
for the set {B} it would return 4

What SQL statement(s) can do this?

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

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

发布评论

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

评论(3

苍风燃霜 2024-08-12 13:45:22

试试这个:

  SELECT value, count(distinct contact) as cnt
    FROM my_table
   WHERE contact IN ('A', 'C')
   GROUP BY value
   HAVING cnt = 2
   ORDER BY value DESC
   LIMIT 1

这是 MySQL 语法,可能因您的数据库而异。 HAVING 子句中的数字 (2) 是集合中元素的数量。

Try this:

  SELECT value, count(distinct contact) as cnt
    FROM my_table
   WHERE contact IN ('A', 'C')
   GROUP BY value
   HAVING cnt = 2
   ORDER BY value DESC
   LIMIT 1

This is MySQL syntax, may differ for your database. The number (2) in HAVING clause is the number of elements in set.

原谅我要高飞 2024-08-12 13:45:22
SELECT max(value) FROM table WHERE contact IN ('A', 'C')

编辑:最大共同点

declare @contacts table ( contact nchar(10) )


insert into @contacts values ('a')
insert into @contacts values ('b')


select MAX(value)
from MyTable
where (select COUNT(*) from @contacts) = 
      (select COUNT(*) 
       from MyTable t
       join @contacts c on c.contact = t.contact
       where t.value = MyTable.value)
SELECT max(value) FROM table WHERE contact IN ('A', 'C')

Edit: max common

declare @contacts table ( contact nchar(10) )


insert into @contacts values ('a')
insert into @contacts values ('b')


select MAX(value)
from MyTable
where (select COUNT(*) from @contacts) = 
      (select COUNT(*) 
       from MyTable t
       join @contacts c on c.contact = t.contact
       where t.value = MyTable.value)
樱娆 2024-08-12 13:45:22

大多数人会告诉您使用:

  SELECT MAX(t.value)
    FROM TABLE t
   WHERE t.contact IN ('A', 'C')
GROUP BY t.value
  HAVING COUNT(DISTINCT t.*) = 2

几个注意事项:

  • DISTINCT 是关键,否则您可能会有两行 t.contact = 'A'。
  • COUNT(DISTINCT t.*) 的数量必须等于 IN 子句中指定的值的数量

我的首选是使用 JOIN:

  SELECT MAX(t.value)
    FROM TABLE t
    JOIN TABLE t2 ON t2.value = t.value AND t2.contact = 'C'
   WHERE t.contact = 'A'

这样做的缺点是您必须对每个条件(在本例中为联系人值)进行自联接(联接到同一个表)。

Most will tell you to use:

  SELECT MAX(t.value)
    FROM TABLE t
   WHERE t.contact IN ('A', 'C')
GROUP BY t.value
  HAVING COUNT(DISTINCT t.*) = 2

Couple of caveats:

  • The DISTINCT is key, otherwise you could have two rows of t.contact = 'A'.
  • The number of COUNT(DISTINCT t.*) has to equal the number of values specified in the IN clause

My preference is to use JOINs:

  SELECT MAX(t.value)
    FROM TABLE t
    JOIN TABLE t2 ON t2.value = t.value AND t2.contact = 'C'
   WHERE t.contact = 'A'

The downside to this is that you have to do a self join (join to the same table) for every criteria (contact value in this case).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文