Max and Avg debt days over a period of time - sql

I have invoices pending payment, every invoice has two dates, first when the invoice is required to pay and the other when the invoice is paid. I want to know in a period of time the max debt and the avg debt
This is the table
Id Invoice Amount InvoiceDate InvoicePayment
----------- ------- ----------- ----------- -------------
1 Bill 1 314 2019-01-20 2019-03-01
2 Bill 2 205 2019-01-14 2019-02-18
3 Bill 3 90 2019-02-04 2019-02-06
4 Bill 4 456 2019-01-03 2019-04-27
I would like to know the max debt amount in february and the avg debt

You can unpivot with cross apply, and use a window sum to compute the "running" debt at each given point in time. The rest is just filtering and aggregation:
select avg(debt) avg_debt, max(debt) max_debt
from (
select x.dt, sum(x.amount) over(order by x.dt) debt
from mytable t
cross apply (values (invoicedate, amount), (invoicepayment, -amount)) as x(dt, amount)
) t
where dt >= '20200201' and dt < '20200301'

Related

How to calculate average monthly number of some action in some perdion in Teradata SQL?

I have table in Teradata SQL like below:
ID trans_date
------------------------
123 | 2021-01-01
887 | 2021-01-15
123 | 2021-02-10
45 | 2021-03-11
789 | 2021-10-01
45 | 2021-09-02
And I need to calculate average monthly number of transactions made by customers in a period between 2021-01-01 and 2021-09-01, so client with "ID" = 789 will not be calculated because he made transaction later.
In the first month (01) were 2 transactions
In the second month was 1 transaction
In the third month was 1 transaction
In the nineth month was 1 transactions
So the result should be (2+1+1+1) / 4 = 1.25, isn't is ?
How can I calculate it in Teradata SQL? Of course I showed you sample of my data.
SELECT ID, AVG(txns) FROM
(SELECT ID, TRUNC(trans_date,'MON') as mth, COUNT(*) as txns
FROM mytable
-- WHERE condition matches the question but likely want to
-- use end date 2021-09-30 or use mth instead of trans_date
WHERE trans_date BETWEEN date'2021-01-01' and date'2021-09-01'
GROUP BY id, mth) mth_txn
GROUP BY id;
Your logic translated to SQL:
--(2+1+1+1) / 4
SELECT id, COUNT(*) / COUNT(DISTINCT TRUNC(trans_date,'MON')) AS avg_tx
FROM mytable
WHERE trans_date BETWEEN date'2021-01-01' and date'2021-09-01'
GROUP BY id;
You should compare to Fred's answer to see which is more efficent on your data.

Subtract Daily amount from Withdraw Amount

I have two tables, tblWithdraw and tblProfit.
tblWithdraw Table
WithdrawId Date User Price
-----------------------------------------------------
1 2021-02-09 SANDANUWAN 2000.00
2 2021-02-09 GAYAN 300.00
3 2021-02-09 KASUN 1500.00
4 2021-02-09 AMAL 4000.00
5 2021-02-09 UDARA 340.00
6 2021-02-09 SULIN 200.00
7 2021-02-09 PERERA 120.00
tblProfit Table
Id Date Inv.No ItemName ItemCode Qty CostPrice DiscountPrice Amount
----------------------------------------------------------------------------------------------
1 2021-02-09 INV0000001 DELL LAP LP001 5 1500.00 1900.00 9500.00
2 2021-02-09 INV0000001 HP MOUSE MO001 7 2500.00 2940.00 20580.00
3 2021-02-09 INV0000001 PACIFIC FAN FAN001 6 2000.00 4000.00 24000.00
4 2021-02-09 INV0000001 SAMSUNG PHONE PH001 8 1000.00 1350.00 10800.00
This is my question. I want to sum all price from tblWithdraw table according to the date. Its mean withdraw date and profit table must be match. New I want to sum all amount from tblProfit table.Then I want to Subtract like this Price - Amount. Finally I want to show total amount of Profit table after subtracted. I used following join but is not working well. please help me to solve this problem. I want to subtract according to the date.
Select MAX(w.Date)Date, SUM(w.Price)Price, SUM(p.Amount)Amount
From tblWithdraw w
Left Join tblProfit p
ON w.Date = p.CurrentDate
Group by w.Date
Out put is like this. its wrong.
Date Price Amount
2021-02-09 33840.00 454160.00
Here's my CTE-based approach based on the comment which I made on the initial question.
WITH WithDraw AS(
SELECT
[date]
,SUM(price) PRICE
FROM tblWithdraw
GROUP BY [date]
),
Profit AS(
SELECT
[date]
,SUM(amount)AMOUNT
FROM tblProfit
GROUP BY [date]
)
SELECT
W.DATE
,W.PRICE
,P.AMOUNT
FROM WithDraw W
INNER JOIN Profit P ON w.date = p.date;
I'm not sure I fully understand the question, this is how I'm reading it:
Select w.Date,
w.agg_price_by_date,
w.agg_price_by_date - COALESCE(p.agg_amount_by_date, 0) AS diff_price_amount
From (SELECT Date,
SUM(Price) AS agg_price_by_date
FROM tblWithdraw w
GROUP
BY Date
) w
Left Join
(SELECT CurrentDate,
SUM(Amount) AS agg_amount_by_date
FROM tblProfit
GROUP
BY CurrentDate
) p
ON w.Date = p.CurrentDate

Datediff with dates in one column but only showing datediff compared to the next invoice

This is to understand how long it took the customer to pay a bill.
The datediff needs to be the next invoice to the payments
as Below example
ID Type1 Amount Date
--------------------------------------------------------------------
1 Invoice 38.16 2014-04-25
1 Payment -40.00 2014-03-23
1 Invoice 40.86 2014-02-22
1 Payment -40.00 2014-02-21
1 Invoice 42.21 2014-01-20
ID Type1 Amount Date DATEDIFF
---------------------------------------------------------
1 Invoice 38.16 2014-04-25
1 Payment -40.00 2014-03-23 29
1 Invoice 40.86 2014-02-22
1 Payment -40.00 2014-02-21 32
1 Invoice 42.21 2014-01-20
This can be done in many ways, one option is to use a correlated sub-query to get the date of the preceding row with item=invoice for all item=payment rows:
select
id, type1, amount, date,
datediff(day,
(select top 1 date
from table1
where date <= t.date
and type1= 'Invoice'
and t.type1='Payment'
order by date desc),
date) as diff
from table1 t;
This might not be the most efficient solution though.
Sample SQL Fiddle
Or you could use an outer apply with the same effect: http://www.sqlfiddle.com/#!6/b1e32/19

Sql for considering previous week same day sales if same day sales are 0

i have a store sales table which looks like below containing two years of data.
If the sales amount is '0' for a particular day for a particular store ,i have to take sales for same day last week.(current day-7)
if those sales are also '0' then sales for current_day-8
if those sales are also '0' then sales for current_day-9
if those sales are also '0' then sales for current_day-10
Sales_table1
day_id week_id sales Store
2/1/2014 201401 34566 1234
2/2/2014 201401 67777 567
2/3/2014 201401 3333 698
2/4/2014 201401 45644 345
2/5/2014 201401 2456 789
**2/6/2014 201401 3456 567**
2/7/2014 201401 5674 780
2/8/2014 201402 3333 1234
2/9/2014 201402 22222 567
2/10/2014 201402 111134 698
2/11/2014 201402 56789 345
2/12/2014 201402 4356 789
**2/13/2014 201402 0 567**
2/14/2014 201402 899 780
please give the query that i can use.
As you have not said which database you are using I'm giving a solution using SQL Server as that's what I'm most familiar with.
The logic of my query is to select all the rows where sales are greater than zero for each store, where the date of the sales info is either the MAX date in the table or the date is less than or equal to the MAX(date)-7 days (effectively exclude any sales in the previous 7 days). I'm doing this because you said you need to take the most recent sale or sales on day -7, -8, -9, etc.
From that set of rows the query then picks the most recent sale date.
;WITH Store_Dates AS (
SELECT store, MAX(day_id) max_date, DATEADD(day, -7, MAX(day_id)) old_date
FROM Sales_Table1
GROUP BY store
)
,Sale_Days AS (
SELECT MAX(day_id) day_id, Sales_Table1.store
FROM Sales_Table1 INNER JOIN Store_Dates
ON Sales_Table1.store = Store_Dates.store
WHERE sales > 0
AND (day_id = max_date
OR day_id <= old_date)
GROUP BY Sales_Table1.store
)
SELECT Sales_Table1.*
FROM Sales_Table1 INNER JOIN Sale_Days
ON Sales_Table1.store = Sale_Days.store
AND Sales_Table1.day_id = Sale_Days.day_id
SQL Fiddle
The only thing my query does not do is stop after going back X number of days. You could do that by limiting the number of rows returned from the Sale_Days query.

How to join tables based on the dates

COMMISSION table
PRODUCT_ID DATE COMMISSION
1 20110101 27.00
1 20120101 28.00
1 20130705 30.00
2 20110101 17.00
2 20120501 16.00
2 20130101 18.00
...
ORDER table
PRODUCT_ID DATE PRICE
1 20110405 2500
2 20130402 3000
2 20130101 1900
Desired output
PRODUCT_ID DATE PRICE COMMISSION
1 20110405 2500 27.00
2 20130402 3000 16.00
2 20130101 1900 18.00
Commission table records commission % based on the product id and date.
Order table is basically a record of orders placed on a particular date,
I'd like to join two tables and bring the appropriate commission based on the date of the order. For example, you can see that the first order's commission is 27.00 as the date for the product_id 1 falls between 20110101 and 20120101.
How do I do this? Seems like a simple 1 to n relationship but I can't figure it out.
Try
SELECT o.*,
(
SELECT TOP 1 commission
FROM commission
WHERE product_id = o.product_id
AND date <= o.date
ORDER BY date DESC
) commission
FROM [order] o
Here is SQLFiddle demo