[Solved] UNION and Date field in a Query

Creating tables and queries
Post Reply
User avatar
charlie.it
Volunteer
Posts: 417
Joined: Wed Aug 21, 2013 2:12 pm
Location: Italy

[Solved] UNION and Date field in a Query

Post by charlie.it »

Ciao to all,
In the database attached here, I don't understand why this code SQL works

Code: Select all

SELECT ' ', "In", "Out" FROM "Table1" ORDER BY "Date"
UNION 
SELECT 'Total', SUM( "In" ), SUM( "Out" ) FROM "Table1"
and this does't work:

Code: Select all

SELECT ' ',"Date", "In", "Out" FROM "Table1" ORDER BY "Date"
UNION 
SELECT 'Total',"Date", SUM( "In" ), SUM( "Out" ) FROM "Table1" 
It seems that waste the field type date.
Thanks
Attachments
New Database.odb
(3.71 KiB) Downloaded 210 times
Last edited by charlie.it on Thu Sep 15, 2016 3:07 pm, edited 1 time in total.
charlie
Italian AOO Admin
macOS 14 Sonoma M1: Open Office 4.1.15 - LibreOffice 7.5.7.1

http://www.charlieopenoffice.altervista.org
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: UNION and Date field in a Query

Post by Villeroy »

The GROUP BY clause is missing.
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: UNION and Date field in a Query

Post by Villeroy »

Just fill in a single constant date value (not a field of dates) like you fill in a single constant ' ' string in the other missing fileds.

Code: Select all

SELECT '',"Date", "In", "Out" FROM "Table1" ORDER BY "Date"
UNION
SELECT 'Total @',Current_date, SUM( "In" ), SUM( "Out" ) FROM "Table1" 
or make the GROUP BY clause obsolete by using an aggregated date:

Code: Select all

SELECT '',"Date", "In", "Out" FROM "Table1" ORDER BY "Date"
UNION
SELECT 'Total @',MAX("Date"), SUM( "In" ), SUM( "Out" ) FROM "Table1" 
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
charlie.it
Volunteer
Posts: 417
Joined: Wed Aug 21, 2013 2:12 pm
Location: Italy

Re: UNION and Date field in a Query

Post by charlie.it »

I had not thought of Current_date or MAX () / MIN (). :super:
Thank you Villeroy, you are always the best :bravo:
charlie
Italian AOO Admin
macOS 14 Sonoma M1: Open Office 4.1.15 - LibreOffice 7.5.7.1

http://www.charlieopenoffice.altervista.org
chrisb
Posts: 298
Joined: Mon Jun 07, 2010 4:16 pm

Re: [Solved] UNION and Date field in a Query

Post by chrisb »

hello charlie.it,

although this topic is tagged as solved i would like to add a small tip.

data fields are selected & displayed from "Table1".
the UNION clause is used in order to display the summed values of the fields "In" & "Out" on a single row.
therefore as Villeroy has pointed out all selected values within the UNION clause must use either an aggregate function or be included in a group clause.

replacing "Date" with 'NULL' in the UNION clause may in your case provide a more appropriate result.

SELECT '', "Date", "In", "Out" FROM "Table1" ORDER BY "Date"
UNION
SELECT 'Total' ,
null, SUM( "In" ), SUM( "Out" ) FROM "Table1"
open office 4.1.14 & LibreOffice 6.4.4.2 x64 using HSQL 1.8.0.10 (Embedded) and HSQL 2.6.0 (Split) on Windows 10
User avatar
charlie.it
Volunteer
Posts: 417
Joined: Wed Aug 21, 2013 2:12 pm
Location: Italy

Re: UNION and Date field in a Query

Post by charlie.it »

Thank you @chrisb, it works very well. :super:
charlie
Italian AOO Admin
macOS 14 Sonoma M1: Open Office 4.1.15 - LibreOffice 7.5.7.1

http://www.charlieopenoffice.altervista.org
Post Reply