PARTITION BY alternative in HSQLDB

最后都变了- 提交于 2019-12-06 08:40:53

The second query in that answer is supported by HSQLDB. If you use the HSQLDB DatabaseManager and the insert test data from its options menu, you get tables that are populated with data and are suitable for this type of query.

SELECT MIN(x.id),  
     x.customerID, 
     x.total
FROM INVOICE x
JOIN (SELECT p.customerID,
             MAX(total) AS max_total
        FROM INVOICE p
    GROUP BY p.customerID) y ON y.customerID = x.customerID
                          AND y.max_total = x.total
GROUP BY x.customerID, x.total

But when you want to select from two tables, there is an interesting alternative in HSQLDB that performs quite well:

SELECT INV.* FROM CUSTOMER, 
LATERAL (SELECT ID, CUSTOMERID, CUSTOMER.FIRSTNAME, TOTAL 
FROM INVOICE  
WHERE CUSTOMERID = CUSTOMER.ID 
ORDER BY TOTAL, ID LIMIT 1) INV

This query returns a result like this:

ID CUSTOMERID FIRSTNAME TOTAL   
-- ---------- --------- ------- 
1  0          Laura     2700.90 
36 1          Robert    4761.60 
27 3          Michael   3420.30 
12 4          Bill      3867.30 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!