Postgres Select all Sundays in 2019 [closed] - postgresql

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.

Related

Converting SAS IFN(), HOUR() and MINUTE() to PostgreSQL [closed]

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

How to convert '02-3月 -21' to date 21-03-02 00:00:00 psql [closed]

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

In Snowflake, how to derive the last 365 days for all dates in Date Dimension table [closed]

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.

query to get count of records till previous day [closed]

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 6 years ago.
Improve this question
I have a table "empdata" it contains fields create date, last modified date, emp id. In order to check count am running query "select count(*) from empdata". Now I need query where I have to get results till yesterday's date.
for example:
select count(*) from empdata
output: 23
select count(*) from empdate ( I need condition here for yesterday's date)
output:20
Note: **I need to use this query in java program so I cannot give yesterday's date in query. Java program will be running daily so date gets* changed daily.*
Use this query to get the results, te condition you've asked should be
where create_date < current_date()
select count(*) from empdata where create_date<current_date()
You have mentioned that you want to run this program in java, Try jdbc
http://www.tutorialspoint.com/jdbc/
edit:
use CURRENT DATE instead of current_date() in the above query for db2

Syntax error while comparing timestamps in PostgreSQL [closed]

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