We are using the graph compare time and we want to compare the amount of sessions for example between 14 days ago and 28 days ago and same wih 30 days ago and 60 days ago.
We currently wrote date BETWEEN $__timeFrom() AND $__timeTo() and it changes upon selecting the date ranges on the top right but we want the client to be able to change the date ranges and for this query to adjust to show -14 days and -30 days ago as well.
SELECT
$__timeGroup(date, '24h'),
sum(sessions) as sessions
FROM
ourchart
WHERE
date BETWEEN $__timeFrom() AND $__timeTo()
group by time
order by time
EDIT - I am able to make intervals of course but we are trying to compare this month to last month in the same graph....
Play with interval math. Example, which will move your timerange 30 days back:
date BETWEEN
timestamp $__timeFrom() - interval '30 days' AND
timestamp $__timeTo() - interval '30 days'
Related
I want to calculate how far I am through the week and / or the current month in Google Sheets. What's the best way to do this?
Ideally in a single cell too. I also want to account for the difference in number of days from month to month.
see:
month progress:
=1-(EOMONTH(TODAY(), )+"23:59:59.999"-NOW())/
(EOMONTH(TODAY(), )+"23:59:59.999"-(EOMONTH(TODAY(), -1)+1))
week progress:
=1-((TODAY()+7-WEEKDAY(NOW(), 2)+"23:59:59.999")-NOW())/
((TODAY()+7-WEEKDAY(NOW(), 2)+"23:59:59.999")-(TODAY()-WEEKDAY(NOW(), 2)))
demo sheet
For weekly, I did this by taking the current timestamp (now()) and the timestamp at the beginning of the week and converting both to unix epoch times. Then, I used the difference between them and divided both by the number of seconds in a week (518400) and this gave me a decimal for the percent the now() is through the week.
=((now()-DATE(1970,1,1))*86400-((to_date(TODAY()-weekday(now(),3))
+ timevalue("00:00am"))-DATE(1970,1,1))*86400)/518400
For monthly, I did this similarly to the weekly method but, to account for the differing number of days in different months, I calculated the timestamp at the end of the month in unix epoch time and then subtracted the time at the beginning of the month from this to work out how many seconds are in the current month.
=((now()-DATE(1970,1,1))*86400-((to_date(eomonth(today(), -1)+1)
+ timevalue("00:00am")-DATE(1970,1,1))*86400))/((to_date(eomonth(today(), 0))
+ timevalue("23:59")-DATE(1970,1,1))*86400-(to_date(eomonth(today(), -1)+1)
+ timevalue("00:00am")-DATE(1970,1,1))*86400)
I want to set day or hour time interval between 2 columns to get lates actual records, if I dont set this I got outdated information when I filter by day.
TableA: create_date
TableB: last_access_date
SO I did this
SELECT *
FROM acc_zone_person
WHERE create_time::date = last_access_time::date
I get daily information no outdated information, but at midnight all information between 23:00 - 00:00
disgarded I want to put interval so that my evening information from 19:00 will be present till 06:00 data.
I used
SELECT *
FROM acc_zone_person
WHERE last_access_time::date >= now() - interval '12 hours'
this time when I switch to older dates, I dont get any data, only data within 12 hours
so I need to find a way do something like this
SELECT *
FROM acc_zone_person
WHERE create_date::date >= last_access_time - interval '12 hours'
It should take create_date as NOW and get 12 hours interval data of last_access_time
According to your own statement: "It should take create_date as NOW ... "
SELECT *
FROM acc_zone_person
WHERE now()::date >= last_access_time - interval '12 hours'
I am a little confused here. Why couldn't you just use the last query you posted? Does it not work? Do you just need to convert the last_update_time to a date?
I've tried to subtract interval from timestamp, but I've got a wrong result in comparison to days via subtracting 2 dates.
E.g.:
select
(now::date - past::date) as days,
(now::date - past::date) / 365.25 as years,
justify_interval(now - past::date) as interval_test
from (
select '2020-09-17 00:00:01'::timestamp as now, '2010-09-17 00:00:01'::timestamp as past
) b;
gives results:
3653 days
10.0013 years
'10 years 1 mon 23 days' interval test
Could anyone help me to understand what is wrong with subtracting?
When I do it vice versa, it's ok:
select
(past::date + 3653)::date,
(past + interval '10 years')::date,
(past + 10*interval '1 year')::date,
(past + 10*12*interval '1 month')::date
from (
select '2020-09-17 00:00:01'::timestamp as now, '2010-09-17 00:00:01'::timestamp as past
) b;
all results give the same date '2020-09-17'
What I do wrong?
I am using PostgreSQL 10.5.
There is nothing wrong with subtracting. It is just that justify_interval doesn't do what you seem to expect. justify_interval uses 30 day months and 24 hour days. So 12 months becomes only 360 and 10 years only 3600 days. Leaving 53 days which is 1 (30 day) month and 23 days.
Edit
The justify_interval documentation on this page refers to justify_days and justify_hours which are directly above it which do mention the use of 30 days months and 24 hour days.
The justify functions do have to make these assumption because the interval type is a general length of time (it has no specific start and end). So the justify functions does not know over which specific months the interval was originally calculated.
The age function however does not take an interval it takes an end and a start so it actually knows which specific months and years are in that period.
I have a condition here in which I will have total experience in terms of month and year. For example, two drop down will be there for asking total number of experience in month and year. So if I am working from 1 Jan 2012, then I will write total experience as 3 year and 11 months. Now I have to convert this 3 year and 11 months into date format so that I can save this into database
You could use java.util.Calendar:
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, month);
calendar.add(Calendar.YEAR, year);
Date date = calendar.getTime();
As a word of caution, the day field would be set to today's date. Check the intended behaviour if the current day is outside of the bounds for the target month. For example, setting the month to February when calendar has a day field of 30. It might be wise to set the day to a known, valid value for every month (eg: 1) before setting the month and year.
Use DATE_SUB() function:
Try this:
SELECT DATE_SUB(DATE_SUB(CURRENT_DATE(), INTERVAL 3 YEAR), INTERVAL 11 MONTH);
You can use mysql's date_sub() function or <date> - interval <expression> unit syntax to subtract an interval from a date.
select date_sub(curdate(),interval '3-11' YEAR_MONTH) as start_date
UPDATE:
Following the conversation between the OP and #eggyal, the OP need to replace the period in the incoming data with - and construct an insert statement as follows:
insert into mytable (...,join_date,...) values (...,date_sub(curdate(),interval '3-11' YEAR_MONTH),...)
I have tried the following:
add_months(to_Date('04/01/ind.birth_dte','MM/DD/YYYY'), 864) >= to_date('&StartDt','MM/DD/YYYY')
Is there a better way to pull April first of the participant's 72nd birth date?
You could use an interval calculation instead, but not sure how you're defining 'better'. Assuming you do want April 1st of the year in which their 72 birthday falls:
trunc(ind.birth_dte, 'YYYY') + interval '72-3' year to month
The trunc() function goes to the first day of their birth year, and the interval adds 72 years and 3 months to that, which will be April 1st.
SQL Fiddle with some sample dates, including a leap day to show that isn't a problem.
Or to compare that adjusted date with a fixed date as a filter:
where trunc(ind.birth_dte, 'YYYY') + interval '72-3' year to month
> to_date('&StartDt','MM/DD/YYYY');
SQL Fiddle.
You can use the trunc() method with your version as well to save building up a string and calling to_date, adding an additional three months to the add_months call (though I'd suggest you at least need a comment indicating where '867' comes from):
where add_months(trunc(ind.birth_dte, 'YYYY'), 867)
> to_date('&StartDt','MM/DD/YYYY');