[Solved] MAX(X) where X<=:param

Creating tables and queries
Post Reply
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

[Solved] MAX(X) where X<=:param

Post by Villeroy »

Having a table "Prices" in embedded hsqldb, reflecting changing prices over time:

Code: Select all

ID	Date	Article	Price
1	2008-01-01	1	10
2	2008-01-02	2	20
3	2008-01-03	3	30
4	2008-03-01	1	11
5	2008-02-22	2	22
I'd like to have a parameter query to get the price of a given article at the given day.

Code: Select all

SELECT TOP 1 * FROM "Prices" WHERE "Article"=1 AND "Date"<={d '2008-01-15'} ORDER BY "Date" DESC
in direct mode returns the price with ID=1, but I'd like to use it with parameter substitution in a form. So I need something that works in parsed mode.
Last edited by Villeroy on Sun Jun 15, 2008 5:48 pm, edited 1 time in total.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: MAX(X) where X<=:param

Post by Villeroy »

OK, this one seems to do the job:

Code: Select all

SELECT * FROM "Prices" WHERE ("Date" IN (SELECT MAX("Date") FROM "Prices" WHERE "Date"<=:X_Date AND "Article"=:Article)) AND "Article" =:Article
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Post Reply