Typeorm groupBy按顺序获取最后一行由另一列

时间:2019-02-18 03:08:55

标签: mysql typeorm

我在使用Typeorm来查询具有重复列<script src="s3-us-west-2.amazonaws.com/s.cdpn.io/138941/teenmax.min.js"></script> <script src="s3-us-west-2.amazonaws.com/s.cdpn.io/138941/waypoints.min.js"></script> <script src="s3-us-west-2.amazonaws.com/s.cdpn.io/138941/classy.js"></script> 和最大值quotationNO的最后一行时遇到麻烦

这是我的桌子

enter image description here

这是我想要的结果 enter image description here

1 个答案:

答案 0 :(得分:1)

下面是一种方法,

SELECT t1.* 
FROM   quotation t1 
       INNER JOIN (SELECT quotationno, 
                          Max(quotationversion ) AS col1 
                   FROM   quotation
                   GROUP  BY quatationversion) t2 
               ON t1.quotationno = t2.quotationno 
                  AND t1.quotationversion = t2.col1;

如果mySql版本为8.0,则还可以使用分析功能。

SELECT <reqcols> 
FROM   ( 
                SELECT   *, 
                         row_number() OVER (partition BY quotationno ORDER BY quotationversion DESC) AS rnk from quotation) a
WHERE  a.rnk=1;

希望这会有所帮助