Firebird 通过检索额外数据进行分组

发布于 2024-08-22 01:34:05 字数 538 浏览 7 评论 0原文

我有一个包含 6 列的表格:idabcd,<代码>e。 id 是主键。

我正在尝试检索该组的不同 a、b、c、max(d),以及与 max(d) 位于同一行的 e(列“id”与我的查询不相关)。

我尝试了这个查询:

SELECT a, b, c, MAX(d), e
FROM tablename
GROUP BY a, b, c;

但它给了我“选择列表中的无效表达式(不包含在聚合函数或 GROUP BY 子句中)。”。如果我添加一个额外的 GROUP BY e,它只会给我不同的 a、b、c、e,每个都有 MAX(d),这不是我需要的。我确实理解为什么会发生这种情况,但我不明白的是如何让它执行我需要的操作......

子查询是正确的方法吗?你能为我写一篇吗?

最令人沮丧的是我的查询可以在 MySQL 中运行:(

I have a table with 6 columns: id, a, b, c, d, e. id is primary key.

I am trying to retrieve distinct a, b, c, max(d) for that group, and e that is present in the same row as max(d) is (column "id" is not relevant for my query).

I tried this query:

SELECT a, b, c, MAX(d), e
FROM tablename
GROUP BY a, b, c;

but it gives me "Invalid expression in the select list (not contained in either an aggregate function or the GROUP BY clause).". If I add an extra GROUP BY e, it just gives me distinct a, b, c, e, with a MAX(d) for each, which is not what I need. I do understand why this happens, what I don't understand is how to make it do what I need...

Is a subquery the way to go? Could you write one for me?

The most frustrating thing is that my query would work in MySQL :(

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

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

发布评论

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

评论(1

压抑⊿情绪 2024-08-29 01:34:05
SELECT t1.a,t1.b,t1.c,t1.d,t1.e
FROM tablename AS t1
INNER JOIN (SELECT a, b, c, MAX(d) d
            FROM tablename
            GROUP BY a, b, c
           ) AS t2
ON  t1.a = t2.a
AND t1.b = t2.b
AND t1.c = t2.c
AND t1.d = t2.d

请注意,如果 MAX(d) 关联多行,则此查询将返回 a,b,c 中具有该 d 的所有行> 价值。

我不知道MySQL如何处理这种情况下的重复行。

SELECT t1.a,t1.b,t1.c,t1.d,t1.e
FROM tablename AS t1
INNER JOIN (SELECT a, b, c, MAX(d) d
            FROM tablename
            GROUP BY a, b, c
           ) AS t2
ON  t1.a = t2.a
AND t1.b = t2.b
AND t1.c = t2.c
AND t1.d = t2.d

Note that if multiple rows are tied for MAX(d), this query will return all of the rows of a,b,c with that d value.

I don't know how MySQL deals with duplicate rows in this scenario.

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