[Solved] LibreOffice 4.3 Base query calculation

Creating tables and queries
Post Reply
harritapio
Posts: 3
Joined: Tue Aug 18, 2015 2:07 pm
Location: Ulvila, Finland

[Solved] LibreOffice 4.3 Base query calculation

Post by harritapio »

hello,
as a beginner with libreoffice base I have not been able to solve a simple problem:

First question:
In my queries I have this calculation on several fields and I would like to use some variable name for this sentence - but I have not been able to figure out how to do it. The calculation itself is working correctly but to use it as it is now is not very practical

Code: Select all

 CASE WHEN ( ROUND( ( 10 - ABS( DATEDIFF( 'ss', "lahto_t", "tulo_t" ) - 9000 ) / 211.5 ), 0 ) ) < 0 THEN 0 ELSE ROUND( ( 10 - ABS( DATEDIFF( 'ss', "lahto_t", "tulo_t" ) - 9000 ) / 211.5 ), 0 ) END "aikapist"
Second question:
Can I replace the constants "9000" and "211.52" on the sentence with some name and if how?
These two values must be changed depending on application environment.

looking forward to your reply

regards

harritapio
Last edited by harritapio on Sun Aug 23, 2015 5:43 am, edited 4 times in total.
harritapio / libreoffice4.3.3.2 on debian linux "stable" version
User avatar
MTP
Volunteer
Posts: 1620
Joined: Mon Sep 10, 2012 7:31 pm
Location: Midwest USA

Re: libreoffice4.3 base query calculation

Post by MTP »

In Base queries you can include table columns. If you need your value to come from something besides a table column, a query may not be the correct tool - some kind of programming language may be required. You can write a program to build an SQL string differently depending on whatever variable (like application environment) and then execute the custom-built SQL.
OpenOffice 4.1.1 on Windows 10, HSQLDB 1.8 split database
harritapio
Posts: 3
Joined: Tue Aug 18, 2015 2:07 pm
Location: Ulvila, Finland

Re: libreoffice4.3 base query calculation

Post by harritapio »

thanks for your advice!

Maybe I will continue to use the current solution until I have learned some more of libreoffice use. At the moment I'm not able to write a custom build SQL string to libreoffice.
harritapio / libreoffice4.3.3.2 on debian linux "stable" version
eremmel
Posts: 1080
Joined: Tue Dec 30, 2008 1:15 am

Re: libreoffice4.3 base query calculation

Post by eremmel »

You wrote "These two values must be changed depending on application environment." that sounds to me hardly changing data when application environment is setup. So you might use the following:
Define a parameter table 'Application' that will have one row only and the fields: { ID (PK), val1, val2 } with values { 0, 9000, 211.52 }
Now the skeleton for your query becomes:

Code: Select all

SELECT ...
CASE WHEN ( ROUND( ( 10 - ABS( DATEDIFF( 'ss', "lahto_t", "tulo_t" ) - val1 ) / val2 ), 0 ) ) < 0 THEN 0 ELSE ROUND( ( 10 - ABS( DATEDIFF( 'ss', "lahto_t", "tulo_t" ) - val1 ) / val2 ), 0 ) END "aikapist"
FROM table1, Application ....
WHERE Application.ID = 0 ....
....
The filter 'Application.ID = 0' is not strictly needed. You might get more complex queries when you start using aggregates.
It's Microsoft marketing that tells you computers are qualified for non-technicians
W11 22H2 (build 22621), LO 7.4.2.3(x64)
harritapio
Posts: 3
Joined: Tue Aug 18, 2015 2:07 pm
Location: Ulvila, Finland

Re: libreoffice4.3 base query calculation

Post by harritapio »

thank you very much "eremmel",
this was the solution for my question.

As a beginner with libreoffice I had not realised to use second table for predefined variables.
harritapio / libreoffice4.3.3.2 on debian linux "stable" version
Post Reply