选择一个最低总计 [英] Select One Lowest Total

查看:53
本文介绍了选择一个最低总计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个查询,通过使用Group By子句选择最低总数。但是如果有两个相同数量的最低总数,我只希望我的查询返回一个值。


有什么想法吗?


谢谢

I have a query that picks the lowest total by using a Group By clause. But if there are two lowest totals of the same amount I only want my query to return one value.

Any ideas?

Thanks

推荐答案

给我们一个线索到你在说什么您正在使用哪些数据?
Give us a clue as to what you''re talking about. What data are you working with?


我可以简单地说出来。使用一个基本解释我想要的例子。似乎我在SQL或SQL上都很糟糕。好的,这里是


让我们说你有一张国家及其人口的表格....


ID ....... .Country ........... Popn


1 ............英国.......... ..... 1000

2 ............美国............. 2000 2000

3 ............法国.......... 1500


好​​的。现在我想做的就是选择人口最少的国家。听起来很容易。但似乎它是不可能的。


如果你按查询使用一个组,你可以返回的是人口,所以你得到1000的答案。这对任何人都没用它并没有告诉你哪个国家有这样的人口。


所以我得出结论我需要两个查询并链接它们。但如果两个国家拥有相同的人口,我只想返回1个国家。哪个国家无关紧要,但我只想归还一条记录。任何想法?


谢谢
Well i can put it simply. Using an example which explains basically what I want. It seems that either Im bad at SQL or SQL is bad. OK here goes

Lets say you have a table of countrys and their populations....

ID........Country...........Popn

1............UK...............1000
2............USA.............2000
3............France..........1500

Ok. Now all i want to do is select the country with the lowest population. Sounds easy. But it seems as though its impossible.

If you use a group by query all you can return is the population so you get an answer of 1000. Which is of no use to anyone because it doesnt tell you which country has that population.

So i have come to the conclusion i need two queries and link them. But if two countrys have same popn I only want to return 1 country. It doesnt matter which country but I only want to return one record. Any ideas?

Thanks



...现在我想做的就是选择人口最少的国家。听起来很容易。但似乎它是不可能的...
...Now all i want to do is select the country with the lowest population. Sounds easy. But it seems as though its impossible...



你需要第三个查询才能做到这一点,由你的第二个查询(与国家加入的那个查询)最低的人口)。在第三个查询的字段列表前使用TOP 1谓词,如下所示:

You will need a third query to do this, fed by your second query (the one with the countries joined back to the lowest populations). Use the TOP 1 predicate before the list of fields in this third query, like this:

展开 | 选择 | Wrap | 行号


这篇关于选择一个最低总计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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