Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 months ago.
This post was edited and submitted for review 3 months ago and failed to reopen the post:
Needs details or clarity Add details and clarify the problem by editing this post.
Improve this question
(hour(date_column ) * 4)
+ ifn(minute(date_column ) = 0, 0
, ifn(minute(date_column ) <= 15, 1
, ifn(minute(date_column ) <= 30, 2
, ifn(minute(date_column ) <= 45, 3, 4))))
as date_no
What are the equivalents of these functions in PostgreSQL?
I am trying to extract value based on 15 mins interval in an hour.
It would help if you explained your goal and current logic in a bit more detail, but for now it looks like this would suffice:
select extract('hours' from date_column) * 4
+ ceil(extract('minutes' from date_column) / 15) as date_no
from your_table;
extract() is used in PostgreSQL for the same thing hour() and minute() are in SAS.
You can use CASE the exact same way you would in SAS but there's no IFN() counterpart.
CEIL() works the same in both, so you could also shorten your SAS version replacing all the IFN's.
You're effectively binning date/times, so you could look into date_bin(). Similar effect can probably be achieved in SAS through the use of INTNX() or plain ROUND() (1, 2).
Demo
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
How to convert '02-3月 -21' to date 21-03-02 00:00:00? in postgresql or kettle
I think a simple replace can do the trick here. Just replace the Chinese character 月 with nothing and format it.
SELECT TO_TIMESTAMP(REPLACE ('02-3月-21', '月', ''),'DD-MM-YY');
// output: 2021-03-02 00:00:00+00
SELECT TO_CHAR(TO_TIMESTAMP(REPLACE ('02-3月-21', '月', ''),'DD-MM-YY'), 'YY-MM-DD HH24:MI:SS')
// output: 21-03-02 00:00:00
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
how would I go about selecting data from only Sundays in 2019 in Postgres?
I think Sundays are a 0 but I am unsure as to how to query it like that.
I am selecting from a preexisting table that contains timestamp strings like 2020-09-03 02:11:60, so basically I want to select all the timestamps that occurred in 2019 in my table usermetrics in the column timestamp.
You have two requests.
The first to find timestamps in 2019:
SELECT
*
FROM
usermetrics
WHERE
timestamp BETWEEN '01/01/2019'::date AND '01/01/2020'::date
The second to find those that occurred on Sundays:
SELECT
*
FROM
usermetrics
WHERE
timestamp BETWEEN '01/01/2019'::date AND '01/01/2020'::date
AND
extract(DOW FROM timestamp) = 0;
01/01/2020::date is used as 12/31/2019::date would miss timestamps that occurred after midnight.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
How to convert time format from 06/23/2020 07:57:45 to 06-23-2020 in hive
Using split and concatenation:
with your_data as (
select '06/23/2020 07:57:45' as str
)
select concat_ws('-',splitted[0],splitted[1],splitted[2]) as Result
from
(
select split(str,'[/ ]') as splitted
from your_data
)s
Result:
06-23-2020
Using from_unixtime(unix_timestamp(string, format_from), format_to):
select from_unixtime(unix_timestamp(str,'MM/dd/yyyy HH:mm:ss'),'MM-dd-yyyy') as Result
from your_data
Result:
06-23-2020
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
My date dimension table has a column "date_id" which has dates from 2016-01-01 till 2025-01-01. I wanted to create a view on the top of this date table and introduce an extra column say "date_id_365". The new column should have ALL the last 365 dates for each date_id.
Like for example If I pick a date_id to say 2020-01-15, that date_id should return dates from 2019-01-15 till 2020-01-15.
How to write the SQL in snowflake for this?
I think maybe you're looking for something like this...which is pretty standard SQL, not specifically for Snowflake. This assumes that date_id is a DATE field, which I'm not seeing in your comments, but you could convert it or use the date field.
SELECT a.date_id, b.date_id as date_id_365
FROM dim_date a
JOIN dim_date b
ON b.date_id BETWEEN DATEADD(year,-1,a.date_id) AND a.date_id
I haven't tested this myself, but pretty sure this will work for you. The only issue will be with your dates for the year 2016, since it won't have any dates in 2015 to join back to.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Error: "syntax error at or near interval"
Join in question:
... LEFT OUTER JOIN usersession e ON a.userID = e.userID AND e.lastAction >= now() interval '-4h' * 1"
lastAction was created as lastAction timestamp NULL DEFAULT NULL
The idea is, to return some nils if the session's timestamp is 4 hours older than the current time.
No idea what the deal is here, never had to compare timestamps before either.
If you want records for the last 4 hours you need:
now() - interval '4 hours'
instead of
now() interval '-4h' * 1