Want to make the output human readable - oracle

Hello, I am trying to run some queries but my output is not right. Could anyone please help me understand what I'm doing wrong?
I am trying to find the difference between these two DATETIME values as minutes.
SELECT TO_CHAR(booking_StartTime - booking_EndTime) AS Diff FROM Booking;

Assuming that booking_StartTime and booking_EndTime are DATE fields - when doing arithmetic using DATE values in Oracle the result is a number of DAYS. Thus, to get minutes you have to multiply by the number of minutes in a day, i.e. by 24 * 60, or 1440. Also - you probably want to subtract the start time from the end time, in order to get a positive value as the result.
SELECT TO_CHAR((booking_EndTime - booking_StartTime) * 1440) AS Diff FROM Booking;
should get you what you want.

Related

Impala date subtraction timestamp and get the result in equivalent days irrespective of difference in hours or year or days or seconds

I want to subtract two date in impala. I know there is a datediff funciton in impala but if there is two timestamp value how to deal with it, like consider this situation:
select to_date('2022-01-01 15-05-53','yyyy-mm-dd HH24-mi-ss')-to_date('2022-01-01 15-04-53','yyyy-mm-dd HH24-mi-ss') from dual;
There is 1 minute difference and oracle would put the result as 0.000694444 days.
My requirement is if there is any such functionality in impala where I can subtract two timestamp value in the manner 'yyyy-mm-dd HH24-mi-ss', and get the result in equivalent days irrespective of if there is difference in days , year, hours, minute or seconds. Any difference should reflect in equivalent number of days.
Any other way where I can achieve the same thing, I am open to that as well.
Thank you in advance.
You can use unix_timestamp(timestamp) to convert both fields to unixtime (int) format. This is actually seconds from 1970-01-01 and very suitable to calculate date time differences in seconds. Once you have seconds from 1970-01-01, you can easily minus them both to know the differences.
Your sql should be like this -
select
unix_timestamp(to_timestamp('2022-01-01 15-06-53','yyyy-MM-dd HH-mm-ss')) -
unix_timestamp(to_timestamp('2022-01-01 15-05-53','yyyy-MM-dd HH-mm-ss')
) diff_in_seconds
Once youhave difference in seconds, you can easily convert them to minutes/hours/days - whatever format you want it.

Using Power BI Desktop, how can I present Time Duration in a matrix that converts it to decimal?

I have Begin Date and End Date in my data file (Excel), then in PowerQuery I add a column where I simply subtract the Begin Date from End Date to create a new column "Import Time", change the result's data type to Duration and in PowerQuery, the column correctly shows the difference expressed in Day:Hour:Minute:Seconds. However, in a report, I need to show the average not of an entire column, but of groups of values in a matrix based on the entire table. In other words, I'm looking for a table that shows columns for Category Name, Avg. Duration and Max Duration, with rows showing the results for each unique Category.
The problem is that when I drag a table field in to the Visualizations Value area and change to Average, it expresses the result as a decimal, and I can't figure out how to show the date / time equivalent. I thought I might have to multiply by 3,600 since the result might be shown in milliseconds, but that didn't work. Any thoughts?
So I theorized that perhaps the issue wasn't one with Power BI, but perhaps with how Microsoft expresses date / time computations. I edited my Excel file that contains the source data and computed the "Import Time" by subtracting Begin Time from End Time. By default, it displays in date / time format of "hh:mm:ss.0000". I then asked Google and came across a simple explanation: to express a date / time in numeric format, you need to multiply the date / time by 24 (hours), 60 (minutes) and 60 (seconds) if that's how you wanted to express the result. I then created a Pivot Table summarizing the average of the Import Times and saw the same thing I did in my Power BI report, which is when it clicked it for me.
To solve the issue, I edited my Power Query step to compute the import time and included " * 24 * 60 * 60" in the formula. After updating the report, the results matched what I saw in Excel and I'm good. Hopefully this helps others to deal with this vexing issue.
You wrap your averaging measure in a FORMAT function to convert it to precisely the text format you want to show in your matrix. For example, suppose your raw output is
Group AvgDuration
------------------
A 0.0633
B 0.2733
C 0.0600
These numbers are in the unit of days, so you can convert to hours, minutes, or seconds like this e.g.:
FORMAT ( [AvgDuration] * 24, "0.00h" )
FORMAT ( [AvgDuration] * 24 * 60, "#,0m" )
FORMAT ( [AvgDuration] * 24,* 60 * 60, "#,0s" )
FORMAT ( [AvgDuration], "hh:mm:ss" )
(where [AvgDuration] is your measure you use to calculate the average)
Those last two should look like this:
Group AvgDuration
------------------
A 5,472s
B 23,616s
C 5,184s
and
Group AvgDuration
------------------
A 01:31:12
B 06:33:36
C 01:26:24

I need to calculate difference between two datetime fields, THEN show a percentage based on the first calculation

I need to calculate the difference between the time a patient arrives for their appointment, and the time they check out, also displaying the default length of their appointment, THEN show the percentage of their default appointment time that was actually used.
I've calculated the first part:
,TRUNC(24*MOD(enc.CHECKOUT_TIME - enc.CHECKIN_TIME,1))|| ':' ||TRUNC(MOD(MOD(enc.CHECKOUT_TIME - enc.CHECKIN_TIME,1)*24,1)*60) AS "VISIT LENGTH"
The default appointment duration is in this field: enc.APPT_LENGTH. This is the part I'm not sure how to do - calculate the percentage of the APPT_LENGTH that was actually used (VISIT_LENGTH) as calculated by the above.
Thanks in advance for any assistance.
Your question is not very clear but, to get the actual duration of an appointment in hours (decimal system), you can use:
(enc.CHECKOUT_TIME - enc.CHECKIN_TIME) * 24 AS ACTUAL_DURATION
since using - with 2 dates in Oracle results in the difference in days between the 2 dates.
Now, if APPT_LENGTH contains the expected duration of the appointment in hours too, you can then use
(((enc.CHECKOUT_TIME - enc.CHECKIN_TIME) * 24) / enc.APPT_LENGTH) * 100 AS DURATION_RATIO
to calculate the ratio (in %) between the actual duration and the expected duration

combining date and time and changing it to GMT

I have read many answers for combining date and time and nothing worked so far. I am working in Oracle SQL developer version 3.1.06 and I am trying to combine date and time stamps together. Date is in format dd-mmm-yy. And time is in the following 3 formats-
1. 0348A-- meaning 3:48 am
2. 03:48:00
3. 228 -- meaning minutes from midnight, calculated as (3*60)+48.
And for all these timestamps, I want a query that gets me to this format --
mm/dd/yyyy hh:mm:ss .
I can change the dates and times to string and attach them, but then when I work in powerpivot I am not able to change them to the required format. So, I want to do it in the query itself.
I have already tried something like this-
1. CAST(deptdt as DATETIME)+CAST(time as DATETIME)
2. CAST(depdt AS TIMESTAMP(0)) + (depdt - TIME '00:00:00' HOUR TO SECOND) AS DATETIME
Please help!!

Openedge syntax for TIME minus 4 hours

I am trying to figure out how to write a Openedge query where i can look back 4 hours. i have struggled with the TIME syntax before. If i understand correctly, the TIME representation in Openedge is in seconds starting from midnight. The query i am trying to write would run 4 times a day, looking back 4 hours.
Is there any way to do this using TIME? Maybe i have to write 4 different queries that only pull records starting at a specific time?
Thanks very much for any help i can get, it is greatly appreciated!
Martin
You don't describe the contents or layout of the table very well.
Yes, TIME, in Progress ABL contains the number of seconds since midnight. So 16:20 for instance is 58800 (16 * 3600 + 20 * 60).
Assuming the field in your table contains an integer representing the time you can do like this to select the records that was created up to four hours ago:
DEFINE VARIABLE iTime AS INTEGER NO-UNDO.
/* I find it easier to write like this but you can very well do = TIME - 14400 instead */
iTime = TIME - 4 * 3600.
FOR EACH tablename NO-LOCK WHERE tablename.createtime >= iTime:
/* Do something */
END.
Note: perhaps you need to check create date as well? And handle midnight?
Another option might be to look at the DATETIME type. There you can do operations like adding and distracting an amount of time.
DEFINE VARIABLE datnow AS DATETIME NO-UNDO.
DEFINE VARIABLE datthen AS DATETIME NO-UNDO.
datnow = NOW.
DISPLAY datnow.
datthen = ADD-INTERVAL(datnow, -4, "hours").
DISPLAY datthen.

Resources