In a PostgreSQL table I have two columns (int8) containing a Unix timestamp. [Note: this table is not mine, so the columns can be changed to an actual date, hence the conversion in the query.]
below is a stripped down version of my query. What I need to do is to group the entries by month. But in my query below, the entries november 2020 get grouped with the entries of november 2021.
select
DATE_PART('month',to_timestamp(e.startts)) as "Date", sum(e.checked)
from entries e
where
e.startts >= date_part('epoch', '2020-10-01T15:01:50.859Z'::timestamp)::int8
and e.stopts < date_part('epoch', '2021-11-08T15:01:50.859Z'::timestamp)::int8
group by "Date"
How can I have "Date" as month/year instead of month? so for example 10/20 instead of 10.
Use to_char() for format the timestamp value:
to_char(to_timestamp(e.startts), 'mm/yy') as "Date"
Related
SELECT (outbound.data_bas_year||outbound.data_bas_month) as year,
EXTRACT(QUARTER from to_date(outbound.data_bas_year||outbound.data_bas_month, 'YYYYMM')) AS quarter,
count(outbound.call_time) as col_1_0_
FROM cfk_dashboard.if_outbnd_call_dtl outbound
WHERE outbound.data_bas_year||outbound.data_bas_month between '20210101' and '20211231'
AND outbound.conn_call_number = 1
GROUP BY year,quarter
I wrote a query to look up January through December quarterly, but no data for January is aggregated.
In other words, only February and March are counted except for January in the first quarter.
However, if I change the condition start date from 20210101 to 20201231, I get the result I want.
Why?
Function to_date generates date for you, if you have not day value then default value of day will be -01.
But in your query after command WHERE you are used outbound.data_bas_year||outbound.data_bas_month, this is gets only 202101 and you compare this with 20211231 or with 20210101, this will not work correctly. For example, you can use outbound.data_bas_year||outbound.data_bas_month||'01' or to_date(outbound.data_bas_year||outbound.data_bas_month, 'YYYYMM') between '20210101'::date and '20211231'::date
I am using this query for fetching the data day wise
SELECT
(count( server_time::timestamp::date)) ,
server_time::timestamp::date
FROM
complaint_details_v2
WHERE
server_time between '2018/10/03' and '2018/10/11'
GROUP BY
server_time::timestamp::date
ORDER BY
server_time ASC
but I want to alter the above query week wise instead of day wise between two dates.
you can group the dates into sevens by doing date arithmetic.
SELECT
(count( server_time::timestamp::date)) ,
min(server_time::timestamp::date) as "week starting"
FROM
complaint_details_v2
WHERE
server_time between '2018/10/03' and '2018/10/11'
GROUP BY
floor((server_time::timestamp::date - '2018/10/03'::date)/7)
ORDER BY
2 ASC
another alternative is grouping expression date_trunc(week,server_time) but that binds you to ISO weeks
I have a date column 'visit_date' of the format '2018-10-04'.
I would need to derive "Week-Start to Week-End" column of format
"Oct 1 to Oct 7" or "1st Oct to 7th Oct", which means
I need to concatenate 2 dates - Week Begin and Week End post which I need to exclude the "Year" part of the dates.
Saw an answer here. It is partially suiting my requirement, where the answer would be "2018-09-03 to 2018-09-09"
SELECT date_trunc('week', visit_date)::date || ' -> '|| (date_trunc('week', visit_date)+ '6 days'::interval)::date as WeekPeriod
If you just need to format the date, to_char is what you need (https://www.postgresql.org/docs/current/static/functions-formatting.html):
SELECT to_char(your_date, 'Mon FMDD') --> Oct 1
SELECT to_char(your_date, 'FMDDth Mon') --> 1st Oct
demo: db<>fiddle
Mon gives the three lettered month
DD gives the day
FM prefix removes leading zeros
th suppif adds the "st", "nd", "rd", "th" ending
select to_char(date_trunc('week', visit_date),'YYYY-MM-DD')||'->'||
to_char((date_trunc('week', visit_date)+ '6 days'::interval),'YYYY-MM-DD') weekPeriod
The key here is the to_char, which allows custom date formats.
There is generally no need to store this type of data as it can be queried in the future the same way it was inserted above, and if you DO need to do queries on this information, it'd be better to store them as dates and not strings, however, the query above will fill the need you described.
I want to use Redshift to count the number of Mondays in a given time range. I've tried using date_part, which returns the day of the week. I can't use a simple count as there are multiple instances on the same day.
if you have dates table reference you can use the following code
select count(distinct my_table.date)
from my_table
where
date_part(dow,my_table.date)=1
and my_table.date between '2015-01-01' and '2016-01-01'
in this case the query will count all Mondays during 2015,
you can change the dates range the the day week .
date_part(dow,my_table.date)=1 -- Monday
date_part(dow,my_table.date)=2 -- Tuesday
and so on
if you don't have dates table , you should create Cartesian product
I have a date field in a postgresql database (field name is input) how can I extract the month only from the date field? I used the syntax below, but I want it to show the actual month name, not a numeric value for the month
EXTRACT(MONTH FROM input) AS "Month"
So if the date in the field input was 04/26/2016 this syntax returns 4, how could I alter this to return April
A simpler version.
to_char(input, 'Month') AS Month
Source : Data Type Formatting Functions
You may find this useful
SELECT to_char(to_timestamp (date_part('month', timestamp '2016-04-26 20:38:40')::text, 'MM'), 'Month')
All the best
Ref Postgresql (Current) Section 9.9. Date/Time Functions and Operators