I have a table where each record contains the time, represented by
an hour column (int) and a minute column (int).For example:
**records |hours| mins**
record1 | 15 | 30
record2 | 12 | 25
I want to write a statement where only the records ahead of the current time will be displayed. So far, I have:
SELECT...
WHERE hours >= hour(current_time)
AND
mins >= minute(current_time)
AND...
But this doesn't work because it needs both the hours and mins to be greater than the current hours and minutes. How do I write to that so that if the hours are the same, then the minutes are compared?
Do something like this:
WHERE hours >= hour(current_time)
OR (hours = hour(current_time) AND mins >= minute(currentime))
Something like
Where hours * 60 + mins > hours(currentTime) * 60 + minute(currentTime)
Related
I'm struggling with this.
I have a column in Snowflake called DURATION, it is VARCHAR type.
The values include basically number in days, hours, minutes, seconds. The value could include either just the number with one unit of time (day or hour or minute or second) such as 3 hours or 14 minutes or 3 seconds or it could include the combination of either all units of time or a few such as 1 day 3 hours 35 minutes or 1 hour 9 minutes or 45 minutes 1 second.
The value could also be blank or invalid such as text or it could be indicating day, hour or minute but without a number (see the last 3 rows in the table below).
I would greatly appreciate it if you guys could help me with the following:
in SNOWFLAKE, convert all valid values to number type and normalize them to minutes (e.g. the resulted value for 7 Hours and 13 Minutes would be 433).
Thanks a lot, guys!
DURATION
1 Second
10 Seconds
1 Minute
3 Minutes
20 Minutes
1 Hour
2 Hours
7 Hours 13 Minutes
1 Hour 1 Minute
1 Day
1 Day 1 Hour
1 Day 1 Hour 1 Minute
1 Day 10 Hours
2 Days 1 Hour
3 Days 9 Hours
1 Day 3 Hours 45 Minutes
Duration (invalid)
Days
Day Minute
Minutes
I tried many things using regex_substr, try_to_number, coalesce functions in CASE statements but I'm getting either 0s or NULL for all values. Very frustrating
I think you would want to use STRTOK_TO_ARRAY in a CTE subquery or put into a temp table. Then you could use ARRAY_POSITION to find the labels and the index one less than the label should be the value. Those values could be put into separate columns with a case for each label pulling the found values. The case statements could be computed columns if you insert the results of the first query into a table. From there you can concatenate colons and cast to a time type and use datediff, or do the arithmetic to calculate the minutes.
I've a PSQL table like this:
Order
Start_Hour
Start_Minute
Finish_Hour
Finish_Minute
10
10
15
12
15
10
12
15
14
15
10
16
00
17
00
And I need to calculate by a query the total time expressed in hours that I spent to finish the order. In this scenario I expect to have a total of 5 hours:
12:15 - 10:15 = 2 hours
14:15 - 12:15 = 2 hours
17:00 - 16:00 = 1 hours
The query result must be 5.
The idea was concatenate start hour/minute and finish hour/minute, convert them to hour, make the difference, calculating the total.
SELECT (Start_Hour & ":" & Start_Minute) as start, (Finish_Hour & ":" & Finish_Minute) as finish
FROM OrderDetails
But when I try to convert them to HH:MM using cast or convert but I got errors.
Any advice?
Thank you
This query uses make_time as Adrian Klaver suggests.
select
"Order",
sum(extract(hour from
make_time("Finish_Hour", "Finish_Minute", 0) -
make_time("Start_Hour", "Start_Minute", 0))
) as duration
from the_table
group by "Order";
However I have remarks about your data design. Hour and minute are not enough for storing time because (apart from missing precision and other reasons) the end time might be over midnight. You have a specific data type for this - timestamp. I would suggest something like
create table the_table
(
order_nr integer,
start_time timestamp,
finish_time timestamp
);
Also note that using mixed case names in Postgresql requires double-quoting.
Use make_time:
select make_time(12, 15, 0) - make_time(10, 15, 0);
?column?
----------
02:00:00
Where in your case you would substitute in Start_Hour, Start_Minute, Finish_Hour, Finish_Minute.
I am trying to add a filter condition in the DB2 database. I am new to it and come from an Oracle background. I am trying to get records with dates in between today at 4 AM and today at 5 PM only. I currently have the below query that returns zero results:
db2 => select datetimeColumn from datetimeExample WHERE datetimeColumn BETWEEN timestamp(current date) - 1 day + 4 hour AND timestamp(current date) - 1 day + 13 hour
DATETIMECOLUMN
--------------------------
0 record(s) selected.
And here is the data in the table that I believe should show but there is something wrong with condition statement, any help is appreciated
db2 => select * from datetimeExample
DATETIMECOLUMN
--------------------------
2016-06-16-09.38.53.759000
1988-12-25-17.12.30.000000
2016-12-25-17.10.30.000000
2016-06-16-04.10.30.000000
2016-06-16-05.10.30.000000
1988-12-25-15.12.30.000000
1988-12-25-14.12.30.000000
2016-06-16-12.10.30.000000
2016-06-16-07.10.30.000000
2016-06-16-08.10.30.000000
10 record(s) selected.
The query should work when you leave out the - 1 day. The reason is that timestamp(current date) returns the timestamp for today at zero hours. Then you add 4 hours and are at the required start time. Similar maths for the end time (and 5 pm should be + 17 hours).
select datetimeColumn from datetimeExample
WHERE datetimeColumn
BETWEEN timestamp(current date) + 4 hours AND timestamp(current date) + 17 hours
I am totally new to SQL and am trying to do some on-the-job training exercises which are currently kicking my rear end.
I am trying to write a query that will look at a table column containing minutes called CC_To_Dep and measure that column against some buckets: 0-30Mins, 30-60Mins, 1-2Hrs, Over 2 Hrs and then return the data to look something like this
Case Count Time
698,523 0-30 Mins
235,888 30-60 Mins
50,853 1-2 Hrs
2,855 Over 2 Hrs
This is what I am using and it labels each record with the time buckets above but it doesn't group them together. I tried a GROUP BY clause at the end but it but then the whole thing errored out.
SELECT
(CC_To_Dep) as CaseCount
,CASE
WHEN(CC_To_Dep)< 30 THEN '0-30Mins'
WHEN(CC_To_Dep)>= 30 AND (CC_To_Dep)<= 60 THEN '30-60Mins'
WHEN(CC_To_Dep)> 60 AND (CC_To_Dep)<= 120 THEN '1-2Hrs'
ELSE 'Over 2 Hrs'
END as Time
FROM Table1
WHERE YEAR(Arr_date)=2013
Any help is appreciated. I have looked around for answers but it is really hard for me to read code in other examples and then apply it to my situation. I'll get there one day but today is not that day.
SELECT
CASE
WHEN(CC_To_Dep)< 30 THEN '0-30Mins'
WHEN(CC_To_Dep)>= 30 AND (CC_To_Dep)<= 60 THEN '30-60Mins'
WHEN(CC_To_Dep)> 60 AND (CC_To_Dep)<= 120 THEN '1-2Hrs'
ELSE 'Over 2 Hrs'
END as Time
, count(*) as caseCount
FROM Table1
WHERE YEAR(Arr_date)=2013
group by
CASE
WHEN(CC_To_Dep)< 30 THEN '0-30Mins'
WHEN(CC_To_Dep)>= 30 AND (CC_To_Dep)<= 60 THEN '30-60Mins'
WHEN(CC_To_Dep)> 60 AND (CC_To_Dep)<= 120 THEN '1-2Hrs'
ELSE 'Over 2 Hrs'
END
I have a table with measures and the time this measures have been taken in the following form: MM/DD/YYYY HH:MI:SS AM. I have measures over many days starting at the same time every day.The datas are minute by minute so basically the seconds are always = 0. I want to select only the measures for the first 5 minutes of each day. I would have used the where statement but the condition would only be on the minutes and note the date is there a way to do this?
Thanks
You could try something like this:
SELECT * FROM SomeTable
WHERE
DATEPART(hh, timestamp_col) = 0 AND -- filter for first hour of the day
DATEPART(mm, timestamp_col) <= 5 -- filter for the first five minutes
Careful! 0 means midnight. If your "first hour" of the day is actually 8 or 9 AM then you should replace the 0 with an 8 or 9.