How to get the values from dummy table to variables? - postgresql

(SELECT SUBSTRING(TO_CHAR(current_timestamp, 'yyyy-mm-dd hh12:mi:ss AM'),21,2)),
current_date ,
current_date - INTERVAL '1' DAYS,
to_char(current_date,'Day') into
vs_current_ampm,
vd_current_dt,
vd_prev_dt,
vl_day;

The forementioned query has a minor syntax error, i.e., an extra ) after 21,2.
I have changed that and the below query works fine.
SELECT
SUBSTRING(TO_CHAR(current_timestamp, 'yyyy-mm-dd hh12:mi:ss AM'),21,2),
current_date , current_date - INTERVAL '1' DAYS,
to_char(current_date,'Day');
If you would like to use the results in another place, you might need to use a cursor as I mentioned in the comments.
Check the documentation here.

Related

How to subtract a number of days in one column from CURRENT_DATE in PostgreSQL

I have a column called number_of_days_since_event and I want to change it to data_of_last_event, the way I can do that is subtracting today's date from the number of days in the number_of_days_since_event column. But I do not know how to subtract days in a column.
This question answers the problem when you know the number of days in advance, i.e., if I would like to subtract 10 days from today it would be:
SELECT CURRENT_DATE - INTERVAL '10 days';
However, I would like to do something like:
SELECT CURRENT_DATE - INTERVAL "myTable.number_of_days_since_event" 'days'
FROM myTable;
But this does not work leading to the error message: syntax error at or near "'day'"
The following using concat solves my problem:
SELECT CURRENT_DATE - concat(myTable.number_of_days_since_event::text,' day')::interval
FROM myTable;
If you are happy with a date result, you could use
SELECT current_date - number_of_days_since_event::integer
FROM mytable;

PostgreSQL: Syntax error at or near "14" when trying to run a delete query

First question here!
So I have a table with a row like this:
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
I would like to run a query that delete all the data in my table older than 14 days.
This is my query:
DELETE FROM customers WHERE timestamp < NOW() - INTERVAL 14 DAY;
This is the error: syntax error at or near "14"
Anyone knows why this is not working and/or how could I achieve my goal??
Many thanks!!
The interval value must be quoted:
DELETE FROM customers WHERE created_at < NOW() - INTERVAL '14 DAYS';
See the doc
DELETE FROM customers WHERE created_at < NOW() - INTERVAL '14 DAY';
Another variation on the existing answers (possibly easier to remember):
DELETE FROM customers WHERE timestamp < NOW() - '14 days'::interval;

How to group by month off a timestamp field from Redshift in Superset

I am trying to show some trend over month in Superset from a table which has a timestamp field called created_at but have no idea how to get it right.
The SQL query generated from this is the followings:
SELECT
DATE_TRUNC('month', created_at) AT TIME ZONE 'UTC' AS __timestamp,
SUM(cost) AS "SUM(cost)"
FROM xxxx_from_redshift
WHERE created_at >= '2017-01-01 00:00:00'
AND created_at <= '2018-07-25 20:42:13'
GROUP BY DATE_TRUNC('month', created_at) AT TIME ZONE 'UTC'
ORDER BY "SUM(cost)" DESC
LIMIT 50000;
Like I mentioned above, I don't know how to make this work and 2nd question is why ORDER BY is using SUM(cost)? If this is a time-series, shouldn't it use ORDER BY 1 instead? I tried to change Sort By but to no avail.
It is quite silly but I found that SUM(cost) doesn't work while sum(cost) works. It is a bug in Superset and will be addressed in https://github.com/apache/incubator-superset/pull/5487

Issue with generate_series in case sentence

Recently my team have updated the version of our postgres server from 9.3 to 10.1
One of our procedures has a piece of code that right now is giving us some problems. This is just an example base on the original code:
SELECT
CASE
WHEN current_date = '2017-12-14' THEN generate_series(current_date , '2017-12-31'::DATE , '1 day')
WHEN current_date = '2017-12-15' THEN generate_series(current_date , '2017-12-31'::DATE , '1 day')
ELSE generate_series('2017-12-01'::DATE, '2017-12-31'::DATE, '1 day')
END AS workday
If I run this code in our previous server I've got the sequence. However in postgres 10.1 I'am getting this error message:
"set-returning functions are not allowed in CASE. "
Is there any problem with this version of postgres is this a bug, or is there another way to get the expected result
You need to move the set-returning functions out of the CASE statement so that it only returns a single row at a time. I would simplify your statement by making the CASE a subquery that you use the resulting column from:
SELECT generate_series(series_start_date::DATE, '2017-12-31'::DATE, '1 day') FROM
(
SELECT
CASE
WHEN current_date='2017-12-14' THEN current_date
WHEN current_date='2017-12-15' THEN current_date
ELSE '2017-12-01'
END AS series_start_date
) as temp_alias;
That query should give you what you want.
Putting 'temp_alias' in there is required, but isn't used.
Disclaimer: I tested this on Postgres 9.6, which is all I have available to test with at the moment. It should work with Postgres 10.1.
The use of set-returning functions in scalar contexts was a hack. They were completely rewritten by Andres Freund to a much better implementation.
Release note: Change the implementation of set-returning functions appearing in a query's SELECT list (Andres Freund).
As an altenative you can use LATERAL JOIN to join the logic of determining reference date to generate_series:
SELECT workday_series.*
FROM
(SELECT CASE CURRENT_DATE
WHEN '2017-12-14' THEN CURRENT_DATE
WHEN '2017-12-15' THEN CURRENT_DATE
ELSE '2017-12-01'
END) AS REF (date_reference)
JOIN LATERAL generate_series(ref.date_reference, '2017-12-31'::DATE, '1 day') AS workday_series(workday) ON TRUE;
SQL fiddle here.

records from yesterday in postrgesql

I have an application in which I've used MySQL. I had a report that stretched records of the last 24 hours. I used the query:
WHERE (DATE_SUB(CURDATE(), INTERVAL 1 DAY) <= FROM_UNIXTIME(`workorder`.`CREATEDTIME` / 1000))
Now I have to use PostgreSQL and do not know how to make a report of the last 24 hours.
Can any of you help?
WHERE workorder.createdtime > current_date - 1 -- Yesterday and today
WHERE workorder.createdtime > current_timestamp - interval '1 day' -- last 24hr
> TIMESTAMP 'yesterday'
For convenience, Postgres includes a few hard-coded values as special Date/Time inputs. They include:
yesterday
today
tomorrow
now
Try SELECT TIMESTAMP 'now'.
For example, here is a query.
SELECT when_row_created_
FROM customer_
WHERE when_row_created_ > TIMESTAMP 'yesterday'
ORDER BY when_row_created_ DESC
;
These commands may not be appropriate to production code, but they certainly are handy in development. Read the docs and do some practice to be sure you understand the behavior of these commands, how the session’s time zone affects them and so on.
Downsides include (a) implicitly ignoring the crucial issue of time zone, and (b) not standard SQL.
where workorder.createdtime >= now() - interval '24 hour'
WHERE workorder.createdtime::date = current_date - 1; --from yesterday