使用不同和/或分组依据来过滤选择

发布于 2025-01-03 08:38:27 字数 598 浏览 1 评论 0原文

我还有一个关于不同和/或分组依据的问题。我的表如下:

|   art   |   ean             |   obs   |   vke   |
---------------------------------------------------
|  type    |  1234567890123   |    1    |   100   |
|  type    |  1234567890123   |    0    |   50    |
|  type    |  1234567890123   |    0    |   60    |
|  type    |  1234567890123   |    0    |   70    |

我需要查询始终选择 obs = 1 的行,并且仅选择 obs = 0 的其他行中最便宜的行。所有其他相等的 EAN 根本不应该列出。这可能吗?

所以结果应该是:

|  type    |  1234567890123   |    1    |   100   |
|  type    |  1234567890123   |    0    |   50    |

I have also a question regarding distinct and/or group by. My table as follow:

|   art   |   ean             |   obs   |   vke   |
---------------------------------------------------
|  type    |  1234567890123   |    1    |   100   |
|  type    |  1234567890123   |    0    |   50    |
|  type    |  1234567890123   |    0    |   60    |
|  type    |  1234567890123   |    0    |   70    |

I need the query to select always the row with obs = 1 and only the cheapest of the others with obs = 0. All other equal EAN should not be listed at all. Is this possible ?

So the result should be:

|  type    |  1234567890123   |    1    |   100   |
|  type    |  1234567890123   |    0    |   50    |

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

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

发布评论

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

评论(2

南街女流氓 2025-01-10 08:38:27
select art, ean, obs, vke 
from table_name
where obs = 1
union all
select art, ean, obs, min(vke) as vke
from table_name
where obs = 0
group by art, ean, obs

我将union两个结果,第一个是所有带有obs aquals 1的行,第二个是按art分组的所有行>、eanobsobs 等于 0minvke

了解有关 union分组依据

select art, ean, obs, vke 
from table_name
where obs = 1
union all
select art, ean, obs, min(vke) as vke
from table_name
where obs = 0
group by art, ean, obs

I'd union two results first one are all rows with obs aquals 1 and the second all rows grouped by art, ean, obs with obs equals 0 and min value of vke.

Read more about union and group by.

妄想挽回 2025-01-10 08:38:27
SELECT art, ean, obs, vke
FROM table_name
WHERE obs = 1
UNION ALL
(
    SELECT art, ean, obs, vke
    FROM table_name
    WHERE obs = 0
    ORDER BY vke
    LIMIT 1
)
SELECT art, ean, obs, vke
FROM table_name
WHERE obs = 1
UNION ALL
(
    SELECT art, ean, obs, vke
    FROM table_name
    WHERE obs = 0
    ORDER BY vke
    LIMIT 1
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文