I have records with values like: date, hour, value. Aggregating whole day is easy, i just group by date. My problem is that i need to aggregate records from whole days with specific start/end time. In example when start time is 9 a.m. then i have records:
- from monday 9 a.m. to tuesday 8 a.m (grouped in one record)
- from tuesday 9 a.m. to wednesday 8 a.m. (grouped in one record)
and so on.
Thanks, for any ideas.
I'm going to assume your hour field is an INT, but regardless you should be able to adapt this...
GROUP BY
CASE WHEN hour >= 9 THEN date ELSE date - 1 END
This essentially treats any hour before 9am as being part of the previous day.
GROUP BY date_trunc('day',date+(hour-9)*interval '1h') or something like that?
Related
I'm working on a booking platform which has several different rates. These rates are determined by the time of day, day of week, and day of year. Here are some examples of the interval types involved:
Monday to Friday, 9am to 5pm
Saturday and Sunday, 12am to 9am
Saturday and Sunday, 9am to 5pm
Saturday and Sunday, 5pm to 12am
December 23rd & 24th, anytime
December 26th & 27th, anytime
What is the best way to represent this, such that it's possible to query for the different effective rates on any given day?
At the moment, the way I've done is using two array type columns, days_of_week[] and hours_of_day[], populating them with the days/hours each rate applies. To account for special cases like December, I also have fields valid_from and invalid_after, however this requires a new entry for each year.
I've had a look at the datetime functions for intervals and such here but haven't seen anything that looks like it could solve this.
why not just listing them in where clause? eg for first sample:
t=# select now(),extract('dow' from now()) between 1 and 5 and now()::time between '09:00' and '17:00';
now | ?column?
-------------------------------+----------
2017-11-27 16:56:01.544642+00 | t
so you take (extract('dow' from now()) between 1 and 5 and now()::time between '09:00' and '17:00') to brackets and add same brackets over OR...
You can addthem all to a function with timestamptz as argument and return true of false to use in where clause
I have a date in sql which will always fall on a Monday and I'm subtracting 13 weeks to get a weekly report. I am trying to get the same 13 week report but for last year's figures as well.
At the moment, I'm using the following:
calendar_date >= TRUNC(sysdate) - 91
which is working fine.
I need the same for last year.
However, when I split this into calendar weeks, there will also be a partially complete week as it will include 1 or 2 days from the previous week. I need only whole weeks.
e.g. the dates that will be returned for last year will be 14-Feb-2015 to 16-May-2015. I need it to start on the Monday and be 16-Feb-2015. This will change each week as I am only interested in complete weeks...
I would do this:
Get the date by substracting 91 days as you're already doing.
Get the number of the day of the week with TO_CHAR(DATE,'d')
Add the number of days until the next monday to the date.
Something like this:
SELECT TO_DATE(TO_DATE('16/05/2015','DD/MM/YYYY'),'DD/MM/YYYY')-91 + MOD(7 - TO_NUMBER(TO_CHAR(TO_DATE(TO_DATE('16/05/2015','DD/MM/YYYY'),'DD/MM/RRRR')-91,'d'))+1,7) d
FROM dual
next_day - returns date of first weekday named by char.
with dates as (select to_date('16/05/2015','DD/MM/YYYY') d from dual)
select
trunc(next_day( trunc(d-91) - interval '1' second,'MONDAY'))
from dates;
I want to get next monday from calculated date. In situation when calculated date is monday i have to move back to previous week ( -1 second).
Have table: item (int) and timestamp (datetime).
Need to know if there are any records with timestamp after last 6 AM.
Example:
At 5 AM it should check if there are any records from after 6 AM
yesterday. AT 7 AM it should check if there are any records from
after 6 AM today
This could be done of course by making a variable with datepart as:
if time now is < 6 AM datepart should be yesterday if time now
is >= 6 AM datepart should be today
but there must be a simpler way ?
This expression should always return the most recent 06:00 in the past:
select DATEADD(hour,
(DATEDIFF(hour,'2014-01-01T06:00:00',CURRENT_TIMESTAMP)/24)*24,
'2014-01-01T06:00:00')
It works by asking how many hours have happened since some arbitrary, known, 6AM. We then round this number down to the closest multiple of 24 (by dividing and multiplying with integer maths), and add this number back onto the same, arbitrary, 6AM
I am having a problem with making a Oracle Query that sums up everything that occurs during the whole last week and everything during this week. so lets say i have this table
DATA DATE
3 2-feb-15
4 3-feb-15
6 3-feb-15
7 27-jan-2015
5 27-jan-2015
4 25-jan-2015
so lets today is feb 5th and the query this week should be 3+4+6 assuming that sunday is feb 1st. The this week's data range should be (feb 2nd - feb 5th) minus sunday.
and the sum for last week should be 7+5+4 no matter what date today is as long as it still last week from Monday to saturday
Another case is, if todays date is feb 2nd, then the sum should be 3.
Please help me, i tried with SYSDATE - (Var) and it doesnt work because it doesnt position the date according to the calendar
thank you very very much
try this :
trunc(sysdate)
I hope its working if its not please give me more detail of your problem
I want to perform an operation in crystal report.
I have a db table contains a date column.
I want to filter and get the rows having data created in last week(last sunday to last saturday = 7 days).For example if today is 24th August Wednesday, then I need data from 14th August(Sunday) to 20th August(Saturday).
Basically I want to find 2 dates and filter the date column.
Date1 = Date(CurrentDate)-Day(7 + WeekDayinNum(CurrentDate)) ; (Ex:for my example it will be 10)
Date2 = Date(CurrentDate)-Day(WeekDayinNum(CurrentDate))
I do not know the Date APIs properly,can anybody help me in this.
This is a common enough date range that CR provides it for you. In your record selection formula, you can just add {table.date} in LastFullWeek
From CR, "LastFullWeek specifies a range of Date values that includes all dates from Sunday to Saturday of the previous week."
Add this to the record selection formula:
{table.date_field} IN Last7Days
If today is Sunday(1) you want rows that are between 7 and 1 days old,
If today is Monday(2) you want rows that are between 8 and 2 days old,
If today is Tuesday(3) you want rows that are between 9 and 3 days old,
etc.
SELECT *
FROM `tablename`
WHERE `somedatefield` >= DATE_SUB(NOW(),INTERVAL (DAYOFWEEK(NOW()) + 6) DAY)
AND `somedatefield` <= DATE_SUB(NOW(),INTERVAL (DAYOFWEEK(NOW())) DAY)