SQL查询的其他条件

发布于 2025-01-30 14:02:18 字数 471 浏览 2 评论 0原文

有必要选择学生参加所有比赛的小组。我提出了这样的要求。但是,当小组中只有一个这样的学生时,情况就属于它。您需要两个或更多。

CREATE VIEW V3 AS
SELECT Groups
FROM R3
JOIN R1 USING (name)
GROUP BY Groups
HAVING COUNT (DISTINCT Competition) = (SELECT COUNT (DISTINCT Competition) FROM R1);

;

CREATE TABLE R1(
name VARCHAR(100),
article VARCHAR(100),
Competition VARCHAR(100));

CREATE TABLE R3 (
name VARCHAR(100),
Groups VARCHAR(100)
);

我尝试添加条件计数(不同的组)&gt 1但是不起作用

It is necessary to select groups whose students took part in all competitions. I made such a request. But the condition falls under it when there is only one such student in the group. You need two or more.

CREATE VIEW V3 AS
SELECT Groups
FROM R3
JOIN R1 USING (name)
GROUP BY Groups
HAVING COUNT (DISTINCT Competition) = (SELECT COUNT (DISTINCT Competition) FROM R1);

Tables

CREATE TABLE R1(
name VARCHAR(100),
article VARCHAR(100),
Competition VARCHAR(100));

CREATE TABLE R3 (
name VARCHAR(100),
Groups VARCHAR(100)
);

I tried adding the condition COUNT (DISTINCT Groups) > 1 but it not work

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

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

发布评论

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

评论(1

鲸落 2025-02-06 14:02:18

这是一种方法:

with cte as (select r1.name, Groups_c, count(r1.name) over(partition by Groups_c) cnt2
from r1
left join r3 on r1.name = r3.name
group by  r3.name,  Groups_c
having count(distinct Competition) = (select count(distinct Competition) from r1))
select r3.Groups_c
from r3
left join cte on r3.Groups_c = cte.Groups_c
group by r3.Groups_c
having count (distinct r3.name) = cte.cnt2 

This is one way:

with cte as (select r1.name, Groups_c, count(r1.name) over(partition by Groups_c) cnt2
from r1
left join r3 on r1.name = r3.name
group by  r3.name,  Groups_c
having count(distinct Competition) = (select count(distinct Competition) from r1))
select r3.Groups_c
from r3
left join cte on r3.Groups_c = cte.Groups_c
group by r3.Groups_c
having count (distinct r3.name) = cte.cnt2 

DEMO

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