HSQLDB中的PARTITION BY替代方案

时间:2016-04-20 08:06:01

标签: sql hsqldb window-functions

我想使用 select * (例如

)在HSQLDB数据库上触发https://stackoverflow.com/a/3800572/2968357中建议的查询
Last-Modified: {some date}
Cache-Control: max-age=0, must-revalidate
Expires: -1

但收到以下错误:

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

意味着不支持 PARTITION BY

我的HSQLDB上的特定查询是否有解决办法?

1 个答案:

答案 0 :(得分:2)

HSQLDB支持该答案中的第二个查询。如果您使用HSQLDB DatabaseManager和insert test data菜单中的options,您将获得填充了数据并适用于此类查询的表。

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

但是当您想从两个表中进行选择时,HSQLDB中有一个有趣的替代方案可以很好地执行:

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

此查询返回如下结果:

ID CUSTOMERID FIRSTNAME TOTAL   
-- ---------- --------- ------- 
1  0          Laura     2700.90 
36 1          Robert    4761.60 
27 3          Michael   3420.30 
12 4          Bill      3867.30