通过变量的总和生成前n个查询 [英] Generating a query of top n by sum of a variable

查看:61
本文介绍了通过变量的总和生成前n个查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下格式的数据:

I''ve got data in the following format:

展开 | 选择 | Wrap | 行号

推荐答案

SELECT子句的TOP谓词属于整个查询,并且无法定义为在每个组中工作。 IE浏览器。使用该SQL返回的内容正是预期的结果 - 一条记录(如果记录具有相同的值,则更多)。


您可以尝试以下SQL并查看是否它适用于TOP n,否则

The TOP predicate of the SELECT clause pertains to the query as a whole and is nowhere defined as working within each group. IE. What you are returning with that SQL is exactly what would be expected - One record (or more if there were records with equal values).

You could try out the following SQL and see if it works for you for TOP n, otherwise

展开 | 选择 | Wrap | 行号


我认为你不能参考来自FROM子句中子查询的外部查询。但是,您应该能够在WHERE子句中移动该逻辑,它也应该可以正常工作。


话虽如此,另一种方法是将表连接到自身并使用聚合实现类似的结果。
I don''t think you can refer to an outer query from a subquery in the FROM clause. However, you should be able to move that logic within the WHERE clause and it should work just as well.

Having said that, another method is to join the table to itself and use aggregates to achieve a similar result.
展开 | 选择 | Wrap | < span class =codeLinkonclick =LineNumbers(this);>行号


我怀疑你可能是对兔子,但你使用不同的字段名称使它成为现实我很难跟上你想要说明的内容。即使我更改名称,我也看不到它匹配的位置,但是SQL的错误程度以及我无法遵循的程度尚不清楚。
I suspect you may be right Rabbit, but you use of different field names makes it very hard for me to follow what you''re trying to illustrate. Even if I change the names I can''t see where it matches, but how much is wrong with the SQL and how much is my inability to follow it is not clear.


这篇关于通过变量的总和生成前n个查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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