I was wondering if there is a way to find the 'median' date in PostgreSQL. The goal is to obtain, according to the median, the date that is in the middle of all the dates.
I tried following modified median function:
select
percentile_cont(0.5) within group (order by date)
from cte
By trying to do so I get the following error message:
SQL Error [42883]: ERROR: function percentile_cont(numeric, timestamp without time zone) does not exist
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
Position: 12
As dates are not supported, I was wondering if there is another way to calculate the median value of a date.
Thank you for any inputs!
You can cast the date value to an integer and then use it for getting the median value using the percentile_cont function.
Like so,
SELECT
percentile_cont(0.5) within group (ORDER by cast(extract(epoch from dateCol1) as integer))
FROM table1
The above gives the median date, but in numeric value, to convert it back to a date type, use the to_timestamp function like so ,
select to_timestamp(1638662400)::date
#gives 2021-12-05
Related
Have a table with a column like this:
first_day_month
01/07/2020
01/07/2020
01/08/2020
01/09/2020
.......
Need to create a column like year-month,
Tried to_char(first_day_month, 'MM/YYYY') but got an error:
Error running query: INVALID_FUNCTION_ARGUMENT: Failed to tokenize string [M] at offset [0]
Tried
concat(extract(year from first_day_month),'-',extract(month from first_day_month) ) as month,
with an error:
Error running query: SYNTAX_ERROR: line 2:1: Unexpected parameters (bigint, varchar(1), bigint) for function concat. Expected: concat(array(E), E) E, concat(E, array(E)) E, concat(array(E)) E, concat(varchar)
Also tried date_parse but didn't get it right, any idea?
Thanks
You need to use TO_DATE first, to convert the column to a proper date. Then use TO_CHAR to format as you want:
SELECT TO_CHAR(TO_DATE(first_day_month, 'DD/MM/YYYY'), 'MM/YYYY') AS my
FROM yourTable;
Note that in this case since the text month year you want is actually just the right substring, you could also directly use RIGHT here:
SELECT RIGHT(first_day_month, 7)
FROM yourTable;
Finally, note that YYYY/MM would generally be a better format to use, as it sorts properly. So perhaps consider using this version:
SELECT TO_CHAR(TO_DATE(first_day_month, 'DD/MM/YYYY'), 'YYYY/MM') AS ym
FROM yourTable;
Your data doesn't seem to be of DATE type, might be string, then need to convert to DATE type first and format display style as desired pattern :
SELECT TO_CHAR(first_day_month::DATE,'MM/YYYY') AS first_day_month
FROM t
Demo
I am trying to calculate the difference between dates in days.
Datatype is Text for columns snapshot_date and date_opened.I am getting an ERROR: function date_part(unknown, integer) does not exist
SELECT DATE_PART('day', snapshot_date::date -date_opened::date)::number from my_table
As documented in the manual subtracting one date from the other returns an integer representing the number of days, so:
snapshot_date::date - date_opened::date
is all you need.
This assumes that both columns can safely be cast to a date.
I am trying to transform a date column to week by using the following code:
select trunc(join_date, 'D') as join_wk from my_table
But I got the following errors:
function trunc(timestamp without time zone, "unknown") does not exist
Hint: No function matches the given name and argument types. You may need to add explicit type casts.
where join_date is of the format:
2017-08-24 14:49:59
What did I do wrong in the query? Thanks!
The name of the function is date_trunk, you have to swap the parameters:
SELECT DATE_TRUNC('D', join_date) AS join_wk FROM my_table
I am trying to calculate the difference between the Maxtime and Mintime however, I think the issue is with converting the 'timestamp' to a date that I can take the difference of. Any help in creating a column that takes the difference of days between these two columns would be so helpful! Thanks!!!
SELECT user,
Min(convert(timestamp,"%m/%d/%Y")) as Mintime,
Max(convert(timestamp,"%m/%d/%Y")) as Maxtime,
( Min(timestamp)-Max(timestamp)) AS Expr1
FROM table1
GROUP BY user
There is no convert function in Access. So you are getting the error.
If you want to convert timestamp to a date you can use the datevalue function in Access.
I'm using Postgres 9.1 and try to get records from a table knowing the given month and should know if the date type date field contains the month.
This is my query:
SELECT consejo.numero,consejo.fecha FROM consejo WHERE MONTH(consejo.fecha) = 2
but I get the following error:
ERROR: There is no month function (date)
LINE 1: ... T consejo.numero, consejo.fecha council FROM WHERE MONTH (cons ...
HINT: No function matches the name and types of arguments. You may need to add explicit type conversion.
might be wrong?
In Postgresql there is no MONTH function available. You can use EXTRACT instead:
WHERE EXTRACT(MONTH FROM consejo.fecha) = 2
Another option is to use PostgreSQL's date_part() function:
WHERE date_part('month', consejo.fecha) = 2
Look at the reference guide:
http://www.postgresql.org/docs/9.5/static/functions-datetime.html