简体   繁体   English

SQL从组中按最高值选择一行

[英]SQL select a row by highest value from a group

I'm trying to select the highest value(calculated) of a group with distinct selection of other columns 我正在尝试通过其他列的不同选择来选择组中的最高值(计算得出)

according to the table-data below, i want to select the rows, which have the highest amount (Qty-Plan) and a distinct selection of Len and Wid 根据下面的表格数据,我要选择数量最多的行(数量计划),并选择不同的Len和Wid

Table data is as follow 表数据如下

+-----------+-----------+---------+---------+------------+---------+
|   Ident   |   Name    | Len     |   Wid   |      Qty   |    Plan |
+-----------+-----------+---------+---------+------------+---------+
|  12345    | Name1     | 1500    |    1000 |         20 |       5 |
|  23456    | Name1     | 1500    |    1000 |         30 |      13 |
|  34567    | Name1     | 2500    |    1000 |         10 |       2 |
|  45678    | Name1     | 2500    |    1000 |         10 |       4 |
|  56789    | Name1     | 1500    |    1200 |         20 |       3 |
|  00001    | Name2     | 1500    |    1200 |         10 |       6 |
|  00002    | Name2     | 1500    |    1200 |         20 |       7 |
|  00003    | Name3     | 1500    |    1200 |         30 |       5 |
|  00004    | Name3     | 1500    |    1200 |         40 |       4 |
+-----------+-----------+---------+---------+------------+---------+

with my query i cant erase the "lower" values: 与我的查询,我无法删除“较低”的值:

select a.Ident ,a.Name, a.Len,a.Wid, a.Qnt-a.Plan as Amount
from table a
join (select ident, max(Qnt - Plan) Amount
      from table
      where Name = 'Name1'
      group by Ident, Len, Wid) b
  on b.Ident = a.Ident and b.Amount = a.Qnt-a.Plan
order by Amount desc

off-topic question: why i cant use -> where b.Amount = a.Amount (he does not know a.Amount ) ??? 离题的问题:为什么我不能使用-> where b.Amount = a.Amount(他不知道a.Amount)?

my desired select should look like: 我想要的选择应如下所示:

+-----------+-----------+---------+---------+------------+
|   Ident   |   Name    | Len     |   Wid   |   Amount   |
+-----------+-----------+---------+---------+------------+
|  56789    | Name1     | 1500    |    1200 |         18 |
|  23456    | Name1     | 1500    |    1000 |         17 |
|  34567    | Name1     | 2500    |    1000 |          8 |
+-----------+-----------+---------+---------+------------+

thanks a lot in advance 提前非常感谢

It's not clear what kind of database you're using, but this solution should work on any DB: 尚不清楚您使用的是哪种数据库,但是此解决方案应可在任何数据库上使用:

SELECT tab.Ident,
       tab.Name,
       tab.Len,
       tab.Wid,
       (tab.Qty - tab.Plan) AS Amount
FROM   (SELECT   Name,
                 Len,
                 Wid,
                 MAX(Qty-Plan) AS Amount
        FROM     my_table
        GROUP BY Name,
                 Len,
                 Wid
       ) AS grouped
JOIN   my_table tab
  ON   grouped.Name = tab.Name
 AND   grouped.Len = tab.Len
 AND   grouped.Wid = tab.Wid
 AND   grouped.Amount = (tab.Qty - tab.Plan)
 AND   tab.Name = 'Name1'

Another approach, using window functions to simplify things: 另一种方法是使用窗口函数来简化操作:

SELECT ident, name, len, wid, qnt - [plan] AS amount
FROM  (SELECT *, row_number() OVER (PARTITION BY len, wid ORDER BY qnt - [plan] DESC) AS rn
       FROM test WHERE name = 'Name1') AS sq
WHERE rn = 1
ORDER BY amount DESC;

SQL Fiddle example . SQL Fiddle示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM