Postgresql - Partial sum per day and overall in one query - postgresql

I've got a table of different transactions with the according timestamps:
Table: Transactions
Recipient Amount Date
--------------------------------------------------
Bob 52 2019-04-21 11:06:32
Jack 12 2019-06-26 12:08:11
Jill 50 2019-04-19 24:50:26
Bob 90 2019-03-20 16:34:35
Jack 81 2019-03-25 12:26:54
Jenny 53 2019-04-20 09:07:02
Jenny 5 2019-03-29 06:15:35
Now I want to get all of Jack's transactions for today and overall:
Result
Person Amount_Today Amount_Overall
-----------------------------------------------
Jack 12 93
What's the most performant way to archieve this in postgresql? At the moment I run two queries - this one is for Amount_Today:
select Recipient, sum(Amount)
from Transactions
where Recipient = 'Jack'
and created_at > NOW() - INTERVAL '1 day'
But that doesn't seem like the right way.

You can use the filter clause:
select Recipient,
sum(Amount) as Amount_Overall,
sum(Amount) FILTER (WHERE created_at > NOW() - INTERVAL '1 day') as Amount_Today
from Transactions
where Recipient = 'Jack'
GROUP BY recipient;
You have probably realized this, but now() - interval '1 day' is not really today, it is the last 24 hours. You could use date_trunc if you want just today.

Related

How can I get weekly sales for every salesman

I have a table like below (tablename: sales)
sales_datetime
sales
salesman
2022-08-01 09:00:00
100
John
2022-08-01 11:00:00
200
John
2022-08-02 10:00:00
100
Peter
2022-08-02 13:00:00
300
John
2022-08-04 14:00:00
300
Peter
2022-08-05 12:00:00
100
John
2022-08-05 16:00:00
200
John
From that table I want to make a summary sales for 5 days period for each salesman. So the summary table that I want is look like this
periode
total_sales
salesman
2022-08-01
300
John
2022-08-01
0
Peter
2022-08-02
300
John
2022-08-02
100
Peter
2022-08-03
0
John
2022-08-03
0
Peter
2022-08-04
0
John
2022-08-04
300
Peter
2022-08-05
300
John
2022-08-05
0
Peter
I have created following query (PSQL) but the results were not same as I want. Assume today is 2022-08-05
with dateseries as
(select generate_series(current_date-'4 days'::interval,
current_date::date,
'1 day'::interval)::date as periode)
select d.periode,coalesce(sum(s.sales),0) as total_sales,s.salesman from dateseries d
left outer join sales s
on d.periode=s.sales_datetime::date
group by d.periode, s.salesman order by d.periode
results:
periode
total_sales
salesman
2022-08-01
300
John
2022-08-02
300
John
2022-08-02
100
Peter
2022-08-03
0
(NULL)
2022-08-04
300
Peter
2022-08-05
300
John
Any advices would be so great. Thank you
Step by step first aggregate the daily sales per salesperson (aggregated_sales CTE), create a list of days to report (days CTE), create a list of salesmen (salesmen CTE) and then query the sales for each day/salesman pair.
with aggregated_sales as
(
select sales_datetime::date sales_date, sum(sales) sales, salesman
from sales group by sales_datetime::date, salesman
),
days(sales_date) as
(
select d::date
from generate_series('2022-08-01', '2022-08-08', interval '1 day') d
),
salesmen (salesman) as
(
select distinct salesman from sales
)
select sales_date, coalesce(sales, 0) sales, salesman
from (select * from days cross join salesmen) fl
left outer join aggregated_sales ags using (sales_date, salesman);
The query may be shorter if CTEs are inlined yet I think that clarity and readability are more important than mere size.
In order to "make a summary sales for 5 days period for each salesman" replace generate_series('2022-08-01', '2022-08-08', interval '1 day') with generate_series(current_date - 4, current_date, interval '1 day').
the results were not same as I want. Assume today is 2022-08-05
Please note that '2022-08-05'::date - '5 days'::interval will give you 2022-07-31, and not 2022-08-01 as you assume. Because of that, I think you meant it to be current_date - '4 days'::interval.
With that out of the way, here is one possible query:
with sales_by_date as (
select
salesman,
sales_datetime::date,
sum(sales) total_sales
from sales
where
-- assuming you need to have totals for salesmen that had sales in specified period only
sales_datetime::date between current_date-'4 days'::interval and current_date
group by
salesman,
sales_datetime::date),
dateseries as (
select
distinct salesman,
generate_series(current_date-'4 days'::interval, current_date, '1 day'::interval)::date as periode
from sales_by_date)
select
d.periode,
coalesce(s.total_sales, 0) total_sales,
d.salesman
from dateseries d
left join sales_by_date s
on d.periode = s.sales_datetime
and d.salesman = s.salesman
order by d.periode, d.salesman;
But you still have to figure out some requirements for this problem. E.g. what if for the specified period there are no sales at all in the sales table?

Count distinct dates between two timestamps

I want to count %days when a user was active. A query like this
select
a.id,
a.created_at,
CURRENT_DATE - a.created_at::date as days_since_registration,
NOW() as current_d
from public.accounts a where a.id = 3257
returns
id created_at days_since_registration current_d tot_active
3257 2022-04-01 22:59:00.000 1 2022-04-02 12:00:0.000 +0400 2
The person registered less than 24 hours ago (less than a day ago), but there are two distinct dates between the registration and now. Hence, if a user was active one hour before midnight and one hour after midnight, he is two days active in less than a day (active 200% of days)
What is the right way to count distinct dates and get 2 for a user, who registered at 23:00:00 two hours ago?
WITH cte as (
SELECT 42 as userID,'2022-04-01 23:00:00' as d
union
SELECT 42,'2022-04-02 01:00:00' as d
)
SELECT
userID,
count(d),
max(d)::date-min(d)::date+1 as NrOfDays,
count(d)/(max(d)::date-min(d)::date+1) *100 as PercentageOnline
FROM cte
GROUP BY userID;
output:
userid
count
nrofdays
percentageonline
42
2
2
100

Postgresql - query to get difference in data count

I have two tables, today's_table and yeterday's_table.
I need to compare the data for an interval of 15 mins at exact same times for today and yesterday.
For example, for below data let's I need to check from 00:00:00 and 00:15:00 on 20201202 and 20201202. So difference should come out as '3' since the yesterday's_table has 8 records and today's_table has 5 records.
today's_table:
Yesterday's table:
I tried something like; (consider now() is 00:15:00)
select count(*) from yeterday's_table where time between now() - interval "24 hours" and now() - interval "23 hours 45 mins"
minus
select count(*) from today's_table where time = now() - interval "15 minutes";
is there any other way to do this?
You can easily do this with subqueries:
SELECT b.c - a.c
FROM (select count(*) as c from yeterdays_table where time between now() - interval '24 hours' and now() - interval '23 hours 45 mins') a,
(select count(*) as c from todays_table where time = now() - interval '15 minutes') b;
Bear in mind you need to single-quote your intervals, and your table names cannot have quotes in them.

PostgreSQL: Get rows which date is 5 days old than payment_date

Currently I need to send an Email to all users that have 5 days with their payment due_date expired and are status=1 (pending to pay) for the current month and year because they might have future dates or past dates. example
due_date= 27/06/2018 send email after 5 days 1/05/2018
my Query to grab all users with a interval within 5 days is the following:
SELECT payments_payment.id, payments_payment.due_date
FROM payments_payment
WHERE payments_payment.due_date < NOW() - '5 day'::interval
AND payments_payment.status = 1
AND EXTRACT(year FROM payments_payment.due_date) = EXTRACT(year FROM NOW())
AND EXTRACT(month FROM payments_payment.due_date) = EXTRACT(month FROM NOW())
ORDER BY payments_payment.due_date ASC;
Need to make a different approach since the question is inverse for that reason I need to get the difference between 2 dates and see if it matches my day limit here is the Query.
PostgreSQL Query:
SELECT due_date
FROM payments_payment
WHERE payments_payment.due_date + interval '5 day' < current_date
AND payments_payment.status = 1
Explanation
Get all payment dates where status equals 1 and month equals current month and year where the due_date substracted by current date is equals to 5 days.

Group every N days

Ive been searching for something like that using PostgreSQL, but havent found yet.
Lets suppose i have the following table:
id order amount created_at
2 527837 10.0 2014-12-01T...
3 527838 50.0 2014-12-02T...
4 527839 30.0 2014-12-02T...
5 527840 40.0 2014-12-10T...
6 527841 80.0 2014-12-13T...
And i want to have a query that returns the sum of all amounts for each full week of 7 days (even if some day had no orders):
Example:
week total_amount
Dec/01 - Dec/07 90.0
Dec/08 - Dec/15 120.0
Dec/16 - Dec/23 0.0
//...and so on until current date
Also, lets suppose, that January has 30 days, and February has 28 days, i want the weeks to be grouped like that:
Jan/01-Jan/08
Jan/09-Jan/16
Jan/17-Jan/24
Jan/25-Feb/02 (theres no problem on crossing months)
Feb/03-Feb/10
What is the best way to do this?
EDIT 1:
I have found a way to build a query to generate a temporary table with the days i need for my grouping, i just having dificult grouping this and joining it with my original table...
(SELECT TO_CHAR(generate_series, 'YYYY-MM-DD') as "day" FROM generate_series('2013-06-01 00:00'::timestamp,
'2015-06-01 00:00'::timestamp, '1 Day'))
select date_trunc('week', created_At),date_trunc('week', created_At)+ INTERVAL '6' DAY,
SUM(amount)
from t
GROUP BY date_trunc('week', created_At)
ORDER BY MIN(created_At);
FIDDLE