How to set condition for specific part in postgresql? - sql

Here is my postgresql statement.
select sum("Color") as color,round(avg("Color")) as avg_color
from "color_table"
How can I set the condition for avg part?
I done some study and i found interval seems very useful. But I dont know where to set the condition.
I just want to get the average for Color which trandate is in past 90 days from now.

Here is one approach:
SELECT
SUM(Color) AS color,
ROUND(AVG(CASE WHEN trandate < NOW() - INTERVAL '90 DAYS'
THEN Color END)) AS avg_color
FROM "color_table";
This interprets your requirement as wanting the average of records where the trandate is older than 90 days from now. If instead you want within the last 90 days, then reverse the inequality.
If you are using Postgres 9.4 or later, you may also use the FILTER clause:
SELECT
SUM(Color) AS color,
ROUND(AVG(Color) FILTER (WHERE trandate < NOW() - INTERVAL '90 DAYS')) AS avg_color
FROM "color_table";

you can try like below
with cte as
( select SUM(Color) AS color,0 as avg_color
FROM "color_table"
union all
select 0, avg(Color ) FROM "color_table"
where trandate < NOW() - INTERVAL '90 DAYS'
) select max(color) as color ,max(avg_color) as avg_color from cte

Related

Can I search between two dates in SQL using DATE_TRUNC and INTERVAL?

Here is what I have working:
select date_trunc('hour', 123.created_at) AS trunc_created_at
FROM 123abc 123
WHERE 123.expires_at > date_trunc('day', GETDATE()) + INTERVAL '2 days'
Is it possible, from here, to then do an 'and' or 'between' to do something like
and 123.expires_at < date_trunc('day', GETDATE()) + INTERVAL '28 days'
Obviously, that doesn't work, but is there a way to do this using number of days instead of a specific date? I'm pretty new to SQL and have been playing with this for 30 minutes, including a dozen google queries, and can't seem to find a way to make something like this work. Only using between and a specific date range.
If you are just trying to look back 28 days you can just subtract that from GETDATE
SELECT CAST(GETDATE() - 28 AS DATE)
So if you wanted to look back 28 days it would be something like this
SELECT
*
FROM TABLE
WHERE 1=1
AND DATE BETWEEN GETDATE() AND GETDATE() - 28
I figured out how to do what I wanted
WHERE 123.expires_at > (date_trunc('day', GETDATE()) + INTERVAL '2 days') AND 123.expires_at < (date_trunc('day', GETDATE()) + INTERVAL '21 days')

Netezza SQL show only last year's data

it sounds simple and it should be simple but for some reason I can't seem to make it happen in Netezza... So far I tried:
select *
from table
where placed_dt >= DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()) - 1, 0);
and it looked like dateadd function doesn't work on Netezza. So I tried:
select *
from table
where placed_dt between (current_date - interval 1 year) and current_date
but still had no luck. Any help would be appreciated!
If you want the last year from the current date:
where placed_dt >= current_date - interval '1 year'
Note that the single quotes are needed.
and you can include the <= current_date if that is also needed.
If you want the last calendar year, there are various methods, but one is:
where date_trunc('year', placed_dt) = date_trunc('year', current_date) - interval '1 year'
You may try:
SELECT *
FROM yourTable
WHERE
placed_dt >= ADD_MONTHS(DATE_TRUNC('year', current_timestamp), -12) AND
placed_at < DATE_TRUNC('year', current_timestamp);
In the above inquality in the WHERE clause, for a current year of 2020, the lower bound represents 2019-01-01 and the upper bound represents 2020-01-01.

SQL Aggregation Join and Subquery Optimisation

I am trying to get aggregate values by time periods of two relations (buys and uses) and join them so that I can get the results in one report and also draw a ratio on them. I am using PostgreSQL. The end report required is: dateTime, u.sum, b.sum, b.sum/u.sum
The following query works but scales very poorly with larger table sizes.
SELECT b2.datetime AS dateTime, b2.sum AS BUY_VOLUME, u1.sum AS USE_VOLUME,
CASE u1.sum
WHEN 0 THEN 0
ELSE (b2.sum / u1.sum)
END AS buyToUseRatio
FROM(
SELECT SUM(b.total / 100.0) AS sum, date_trunc('week', (b.datetime + INTERVAL '1 day')) - INTERVAL '1 day' as datetime
FROM buys AS b
WHERE
datetime > date_trunc('month', CURRENT_DATE) - INTERVAL '1 year'
GROUP BY datetime) AS b2
INNER JOIN (SELECT SUM(u.amount) / 100.00 AS sum, date_trunc('week', (u.datetime + INTERVAL '1 day')) - INTERVAL '1 day' AS datetime
FROM uses AS u
WHERE
datetime > date_trunc('month', CURRENT_DATE) - INTERVAL '1 year'
GROUP BY datetime) AS u1 ON b2.datetime = u1.datetime
ORDER BY b2.datetime ASC;
I was wondering if anyone could help me by providing an alternative query that would get the end result required and is faster to execute.
I appreciate any help on this :-) My junior level SQL is a little rusty and I can't think of another way of doing this without creating indexes. Thanks in advance.
At least, these indexes can help your query:
create index idx_buys_datetime on buys(datetime);
create index idx_uses_datetime on uses(datetime);
Your query seems fine. However, you could use full join (instead of inner) to have all rows, where at least one of your tables have data. You could even use generate_series() to always have 1 year of results, even when there is no data in either of your tables, but I'm not sure if that's what you need. Also, some other things can be written more easily; your query could look like this:
select dt, buy_volume, use_volume, buy_volume / nullif(use_volume, 0.0) buy_to_use_ratio
from (select sum(total / 100.0) buy_volume, date_trunc('week', (datetime + interval '1 day')) - interval '1 day' dt
from buys
where datetime > date_trunc('month', current_timestamp - interval '1 year')
group by 2) b
full join (select sum(amount) / 100.0 use_volume, date_trunc('week', (datetime + interval '1 day')) - interval '1 day' dt
from uses
where datetime > date_trunc('month', current_timestamp - interval '1 year')
group by 2) u using (dt)
order by 1
http://rextester.com/YVASV92568
So the answer depends on how large your tables are, but if it was me, I would create one or two new "summary" tables based on your query and make sure to keep them updated (run a batch job once a day to update them or once an hour with all the data that has changed recently).
Then, I would be able to query those tables and do so, much faster.
If however, your tables are very small, then just keep going the way you are and play around with indexes till you get some timing which is acceptable.

Difference of datetime column in SQL

I have a table of 20000 records. each Record has a datetime field. I want to select all records where gap between one record and subsequent record is more than one hour [condition to be applied on datetime field].
can any one give me the SQL command code for this purpose.
regards
KAM
ANSI SQL supports the lead() function. However, date/time functions vary by database. The following is the logic you want, although the exact syntax varies, depending on the database:
select t.*
from (select t.*,
lead(datetimefield) over (order by datetimefield) as next_datetimefield
from t
) t
where datetimefield + interval '1 hour' < next_datetimefield;
Note: In Teradata, the where would be:
where datetimefield + interval '1' hour < next_datetimefield;
This can also be done with a sub query, which should work on all DBMS. As gordon said, date/time functions are different in every one.
SELECT t.* FROM YourTable t
WHERE t.DateCol + interval '1 hour' < (SELECT min(s.DateCol) FROM YourTable s
WHERE t.ID = s.ID AND s.DateCol > t.DateCol)
You can replace this:
t.DateCol + interval '1 hour'
With one of this so it will work on almost every DBMS:
DATE_ADD( t.DateCol, INTERVAL 1 hour)
DATEADD(hour,1,t.DateCol)
Although Teradata doesn't support Standard SQL's LEAD it's easy to rewrite:
select tab.*,
min(ts) over (order by ts rows between 1 following and 1 following) as next_ts
from tab
qualify
ts < next_ts - interval '1' hour
If you don't need to show the next timestamp:
select *
from tab
qualify
ts < min(ts) over (order by ts rows between 1 following and 1 following) - interval '1' hour
QUALIFY is a Teradata extension, but really nice to have, similar to HAVING after GROUP BY

SQL date check within set number of days

Im trying to find out how many clients viewed a property within 14 days of May 20, 2004, either before or after. Not really sure at all how to go about this.
Im assuming i need to group it and use a having?
EDIT: I am using oracle now
select count(*)
from VIEWING
WHERE CLAUSE?
For a one time query with that specific date,
select count(*) clients
from yourtable
where yourdatefield >= {d'2004-05-06'}
and yourdatefield < {d'2004-06-08'}
You might want to consult a calendar to see if those dates are correct.
Edit #1, since you are using Oracle, you can use:
select count(*) TotalClients
from yourtable
where dt >= (to_date('2004-05-20', 'yyyy-mm-dd') - INTERVAL '14' DAY)
and dt <= (to_date('2004-05-20', 'yyyy-mm-dd') + INTERVAL '14' DAY)
See SQL Fiddle with Demo
Based on some of your previous questions you were using MySQL.
If you are using MySQL then you can use the DATE_ADD() function to get the date range and then use count(*) to return all records from those dates:
select count(*) TotalClients
from yourtable
where dt >= date_add(str_to_date('2004-05-20', '%Y-%m-%d'), INTERVAL -14 day)
and dt <= date_add(str_to_date('2004-05-20', '%Y-%m-%d'), INTERVAL 14 day)