[Solved] Make report with grouping by month

Getting your data onto paper - or the web - Discussing the reports features of Base
Post Reply
artem78
Posts: 12
Joined: Wed Jun 07, 2023 8:21 pm

[Solved] Make report with grouping by month

Post by artem78 »

Hello.

I have the table which holds rental period dates for some object. Here is example:
Снимок экрана в 2023-11-24 18-41-56.png
Снимок экрана в 2023-11-24 18-41-56.png (6.04 KiB) Viewed 1992 times
I need to make report, which should represents how many days object was occupied each month. For my sample data result should be like this:
Снимок экрана в 2023-11-24 18-44-34.png
Снимок экрана в 2023-11-24 18-44-34.png (4.29 KiB) Viewed 1992 times
Note that occupied period might starts and ends in different months.

Earlier I solved similar task in Sqlite3 using SQL query with WITH RECURSIVE sentence. Unfortunately hsqldb 1.8 used in LibreOffice not supports it (only 2.x version). I tried to upgrade database engine using HyperSQLOOo extension, but with no luck.

Demo db:
rent db demo.odb
(3.58 KiB) Downloaded 192 times
Last edited by robleyd on Sun Dec 03, 2023 1:31 am, edited 2 times in total.
Reason: Add green tick
OpenOffice 4.1.14 on Windows 7 and LibreOffice 6.4.7.2 on Linux Mint 20.3
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Make report with grouping by month

Post by Villeroy »

My way to convert HSQL1 to HSQL2: viewtopic.php?f=21&t=86071

Nevertheless, there is a work-around if you accept a calendar table with dates.
Attachments
rent db demo.odb
(50.34 KiB) Downloaded 199 times
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
Nick N,
Posts: 140
Joined: Wed Aug 09, 2023 2:10 pm

Re: Make report with grouping by month

Post by Nick N, »

Hi,

If it is of interest to you, please find enclosed a partial solution. I.E. you might by yourself select DATE format to suit your needs.

Regards
Attachments
day rental report.png
day rental report.png (22.72 KiB) Viewed 1919 times
rent db demo-rev.1.odb
(5.21 KiB) Downloaded 204 times
Libre Office 6.0.7 on Ubuntu 18.04
chrisb
Posts: 298
Joined: Mon Jun 07, 2010 4:16 pm

Re: Make report with grouping by month

Post by chrisb »

hello artem78,

i am assuming that there is never more than one month between "begin" and "end".
i added the table "tMonths" because we need to know the maximum number of days per month.
i have also catered for leap years.
Rent_chrisb.odb
(5.2 KiB) Downloaded 260 times
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
Nick N,
Posts: 140
Joined: Wed Aug 09, 2023 2:10 pm

Re: Make report with grouping by month

Post by Nick N, »

@ Mr. Villeroy

Thank you Sir. I appreciate your concern.

Regards

Nick
Libre Office 6.0.7 on Ubuntu 18.04
artem78
Posts: 12
Joined: Wed Jun 07, 2023 8:21 pm

Re: Make report with grouping by month

Post by artem78 »

chrisb wrote: Tue Nov 28, 2023 11:32 am i am assuming that there is never more than one month between "begin" and "end".
No. Possible more than one month. Minumum rent period is one day, maximum not limited.
OpenOffice 4.1.14 on Windows 7 and LibreOffice 6.4.7.2 on Linux Mint 20.3
Nick N,
Posts: 140
Joined: Wed Aug 09, 2023 2:10 pm

Re: Make report with grouping by month

Post by Nick N, »

Prastitié Artem,

The reason why i did not do a MONTH query is just because of above concern. I.E. should you wish to know a result in financial terms, I would like you to consider a period divided by DAYS. Else the matter could become potentially COMPLICATED.

Dobrie Dien

Anakin
Libre Office 6.0.7 on Ubuntu 18.04
chrisb
Posts: 298
Joined: Mon Jun 07, 2010 4:16 pm

Re: Make report with grouping by month

Post by chrisb »

artem78 wrote: Tue Nov 28, 2023 7:58 pm maximum not limited
what does that mean 1 year, 5 years, 10 years?

it's not nice to move the goal posts, you should have made this clear in the initial post & example.

i made a small mistake in my first attachment regarding leap years which will result in missing records.
this attachment fixes that error & handles gaps of up to 12 months
check out the query result for Nov 2015 to Mar 2016 which relates to the record in the table "Rents" where ID = 7.
Attachments
Rent2_chrisb.odb
(4.82 KiB) Downloaded 224 times
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
artem78
Posts: 12
Joined: Wed Jun 07, 2023 8:21 pm

Re: Make report with grouping by month

Post by artem78 »

Villeroy wrote: Mon Nov 27, 2023 6:58 pm Nevertheless, there is a work-around if you accept a calendar table with dates.
Yes, this is completely acceptable. I tried your solution and with small modification seems it works as what I need.

Code: Select all

SELECT
	"M", "Y",
	SUM( "Count" ) AS "Count"
FROM
	(
	SELECT
		"ID", "Y", "M",
		COUNT( "D" ) AS "Count"
	FROM (
		SELECT
			"R"."ID", "CAL"."D",
			MONTH( "CAL"."D" ) AS "M",
			YEAR( "CAL"."D" ) AS "Y"
		FROM "CAL", "Rents" AS "R"
		WHERE "CAL"."D" BETWEEN "R"."begin" AND "R"."end"
		)
	GROUP BY "ID", "Y", "M"
	)
GROUP BY "Y", "M"
Thanks, Villeroy!
OpenOffice 4.1.14 on Windows 7 and LibreOffice 6.4.7.2 on Linux Mint 20.3
artem78
Posts: 12
Joined: Wed Jun 07, 2023 8:21 pm

Re: Make report with grouping by month

Post by artem78 »

chrisb wrote: Wed Nov 29, 2023 1:19 am
artem78 wrote: Tue Nov 28, 2023 7:58 pm maximum not limited
what does that mean 1 year, 5 years, 10 years?
Usually rent period lies in the range from several days to several weeks. In rare cases longer.
OpenOffice 4.1.14 on Windows 7 and LibreOffice 6.4.7.2 on Linux Mint 20.3
Nick N,
Posts: 140
Joined: Wed Aug 09, 2023 2:10 pm

Re: Make report with grouping by month

Post by Nick N, »

Hello,

I beg your pardon, but I did try to keep information as elementary as possible.

Glad to learn that you achieved your task.

Regards

Nick
Attachments
elementary example.png
elementary example.png (21.67 KiB) Viewed 1303 times
Libre Office 6.0.7 on Ubuntu 18.04
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Make report with grouping by month

Post by Villeroy »

artem78 wrote: Fri Dec 01, 2023 1:05 pm
Usually rent period lies in the range from several days to several weeks. In rare cases longer.
My solution works with any period of time.
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