Sql Query明确选择

时间:2017-11-29 07:55:25

标签: sql greatest-n-per-group

我的表:

0010121301  000000000001000057  TULUMBA KG  KG  1020    20170911    9.000
0010121302  000000000001000057  TULUMBA KG  KG  1020    20170911    30.000
0010121303  000000000001000057  TULUMBA KG  KG  1020    20170911    10.000
0010121304  000000000001000057  TULUMBA KG  KG  1020    20170911    10.000
0010121305  000000000001000057  TULUMBA KG  KG  1020    20170911    20.000
0010121306  000000000001000057  TULUMBA KG  KG  1020    20170911    10.000
0010121395  000000000001000057  TULUMBA KG  KG  1000    20170911    10.000
0010121399  000000000001000057  TULUMBA KG  KG  1000    20170911    10.000
0010121502  000000000001000057  TULUMBA KG  KG  1033    20170911    5.000
0010121515  000000000001000057  TULUMBA KG  KG  1023    20170911    10.000

如何使用查询从表格的其余部分的表中获取以下结果?

我尝试了很多方法但没有成功。

0010121302  000000000001000057  TULUMBA KG  KG  1020    20170911    30.000
0010121395  000000000001000057  TULUMBA KG  KG  1000    20170911    10.000
0010121502  000000000001000057  TULUMBA KG  KG  1033    20170911    5.000
0010121515  000000000001000057  TULUMBA KG  KG  1023    20170911    10.000

此处WERKS字段将是单数加上记录中的所有字段,这些字段是每个werks字段的最大MENGE字段

4 个答案:

答案 0 :(得分:1)

你可以尝试类似的东西;

<proxies>
  <proxy>
  <id>proxynamehere</id>
  <active>true</active>
  <protocol>http</protocol>
  <username>yourname</username><!-- omit this if you aren't required to authenticate -->
  <password>password</password><!-- omit this if you aren't required to authenticate -->
  <host>proxy.host.com</host>
  <port>8888</port>
  <nonProxyHosts>local.net|some.host.com|127.0.0.1</nonProxyHosts>
</proxy>

答案 1 :(得分:0)

会这样做。但是我不确定我的所有列都是正确的。请在您的问题中为您的数据添加列名。

With maxvalues as ( select max(MENGE) as MaxPrice,MATNR,TXZ01,MEINS,WERKS,LFDAT FROM MYTABLE
GROUP BY MATNR,TXZ01,MEINS,WERKS,LFDAT
)

Select a.BAFNR,b.* from MYTABLE A
inner join MAXVALUES B on a.WERKS = b.WERKS and A.MENGE = b.MENGE

答案 2 :(得分:0)

由于未提及rdbms,因此假设支持窗口OLAP功能,您可以使用row_number(),如下所示。

select t2.banfn, t2.matnr, t2.txz01, t2.meins, t2.col1, t2.werks, t2.lfdat, t2.menge 
from (
     select t1.*, row_number() over(partition by werks order by menge desc) as rn
     from table1 t1
     ) t2
where rn = 1
order by t2.banfn;

<强>结果:

+----------+---------+---------+-------+------+-------+----------+-------+
|  banfn   |  matnr  |  txz01  | meins | col1 | werks |  lfdat   | menge |
+----------+---------+---------+-------+------+-------+----------+-------+
| 10121302 | 1000057 | TULUMBA | KG    | KG   |  1020 | 20170911 |    30 |
| 10121395 | 1000057 | TULUMBA | KG    | KG   |  1000 | 20170911 |    10 |
| 10121502 | 1000057 | TULUMBA | KG    | KG   |  1033 | 20170911 |     5 |
| 10121515 | 1000057 | TULUMBA | KG    | KG   |  1023 | 20170911 |    10 |
+----------+---------+---------+-------+------+-------+----------+-------+

<强> row_number() DEMO

如果不支持row_number,您可以使用相关查询和聚合

select min(t1.banfn) as banfn, t1.matnr, t1.txz01, t1.col1, t1.werks, t1.lfdat, t1.menge
from table1 t1
where t1.menge = 
    (select max(t2.menge)
    from table1 t2
    where t1.werks = t2.werks
     ) 
group by t1.matnr, t1.txz01, t1.col1, t1.werks, t1.lfdat, t1.menge  
order by banfn

<强> Correlated-Query DEMO

答案 3 :(得分:-1)

正如你所说的那样,你只想要在没有任何其他条件的情况下得到明显的结果,你应该尝试类似......

select * from YourTable Y1 inner join 
(select WERKS, MAX(MENGE) as MaxMENGE from 
YourTable group by WERKS) Y2 ON Y1.WERKS = Y2.WERKS and Y1.MENGE = Y2.MaxMENGE

{select * from TABLE_NAME group by DISTINCT_COLUMN_NAME}
您可以使用Change vars in CAPS to actual name.子句来订购