PARTITION BY alternative in HSQLDB

只愿长相守 提交于 2019-12-12 09:59:55

问题


I would like to fire the query suggested in https://stackoverflow.com/a/3800572/2968357 on a HSQLDB database using select * such as

WITH tmpTable AS (
SELECT p.* ,
        ROW_NUMBER()  OVER(PARTITION BY p.groupColumn order by p.groupColumn desc) AS rowCount
  FROM sourceTable p) SELECT * FROM  tmpTable WHERE tmpTable.rowCount = 1

but getting the following error:

Caused by: org.hsqldb.HsqlException: unexpected token: PARTITION required: )

meaning PARTITION BY is not supported.

Is there a work-around for my specific query on HSQLDB?


回答1:


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 


来源:https://stackoverflow.com/questions/36737540/partition-by-alternative-in-hsqldb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!