Subtract dates within CASE expression - postgresql

I am looking to find out how many days are between 2 dates in a query. This is probably something simple to a seasoned Postgres person, but I cannot find an answer..
And help would be great.
CASE
WHEN DATEDIFF('day',a.date_approve,a.current_rec_date) = 1
THEN a.current_rec_date
ELSE a.date_approved
END AS date_approved,

There is no datediff() in Postgres.
If the columns are dates then simply subtracting them will give you the difference in days.
...
a.date_approve - a.current_rec_date = 1
...
If they're timestamps subtracting them will get you an interval so you'd need to compare it to an interval.
...
a.date_approve - a.current_rec_date = '1 day'::interval
...
You can get more information from the documentation.

Related

Subtract between timestamp in Redshift

I found a weird thing. If a timestamp value subtract another, then Redshift will return an strange prefix. For example,
select table1.c_timestamp - table1.c_timestamp from table_1
Expect result should be ZERO or similar something, because these two timestamp values are same.
However, what I received is "5012369 years 4 mons", which I have no idea how does Redshift calculate the result.
Is there anyone can show me some clues?
Thanks
Contrary to the other answer,
Datediff doesn't exactly subtract, but rather counts the number of times the datepart chosen starts between the two timestamps.
datediff(second, '2018-04-10 00:00:00.001','2018-04-10 00:00:00.999')
>> 0
select datediff(second, '2018-04-10 00:00:00.999','2018-04-10 00:00:01.001')
>> 1
See: Datediff documentation
Edit: this is the way I found of how to perform the OP's task
SELECT
round(((EXTRACT('epoch' FROM TIMESTAMP '2018-05-27 09:59:59.999') - EXTRACT('epoch' FROM TIMESTAMP '2018-05-27 09:59:59.001'))*1000 + EXTRACT(millisecond FROM TIMESTAMP '2018-05-27 09:59:59.999') - EXTRACT(millisecond FROM TIMESTAMP '2018-05-27 09:59:59.001'))::real/1000)
The right way to subtract between datetimes is:
select datediff(seconds, table1.c_timestamp, table1.c_timestamp) from table_1
Of course, it doesn't make much sense to subtract a timestamp from itself, because that obviously returns 0, but I assume you just run that as a test.

Q (KDB) selecting today's date within date range

I am trying to set up an dynamic threshold by different user, but only return result from today's date. I was able to return all the records from past 30 days, but I am having trouble only outputting today's date based on the calculation from past 30 days.. I am new to q language and really having a trouble with this simple statement :( (have tried and/or statement but not executing..) Thank you for all the help in advance!
select user, date, real*110 from table where date >= .z.D - 30, real> (3*(dev;real) fby user)+((avg;real) fby user)
Are you saying that you want to determine if any of todays "real" values are greater than 3 sigma based on the past 30 days? If so (without knowing much about your table structure) I'm guessing you could use something like this:
q)t:t,update user:`user2,real+(.0,39#10.0) from t:([] date:.z.D-til 40;user:`user1;real:20.1,10.0+39?.1 .0 -.1);
q)sigma:{avg[y]+x*dev y};
q)select from t where date>=.z.D-30, ({(.z.D=x`date)&x[`real]>sigma[3]exec real from x where date<>.z.D};([]date;real)) fby user
date user real
---------------------
2016.03.21 user1 20.1

Postgres - Convert Date Range to Individual Month

I have found similar help, but the issue was more complex, I am only good with the basics of SQL and am striking out here. I get a handful of columns a,b,c,startdate,enddate and i need to parse that data out into multiple rows depending on how many months are within the range.
Eg: a,b,c,1/1/2015, 3/15,2015 would become:
a,b,c,1/1/2015,value_here_doesnt_matter
a,b,c,2/1/2015,value_here_doesnt_matter
a,b,c,3/1/2015,value_here_doesnt_matter
Does not matter if the start date or end date is on a specific day, the only thing that matters is month and year. So if the range included any day in a given month, I'd want to output start days for each month in the range, with the 1st as a default day.
Could I have any advice on which direction to begin? I'm attempting generate_series, but am unsure if this is the right approach or how to make it work with keeping the data in the first few arbitrary columns consistent.
I think generate_series is the way to go. Without knowing what the rest of your data looks like, I would start with something like this:
select
a, b, c, generate_series(startdate, enddate, interval '1 month')::date
from
my_table

postgresql: generate interval count of hours between two timestamps

I am using postgres 8.3.12 and novice.
I have a column formatted as follows: '29/04/2013 at 09:27:51 PM (or AM)'
I am doing the following to set them to a timestamp:
case when <criteria> to_timestamp(time, 'MM/DD/YYYY at HH:MI:SS AM') as start
case when <criteria> to_timestamp(time, 'MM/DD/YYYY at HH:MI:SS AM') as end
My goal is to calculate the hours between two time stamps.
I looked at http://www.postgresql.org/docs/current/static/functions-datetime.html
After I set them to timestamps, is it simply
end - start as difference;
Assuming you have two columns named timea and timeb, the following will return you the number of hours between them in PostgreSQL:
SELECT EXTRACT(EPOCH FROM timea - timeb) / 3600 AS hours FROM ...;
It might also be useful to note that:
EXTRACT(EPOCH FROM timea - timeb)
Also, timea and timeb don't need to be columns - you can use whatever expressions you want here, as long as they are timestamps.
Will return to you the number of seconds between the two timestamps - this can be useful to compute the number of minutes, hours, days, etc. - whatever you like. Hours, in particular, contain 3600 seconds, so you simply divide by 3600 to get the number of hours.
The EXTRACT function can do all kinds of powerful things for you. I'd suggest looking at the documentation for it here.

how to obtain interval from date on postgres in days

I have two columns, say "2010-10-26" and "2010-08-23" and I would like to obtain the interval in days, what is the best way to do this on a select query taking into account that there are months with 30,31,29,28 days? Is there any function that already does this?
Thank you.
If they are already in the date format, it's very easy:
SELECT '10/26/2010'::date - '08/23/2010'::date;
OR:
SELECT date '2001-10-01' - date '2001-09-28'; // outputs integer 3