Difficulty with a UNION query

I’m using LibreOffice Base 7.5.7.1., and I have two queries which I’d like to UNION. Both queries work independently, and to ensure the data types match I’ve used CAST. When I use UNION it doesn’t work, and gives the error response “The given command is not a SELECT statement.
Only queries are allowed.” When I ‘run SQL directly’ I get the error response “The data content could not be loaded.” I get this same response with the queries when I run them separately using ‘run SQL directly’. Both queries have come through a few layers of queries involving calculations, and one involves a DISTINCT, if that makes any difference.

SELECT
CAST(“job_id” AS INTEGER) AS “job_id”,
CAST(“task_id” AS INTEGER) AS “task_id”,
CAST(“estimated_task_remainder_minutes” AS REAL) AS “indicative_remainder_minutes”,
CAST(“estimated_task_installation_minutes” AS REAL) AS “indicative_installation_minutes”
FROM
“query_EstimatedLabourFiguresForIndicativeCost”

UNION ALL

SELECT
CAST(“job_id” AS INTEGER) AS “job_id”,
CAST(“task_id” AS INTEGER) AS “task_id”,
CAST(“indicative_remainder_minutes” AS REAL) AS “indicative_remainder_minutes”,
CAST(“indicative_installation_minutes” AS REAL) AS “indicative_installation_minutes”
FROM
“query_IndicativeCostBasicLabourByTaskSingleInstance”;

Did you really set a ‘;’ in the query editor?

When reading the code it seems to be you are trying to UNION queries, not tables or views.
The database you use doesn’t know anything about queries you have saved in Base GUI. So a query, which should be executed directly, could only use tables or views as data source.

You’re right that I was trying to UNION queries, I hadn’t realised that wouldn’t work. I’ve now reworked the whole chain of queries as views, and it works. Thank you! :grinning:

I’m very much a newbie with databases, can you give me a pointer as to what differentiates queries and views? I still don’t quite understand the concept since the output looks the same.

Finally, I’m guessing from your comment me putting in a ‘;’ is a faux pas? :stuck_out_tongue_winking_eye:

Difference between queries and views:
Views will be executed by the database directly. For Base GUI all views will look like tables, which are write protected. Reports will run better with views than with queries if you use alias for a field, functions for calculating … Views are faster, because they will run directly in database.
Queries will be analyzed by Base GUI. If there is a primary key for every table in a query it would be allowed to insert, update and delete values in the query, because Base GUI could find the right table to execute this command.

Colon [;] is never used in the query editor, because the whole code will be tried to execute at once. It will be a separator for executing more than one command in direct SQL (Tools → SQL). So it will show the end of the command. Queries in Base will work with a colon at the end, but not with a colon followed by another command.

That’s helpful to know, thank you for filling me in!

Apache OpenOffice Community Forum - [Solved] Union query alternatives - (View topic)