Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PARTITION BY doesn't work in H2 db

Tags:

oracle

h2

I'm using PARTITION BY clause to do sorting on the result. Details for using PARTITION BY is mentioned in this question Sql Order by on multiple column. It works fine when i run in Oracle. I'm using H2 db for my unit test cases. When i run the same query on H2 db, it doesn't work. Is it known issue in H2? Is there any alternate solution which can work in Oracle and H2 both.

like image 626
Pankaj Avatar asked Jun 04 '13 22:06

Pankaj


People also ask

Can we use partition by in SQL?

The SQL PARTITION BY expression is a subclause of the OVER clause, which is used in almost all invocations of window functions like AVG() , MAX() , and RANK() . As many readers probably know, window functions operate on window frames which are sets of rows that can be different for each record in the query result.

Why H2 database is not used in production?

Mainly, H2 database can be configured to run as inmemory database, which means that data will not persist on the disk. Because of embedded database it is not used for production development, but mostly used for development and testing.

How does DB partitioning work?

Partitioning is the database process where very large tables are divided into multiple smaller parts. By splitting a large table into smaller, individual tables, queries that access only a fraction of the data can run faster because there is less data to scan.

What is MVCC in H2 database?

Multi-Version Concurrency Control (MVCC) That means, if connection A updates a row but doesn't commit this change yet, connection B will see the old value. Only when the change is committed, the new value is visible by other connections (read committed).


1 Answers

I don't think H2 supports window functions (aka analytic functions). However, you can do the query in the link using standard SQL:

SELECT t.*
FROM yourtable t join
     (select vendorname, max(incidentdate) as maxdate
      from yourtable yt
      group by vendorname
     ) vn
     on vn.vendorname = yt.vendorname
ORDER BY vn.maxDate DESC, t.VendorName ASC, t.IncidentDate DESC;

Although this should run in both environments, the over form probably performs better in SQL.

like image 52
Gordon Linoff Avatar answered Oct 16 '22 18:10

Gordon Linoff