[Solved] SQL Query: Need "Time" + "MinuteLen" as "EndTime"
-
- Posts: 9
- Joined: Tue Mar 19, 2024 4:00 pm
- Location: Albuquerque, NM, USA
[Solved] SQL Query: Need "Time" + "MinuteLen" as "EndTime"
I have a database of student lesson records. The Lessons table includes a lesson start time and a length in minutes. How do I add a time field with the (start time + length)?
This is the current relevant parts of the SQL query without an attempted end time:
SELECT "Lessons"."Date",
"Lessons"."Lesson.Time" AS "Time",
"Lessons"."Length",
FROM "Lessons", "Students" WHERE "Lessons"."Date" >= CURRENT_DATE
Embedded database: HSQL database engine
This is the current relevant parts of the SQL query without an attempted end time:
SELECT "Lessons"."Date",
"Lessons"."Lesson.Time" AS "Time",
"Lessons"."Length",
FROM "Lessons", "Students" WHERE "Lessons"."Date" >= CURRENT_DATE
Embedded database: HSQL database engine
Last edited by Panaiotis82 on Wed Mar 20, 2024 7:53 pm, edited 5 times in total.
OpenOffice 4.1.15
Mac OS 13.5.2
Mac OS 13.5.2
Re: SQL Query: Need "Time" + "MinuteLen" as "EndTime"
Embedded HSQL does not have a DateAdd function like most other database engines have. As a work-around, you can calculate the day fractions and add them as you would do in a spreadsheet. A formatted control on a form or report can display this value as a time.
Code: Select all
SELECT *,
Hour("Time")/24 + Minute("Time")/1440 + Hour("Length")/24 + Minute("Length")/1440 AS "DayFraction"
FROM "Lessons"
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
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
-
- Posts: 9
- Joined: Tue Mar 19, 2024 4:00 pm
- Location: Albuquerque, NM, USA
Re: SQL Query: Need "Time" + "MinuteLen" as "EndTime"
After 4 hours of failed attempts to use and modify your example, I figured out a solution. First, the Length value is an integer, not a time value, so the Minute("Length") function crashed OO. However, removing the MINUTE() function always returned a value of zero until I added several zeros after a decimal point in the division value. The following example works:
The field types and names are: time: "Time", int: "Length"
The formula that works is:
SELECT "Time", "Length", HOUR( "Time" ) /24.00000 + "Length" /1440.00000 AS "End" FROM "Lessons"
Then I change the "End" column to a Time format. This works as long as the Length value is not blank (NULL?)
The field types and names are: time: "Time", int: "Length"
The formula that works is:
SELECT "Time", "Length", HOUR( "Time" ) /24.00000 + "Length" /1440.00000 AS "End" FROM "Lessons"
Then I change the "End" column to a Time format. This works as long as the Length value is not blank (NULL?)
OpenOffice 4.1.15
Mac OS 13.5.2
Mac OS 13.5.2
Re: [Solved] SQL Query: Need "Time" + "MinuteLen" as "EndTime"
Any operation with a Null value returns Null.
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
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
-
- Posts: 9
- Joined: Tue Mar 19, 2024 4:00 pm
- Location: Albuquerque, NM, USA
Re: [Solved] SQL Query: Need "Time" + "MinuteLen" as "EndTime"
Yes, I see that. Thanks for your help. I've been trying for years to solve the time issue: literally. I will try something similar for dates. Hpefully I will be able to work out the details on my own.
OpenOffice 4.1.15
Mac OS 13.5.2
Mac OS 13.5.2
-
- Posts: 9
- Joined: Tue Mar 19, 2024 4:00 pm
- Location: Albuquerque, NM, USA
Re: SQL Query: Need "Time" + "MinuteLen" as "EndTime"
I reset this as unsolved because the solution only partially works, and has exposed anomalous and finicky behavior. While I was able to get something to work under extremely limited conditions, implementing it in the necessary or any usable context consistently fails.
The SQL editor requires the formula to be without spaces: e.g. HOURS("Time")/24.00000. However, after closing the editor and re opening it, the spaces are added back and have to be removed before saving any edits.
Worse than that, this formula doesn't work in a table form at all.
In a query, OO crashes if the query includes references to a linked table. These references work if there are no calculated fields, so I can either have the calculation or the reference fields, but not both.
There is probably some magic combination that will provide an acceptable solution, but I have been unable to find it, and even systematically trying different combinations has not gotten me any closer.
The SQL editor requires the formula to be without spaces: e.g. HOURS("Time")/24.00000. However, after closing the editor and re opening it, the spaces are added back and have to be removed before saving any edits.
Worse than that, this formula doesn't work in a table form at all.
In a query, OO crashes if the query includes references to a linked table. These references work if there are no calculated fields, so I can either have the calculation or the reference fields, but not both.
There is probably some magic combination that will provide an acceptable solution, but I have been unable to find it, and even systematically trying different combinations has not gotten me any closer.
OpenOffice 4.1.15
Mac OS 13.5.2
Mac OS 13.5.2
Re: SQL Query: Need "Time" + "MinuteLen" as "EndTime"
original post deleted by author.
i had uploaded an attachment which demonstrated an alternative formula in query & form.
the attachment was never downloaded by Panaiotis82 so therefore i have withdrawn my contribution.
i had uploaded an attachment which demonstrated an alternative formula in query & form.
the attachment was never downloaded by Panaiotis82 so therefore i have withdrawn my contribution.
Last edited by chrisb on Thu Mar 21, 2024 5:43 am, edited 1 time in total.
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
-
- Posts: 334
- Joined: Sun Sep 06, 2020 8:27 am
Re: SQL Query: Need "Time" + "MinuteLen" as "EndTime"
Functions IFNULL or COALESCE can provide an alternative value for NULL likePanaiotis82 wrote: ↑Tue Mar 19, 2024 10:21 pm ..."
The formula that works is:
SELECT "Time", "Length", HOUR( "Time" ) /24.00000 + "Length" /1440.00000 AS "End" FROM "Lessons"
Then I change the "End" column to a Time format. This works as long as the Length value is not blank (NULL?)
Code: Select all
SELECT "Time", "Length", HOUR( "Time" ) /24.00000 +
COALESCE("Length" /1440.00000, 0) AS "End" FROM "Lessons"
LibreOffice 7.6 on Windows 10pro and other Versions parallel
-
- Posts: 334
- Joined: Sun Sep 06, 2020 8:27 am
Re: SQL Query: Need "Time" + "MinuteLen" as "EndTime"
Never seen that, but I'm using LibreOffice.Panaiotis82 wrote: ↑Wed Mar 20, 2024 2:58 am ....
The SQL editor requires the formula to be without spaces: e.g. HOURS("Time")/24.00000. However, after closing the editor and re opening it, the spaces are added back and have to be removed before saving any edits.
...
If you wish to keep your precise query, you may set "direct SQL"-mode. (Rightmost icon, if I'm remembering right). You can't use :param then, but OpenOffice should not change a single character in your query.
LibreOffice 7.6 on Windows 10pro and other Versions parallel
-
- Posts: 334
- Joined: Sun Sep 06, 2020 8:27 am
Re: SQL Query: Need "Time" + "MinuteLen" as "EndTime"
Show the code here, so one can check, because this is not a real restriction of Base, even if you may have found a bug.Panaiotis82 wrote: ↑Wed Mar 20, 2024 2:58 am...
In a query, OO crashes if the query includes references to a linked table. These references work if there are no calculated fields, so I can either have the calculation or the reference fields, but not both.
...
Work-arounds: Try direct SQL, if the cause is Base when it interprets your query or save a working query as View in the database (e.g. with calculated fields), then add the reference fields using the view as datasource.
LibreOffice 7.6 on Windows 10pro and other Versions parallel
-
- Posts: 9
- Joined: Tue Mar 19, 2024 4:00 pm
- Location: Albuquerque, NM, USA
Re: SQL Query: Need "Time" + "MinuteLen" as "EndTime"
For some inexplicable reason OO is behaving better today. Here is the SQL line that works:
SELECT "Lessons"."Was.Taught" AS "Taught", "Students"."Name.first", "Students"."Name.last", "Lessons"."Date" AS "Date",
"Lessons"."Lesson.Time" AS "Time", "Lessons"."Length" AS "Length",
HOUR( "Lesson.Time" ) / 24.000000000 + MINUTE( "Lesson.Time" ) / 1440.000000000 + "Length" / 1440.000000000 AS "End",
"Lessons"."Assignments" AS "Assignments", "Lessons"."notes" AS "Notes" FROM "Lessons", "Students", "Date-1" WHERE "Lessons"."Student.ID" = "Students"."Student.ID" AND "Lessons"."Was.Taught" = FALSE AND "Lessons"."Date" >= "Date-1"."First" AND "Lessons"."Date" <= "Date-1"."Last" ORDER BY "Date" ASC, "Time" ASC
Notice how many zeros past the decimal places it takes to give me clean values when the result is formatted as a time.
SELECT "Lessons"."Was.Taught" AS "Taught", "Students"."Name.first", "Students"."Name.last", "Lessons"."Date" AS "Date",
"Lessons"."Lesson.Time" AS "Time", "Lessons"."Length" AS "Length",
HOUR( "Lesson.Time" ) / 24.000000000 + MINUTE( "Lesson.Time" ) / 1440.000000000 + "Length" / 1440.000000000 AS "End",
"Lessons"."Assignments" AS "Assignments", "Lessons"."notes" AS "Notes" FROM "Lessons", "Students", "Date-1" WHERE "Lessons"."Student.ID" = "Students"."Student.ID" AND "Lessons"."Was.Taught" = FALSE AND "Lessons"."Date" >= "Date-1"."First" AND "Lessons"."Date" <= "Date-1"."Last" ORDER BY "Date" ASC, "Time" ASC
Notice how many zeros past the decimal places it takes to give me clean values when the result is formatted as a time.
OpenOffice 4.1.15
Mac OS 13.5.2
Mac OS 13.5.2