仅返回组键,而组的行不匹配

发布于 2025-02-08 23:04:11 字数 503 浏览 3 评论 0 原文

| target | start | end  |
| ------ | ----- | ---- |
| 1      | NULL  | NULL |
| 1      | 100   | NULL |
| 2      | NULL  | NULL |
| 2      | NULL  | 100  |

我正在尝试通过描述的表运行查询,这为我提供了所有 target 值,其中所有连接的行不匹配 start nes null nes null and End in null in null < <

<强>因此,预期的回报将是

| target |
| ------ |
| 2      | <-- Has no connected rows where start IS NOT NULL and end IS NULL 

我已经尝试了自我加入,这没有完成所需的结果

| target | start | end  |
| ------ | ----- | ---- |
| 1      | NULL  | NULL |
| 1      | 100   | NULL |
| 2      | NULL  | NULL |
| 2      | NULL  | 100  |

I am trying to run a query over the described table, which gives me all target values where all of the connected row does not match start IS NOT NULL and end IS NULL

So the expected return would be

| target |
| ------ |
| 2      | <-- Has no connected rows where start IS NOT NULL and end IS NULL 

I have already tried a self join, which did not accomplish the wanted result

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

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

发布评论

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

评论(1

不羁少年 2025-02-15 23:04:11

使用子句中的聚合和条件:

SELECT target
FROM tablename
GROUP BY target
HAVING SUM(start IS NOT NULL AND end IS NULL) = 0;

请参阅

Use aggregation and the condition in the HAVING clause:

SELECT target
FROM tablename
GROUP BY target
HAVING SUM(start IS NOT NULL AND end IS NULL) = 0;

See the demo.

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