结合使用select *,distinct和aggregate函数。 [英] combined use of select* , distinct and aggregate function.

查看:165
本文介绍了结合使用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 值?它无法返回所有这些,因为这将需要多行。



您可能通过添加<$ c $获得所需的内容c> 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函数。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆