结合使用select *,distinct和aggregate函数。

编程入门 行业动态 更新时间:2024-10-22 23:31:15
本文介绍了结合使用select *,distinct和aggregate函数。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有桌子

I have table

abc(abc_ID(PK), other_Id(FK), value_Field)

当我点击查询时

when I hit query

select other_Id, Min(value_field) from abc group by other_Id

我收到了结果。 但现在,我必须选择甚至 abc_ID 字段结果。 我试过像

I am getting result. But now, I have to select even abc_ID field with this result. I tried like

select abc_ID, other_Id, Min(value_field) from abc group by other_Id

但没有收到结果.. 请帮助。我是初学者。

but not getting result.. Please help. I am a beginner.

推荐答案

问题是因为 abc_ID 不是分组的一部分,SQL可以不返回它:GROUP BY说将所有具有相同值的行放入一行,所以当你尝试返回< code> 时,这很好other_Id因为它每行有一个不同的值,并且 MIN(value_Field)也很好,因为eit将不同的值组合成新行的单个值。但是它应该返回哪个 abc_ID 值?它无法返回所有这些,因为这将需要多行。 您可能通过添加 abc_ID 到GROUP BY子句,但我怀疑会搞乱MIN值: The problem is that because abc_ID is not part of the grouping, SQL can't return it: GROUP BY says "Make all the rows that have the same value into a single row", so it's fine when you try to return the <code>other_Id because it will have one different value per row, and the MIN(value_Field) is also fine, becaus eit combines the different values into a single value for the new row. But which of the abc_ID values should it return? It can't return all of them, because that would require more than one row. It is possible that you might get what you want by adding abc_ID to the GROUP BY clause, but I suspect that will mess up the MIN value: SELECT abc_ID, other_Id, MIN(value_field) FROM abc GROUP BY other_Id, abc_ID

通过在临时变量中存储数据来解决。 Solved by Storing data in temp variable.

更多推荐

结合使用select *,distinct和aggregate函数。

本文发布于:2023-10-26 23:30:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1531659.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   select   distinct   aggregate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!