SQL:为表中的所有值集显示不同的ID [英] SQL:Display distinct ids for all the set of values from table

查看:89
本文介绍了SQL:为表中的所有值集显示不同的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个问题,在执行查询后,我得到这样的结果

I have a problem where after executing a query i'm getting a result like this

DevID   Difference      
-----------------
99       5        
99       10   
99       5        
99       4 
12       8        
12       9 
12       5        
12       6 

我不希望重复的ID ,
我应该只能显示一个id。
这可以通过使用distinct轻松实现,但是问题是我还需要显示差异列。我不介意diff中包含哪个值,但是99的值之一可以在那里,但基本上我只需要一个id值。
预期结果是这样的。

i dont want the duplicate ids, I should be able to display only one id. This could be easily achieved by using distinct however the problem is i also need to display the Difference column. I'm not bothered which value comes in diff but either one of the values for 99 can come there but basically i just need one value for id. Expected result is something like this.

DevID   Difference      
-----------------
99       5        
12       8  

如果它有助于此问题是以下问题的继续
属于同一组但存储在两行中的值的差异

If it helps this question is continuation of the following question Difference of values that belong to same group but stored in two rows

推荐答案

您可以将 GROUP BY MAX MIN 一起使用,或者

You can use GROUP BY along with MAX or MIN or any other aggregation function to get a result as you expected.

SELECT DevID, MAX(Difference) 
FROM Yourtablename
GROUP BY DevID

这篇关于SQL:为表中的所有值集显示不同的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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