Running total in two tables - mysql

Consider two tables: invoices and payments. The invoices table contains records of invoices raised, and the payments table contains records of payments received.
invoices
id
date
cname
amount
1
2021-12-12
cname1
10000
2
2021-12-13
cname2
5000
3
2022-01-15
cname1
7000
4
2022-01-16
cname2
1000
payments
id
date
cname
amount
1
2022-01-05
cname1
5000
2
2022-01-07
cname2
5000
3
2022-02-05
cname1
10000
4
2022-02-06
cname2
1000
CALCULATE RUNNING BALANCE
Q) Extend the SQL query to do invoice / payment matching as follows (as of 28/2/2022)
matching
date
document_id
cname
amount
due
2021-12-12 00:00:00
1
cname1
10000
10000
2022-01-05 00:00:00
1
cname1
-5000
5000
2022-01-15 00:00:00
3
cname1
7000
12000
2022-02-05 00:00:00
3
cname1
-10000
2000
2021-12-13 00:00:00
2
cname2
5000
5000
2022-01-07 00:00:00
2
cname2
-5000
0
2022-01-16 00:00:00
4
cname2
1000
1000
2022-02-06 00:00:00
4
cname2
-1000
0

You can union both tables considering the second one with negative amount, and then a simple running total will produce the result you want. For example:
select
date,
id as document_id,
cname,
amount,
sum(amount) over(partition by id order by date) as due
from (
select * from invoices
union all select id, date cname, -amount from payments
) x
order by cname, date

SELECT `date`,
documentId,
cname,amount,
due FROM (SELECT `date`,
documentId,
cname,
amount,
(CASE WHEN #running_customer='' THEN #running_balance:=amount
WHEN #running_customer=cname THEN #running_balance:=#running_balance+amount ELSE #running_balance:=amount END) due,
#running_customer:=cname
FROM (SELECT `date`, id AS documentId,cname, amount FROM `invoices`i
UNION ALL
SELECT `date`, id AS documentId,cname, amount*-1 AS actionType FROM `payments` p) final
JOIN (SELECT #running_customer:='') rc
JOIN (SELECT #running_balance:=0) rb
ORDER BY cname, `date`) finalResult
You need to be using assignment operator for these kind of problems.

Related

Daily Sales from Total Sales

I have a database that looks like this:
ID
Sale_Date(YYYY-MM-DD)
Total_Volume
123
2022-01-01
0
123
2022-01-02
2
123
2022-01-03
5
456
2022-04-06
38
456
2022-04-07
40
456
2022-04-08
45
I want to get a daily sale column from Total Volume. which is just by subtracting the total volume on date x with total volume on date x-1 for each id.
ID
Sale_Date(YYYY-MM-DD)
Total_Volume
Daily_Sale
123
2022-01-01
0
0
123
2022-01-02
2
2
123
2022-01-03
5
3
456
2022-04-06
38
38
456
2022-04-07
40
2
456
2022-04-08
45
5
My initial attempt was using a rank function and self join but that didnt turn out correct.
with x as (
select
distinct t1.ID,
t1.Sale_Date,
t1.Total_volume,
rank() over (partition by ID order by Sale_Date) as ranker
from t t1 order by t1.Sale_Date)
select t2.ID, t2.ranker, t2.Sale_date, t1.Total_volume, t1.Total_volume - t2.Total_volume as Daily_sale
from x t1, x t2 where t1.ID = t2.ID and t2.ranker = t1.ranker-1 order by t1.ID;
You should use:
the LAG window function to retrieve last "Sale_Date" value
the COALESCE function to replace NULL with "Total Volume" for each first rows
Then subtract Total_Volume from the previous value of Total_Volume and coalesce if the value of the LAG is NULL.
SELECT *,
COALESCE(`Total_Volume`
-LAG(`Total_Volume`) OVER(PARTITION BY `ID`
ORDER BY `Sale_Date(YYYY-MM-DD)`), `Total_Volume`) AS `Daily_Sale`
FROM tab
Check the demo here.

MySQL: calculate percentiles among each group

I have a Transaction table with transactions data :
"created" - DateTime of transaction
"price" - transaction price
"id": product identifier
Sample data
id
created
price
5
2022-05-08 20:20:00
1
5
2022-05-08 19:00:00
2
5
2022-05-08 7:40:00
3
5
2022-05-05 8:20:00
4
2
2022-05-09 10:40:00
5
2
2022-05-09 10:40:00
6
2
2022-05-07 15:40:00
7
2
2022-05-03 16:30:00
8
Goal: to calculate the 25% percentile for prices and the number of transactions with prices lower than the 25 price percentile(25% percent of all) per id.
Expected result:
id
price 1st q
n_transactions
5
2
1
2
6
1
I have tried:
SELECT
id,
MAX(CASE WHEN Quartile = 1 THEN price END) 1Quartile,
FROM (
SELECT
id,
price,
NTILE(4) OVER (PARTITION BY id ORDER BY price) AS Quartile
FROM
Transactions) Vals
GROUP BY
id
ORDER BY
id
which should return the 1 quartile for the price but it returns only an execution error with the message "check SQL syntax".
MySQL version: 5.7.36

MySQL SUM two columns then another column with the net amount

Hi ive tried all the solution on stack over flow to no avail
i have 2 tables with a ID primary key, then date, and amount. there can be multiple dates of the same date in the table. the debits uses negative numbers in debits table
table "credits"
id | date | amount
1 2020-01-01 10.00
2 2020-01-02 20.00
3 2020-01-03 30.00
4 2020-01-01 10.00
5 2020-01-02 10.00
6 2020-01-03 10.00
table "debits"
id | date | amount
55 2020-01-01 -5.00
56 2020-01-02 -5.00
57 2020-01-03 -5.00
58 2020-01-01 -5.00
59 2020-01-02 -5.00
60 2020-01-03 -5.00
I want to return a 3 column result like so, grouped by DATE with 4 fields, date, amount credits (for teh day) amount debits (for the day) and the amont total (for the day)
date | amount_credits | amount_debits | amount_total
2020-01-01 20 10 10
2020-01-02 30 10 20
2020-01-03 40 10 30
I would do this using union all and aggrgtion:
select date, sum(credit) as credits, abs(sum(debits)) as debits),
sum(credits) + sum(debits) as net
from ((select c.date, c.amount as credit, 0 as debit
from credits c
) union all
(select c.date, 0, d.amount
from debits d
)
) cd
group by date;
I note that the sign of the debits amount changes, from the source table to the result set, which is why the outer query uses abs().
In particular, using union all and group by ensures that all dates in the original data are in the result set -- even if the date in in only one of the tables.
I'd group both tables on the date and then join the two:
SELECT c.date, amount_credits, amount_debits, amount_credits - amount_debits AS amount_total
FROM (SELECT date, SUM(amount) AS amount_credits
FROM credits
GROUP BY date) c
JOIN (SELECT date, -1 * SUM(amount) AS amount_debits
FROM debits
GROUP BY date) d ON c.date = d.date

Conditional Window Functions

I have a sales table that looks like this:
store_id cust_id txn_id txn_date amt industry
200 1 1 20180101 21.01 1000
200 2 2 20200102 20.01 1000
200 2 3 20200103 19 1000
200 3 4 20180103 19 1000
200 4 5 20200103 21.01 1000
300 2 6 20200104 1.39 2000
300 1 7 20200105 12.24 2000
300 1 8 20200105 25.02 2000
400 2 9 20180106 103.1 1000
400 2 10 20200107 21.3 1000
Here's the code to generate this sample table:
CREATE TABLE sales(
store_id INT,
cust_id INT,
txn_id INT,
txn_date bigint,
amt float,
industry INT);
INSERT INTO sales VALUES(200,1,1,20180101,21.01,1000);
INSERT INTO sales VALUES(200,2,2,20200102,20.01,1000);
INSERT INTO sales VALUES(200,2,3,20200103,19.00,1000);
INSERT INTO sales VALUES(200,3,4,20180103,19.00,1000);
INSERT INTO sales VALUES(200,4,5,20200103,21.01,1000);
INSERT INTO sales VALUES(300,2,6,20200104,1.39,2000);
INSERT INTO sales VALUES(300,1,7,20200105,12.24,2000);
INSERT INTO sales VALUES(300,1,8,20200105,25.02,2000);
INSERT INTO sales VALUES(400,2,9,20180106,103.1,1000);
INSERT INTO sales VALUES(400,2,10,20200107,21.3,1000);
What I would like to do is create a new table, results that answers the question: what percentage of my VIP customers have, since January 3rd 2020, shopped i) at my store only; ii) at my store and at other stores in the same industry; iii) at only other stores in the same industry? Define a VIP customer to be someone who has shopped at a given store at least once since 2019.
Here's the target output table:
store industry pct_my_store_only pct_both pct_other_stores_only
200 1000 0.5 0.5 0.0
300 2000 0.5 0.5 0.0
400 1000 0.0 1.0 0.0
I'm trying to use window functions to accomplish this. Here's what I have so far:
CREATE TABLE results as
SELECT s.store_id, s.industry,
COUNT(DISTINCT (CASE WHEN s.txn_date>=20200103 THEN s.cust_id END)) * 1.0 / sum(count(DISTINCT (CASE WHEN s.txn_date>=20200103 THEN s.cust_id END))) OVER (PARTITION BY s.industry) AS pct_my_store_only
...AS pct_both
...AS pct_other_stores_only
FROM sales s
WHERE sales.txn_date>=20190101
GROUP BY s.store_id, s.industry;
The above does not seem to be correct; how can I correct this?
Join the distinct store_ids and industries to the concatenated distinct store_ids and industries for each customer and then use window function avg() with the function find_in_set() to determine if a customer how many customer have shopped or not from each store:
with
stores as (
select distinct store_id, industry
from sales
where txn_date >= 20190103
),
customers as (
select cust_id,
group_concat(distinct store_id) stores,
group_concat(distinct industry) industries
from sales
where txn_date >= 20190103
group by cust_id
),
cte as (
select *,
avg(concat(s.store_id) = concat(c.stores)) over (partition by s.store_id, s.industry) pct_my_store_only,
avg(find_in_set(s.store_id, c.stores) = 0) over (partition by s.industry) pct_other_stores_only
from stores s inner join customers c
on find_in_set(s.industry, c.industries) and find_in_set(s.store_id, c.stores)
)
select distinct store_id, industry,
pct_my_store_only,
1 - pct_my_store_only - pct_other_stores_only pct_both,
pct_other_stores_only
from cte
order by store_id, industry
See the demo.
Results:
> store_id | industry | pct_my_store_only | pct_both | pct_other_stores_only
> -------: | -------: | ----------------: | -------: | --------------------:
> 200 | 1000 | 0.5000 | 0.5000 | 0.0000
> 300 | 2000 | 0.5000 | 0.5000 | 0.0000
> 400 | 1000 | 0.0000 | 1.0000 | 0.0000

SQL Query get total value based on different unit price,quantity at different time

I have a transaction table that like this: quantity is the total quantity in stock based on different unit price. let's call it T
id | transaction_time | item | unit_price | quantity | subtotal
1 2012-5-15 A 1.00 15 15.00
2 2012-5-15 A 3.00 15 45.00
3 2012-5-15 B 1.00 10 10.00
4 2012-6-10 A 2.00 15 30.00
5 2012-6-15 A 2.00 10 20.00
I need to get the total value of each item in stock over time...however, same items are based on different unit price. The result for A for example is:
transaction_time | item | quantity | subtotal
2012-5-15 A 30 60.00
2012-6-10 A 45 90.00
2012-6-15 A 40 80.00
2012-5-15, we have 15 item A with price 1.00, 15 item A with price 3.00, so the total quantity is 30, subtotal is 15*1+15*3=60.
2012-6-10 we have 15 more item A with price 2, so the total quantity become 30+15=45, subtotal become 60+15*2=90
2012-6-15 we have 10 item A with price 2, so item A with price 2 goes down from 15 to 10. the total quantity become 40, and the subtotal goes down -2*5, which become 80.
I tried
select transaction_time,sum(quantity),sum(subtotal)
where id in(select max(id) from T group by unit_price,item)
group by item
having item=A
This only gives me the last line
2012-6-15 A 40 80.00
You need first to identify all possible unit_price values for the specific item:
SELECT DISTINCT unit_price
FROM t
WHERE item = 'A'
Output:
unit_price
----------
1
3
2
You also need to identify all possible transaction_times:
SELECT DISTINCT transaction_time
FROM t
WHERE item = 'A';
Output:
transaction_time
----------------
2012-05-15
2012-06-10
2012-06-15
Now perform a CROSS JOIN between the above two sets
SELECT *
FROM (
SELECT DISTINCT transaction_time
FROM t
WHERE item = 'A') AS times
CROSS JOIN (
SELECT DISTINCT unit_price
FROM t
WHERE item = 'A') AS up
ORDER BY times.transaction_time
to get:
transaction_time unit_price
----------------------------
2012-05-15 3
2012-05-15 2
2012-05-15 1
2012-06-10 3
2012-06-10 2
2012-06-10 1
2012-06-15 1
2012-06-15 3
2012-06-15 2
Now use the above and perform a correlated subquery to get unit_price per transaction_time from item 'A':
SELECT transaction_time, unit_price,
(SELECT quantity
FROM t
WHERE t.item = 'A'
AND t.unit_price = up.unit_price
AND t.transaction_time <= times.transaction_time
ORDER BY transaction_time DESC LIMIT 1) AS quantity
FROM (
SELECT DISTINCT transaction_time
FROM t
WHERE item = 'A') AS times
CROSS JOIN (
SELECT DISTINCT unit_price
FROM t
WHERE item = 'A') AS up
ORDER BY times.transaction_time
Output:
transaction_time unit_price quantity
----------------------------------------
15.05.2012 00:00:00 1 15
15.05.2012 00:00:00 3 15
15.05.2012 00:00:00 2 NULL
10.06.2012 00:00:00 1 15
10.06.2012 00:00:00 3 15
10.06.2012 00:00:00 2 15
15.06.2012 00:00:00 1 15
15.06.2012 00:00:00 3 15
15.06.2012 00:00:00 2 10
The final result is simply a matter of performing a GROUP BY on the above:
SELECT transaction_time,
'A' AS item,
SUM(quantity) AS quantity,
SUM(quantity*unit_price) AS subtotal
FROM (
SELECT transaction_time, unit_price,
(SELECT quantity
FROM t
WHERE t.item = 'A'
AND t.unit_price = up.unit_price
AND t.transaction_time <= times.transaction_time
ORDER BY transaction_time DESC LIMIT 1) AS quantity
FROM (
SELECT DISTINCT transaction_time
FROM t
WHERE item = 'A') AS times
CROSS JOIN (
SELECT DISTINCT unit_price
FROM t
WHERE item = 'A') AS up) AS x
GROUP BY transaction_time
Output:
transaction_time item quantity subtotal
----------------------------------------------
15.05.2012 A 30 60
10.06.2012 A 45 90
15.06.2012 A 40 80
Demo here
Following query(kind of complex, maybe slow, needs optimization) works, check DEMO
SELECT tr_sub.cur_tt, tr_sub.item, sum(tr.quantity), sum(tr.quantity*tr.unit_price)
FROM
(SELECT tr1.transaction_time as cur_tt, max(tr2.transaction_time) as prev_tt, tr1.item as item,
IF (tr1.unit_price=tr2.unit_price, tr1.unit_price, tr2.unit_price) as t_p
FROM transactions tr1 LEFT JOIN transactions tr2 ON
tr1.transaction_time>=tr2.transaction_time AND tr1.item=tr2.item
GROUP BY tr1.item, tr1.transaction_time, t_p
) as tr_sub INNER JOIN transactions tr ON
tr_sub.prev_tt=tr.transaction_time
AND tr_sub.item=tr.item
AND tr_sub.t_p=tr.unit_price
GROUP BY tr_sub.item, tr_sub.cur_tt
ORDER BY tr_sub.cur_tt, tr_sub.item