How can I form a SQL Query for a HSQLDB that counts the # of rows per day using a BIGINT type for a date?

StackOverflow https://stackoverflow.com/questions/21101050

質問

I have an HSQLDB, ver. 1.8.0, database with a sample schema with something like this:

CREATE CACHED TABLE ENTRY
(ENTRYID INTEGER GENERATED BY DEFAULT AS IDENTITY(START WITH 0) NOT NULL PRIMARY KEY,
 REQID VARCHAR,
 REVOCATIONDATE BIGINT)

I would like to retrieve a row count for every date, something like this:

ENTRYID  DATE_COUNT REVOCATIONDATE  

1           10       2014-01-01  
2           5        2014-01-02  
3           15       2014-01-03  

The problem is that the REVOCATIONDATE is a BIGINT instead of a normal date or timestamp. This is a vendor provided DB so, assume the schema is not allowed to be changed.

How can I create a SQL query that will do this?

役に立ちましたか?

解決

The simple answer is HSQLDB 1.8 is an old version and does not support some advanced functions. HSQLDB 2.3.x supports a full range of functions to convert between different representations.

With HSQLDB 1.8 it is possible to create your own function in Java to convert the millisecond value of a Timestamp to a java.sql.Timestamp or java.sql.Date

Something like this in a class that you create:

static java.sql.Date millisToDate(long millis) {
    return new java.sql.Date(millis);
}

If you then include this class on the classpath used for running the database, you can call the function in your SELECT statement like this:

SELECT ENTRYID, "myclass.millisToDate"(REVOCATIONDATE) FROM ENTRY

Your result example will look like this:

SELECT MIN(ENTRYID), COUNT(ENTRYID), "myclass.millisToDate"(REVOCATIONDATE) FROM ENTRY
    GROUP BY "myclass.millisToDate"(REVOCATIONDATE)

I haven't actually tried any of the above suggestions, but they should work, perhaps with some alteration.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top