4

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?

Community
  • 1
  • 1
Karl
  • 109
  • 1
  • 10
  • Use the part of that answer with the heading **Supported by any database** –  Apr 20 '16 at 08:12
  • Thanks, but how do i accomplish the select * statement i just added to my question? – Karl Apr 20 '16 at 08:23
  • The CTE (WITH .. AS) query is not necessary unless you use the ROW_NUMBER() OVER(PARTITION .. syntax. – fredt Apr 20 '16 at 17:39

1 Answers1

3

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 
fredt
  • 24,044
  • 3
  • 40
  • 61