Get cumulative sum that reset for each year - sql

Please consider this table:
Year Month Value YearMonth
2011 1 70 201101
2011 1 100 201101
2011 2 200 201102
2011 2 50 201102
2011 3 80 201103
2011 3 250 201103
2012 1 100 201201
2012 2 200 201202
2012 3 250 201203
I want to get a cumulative sum based on each year. For the above table I want to get this result:
Year Month Sum
-----------------------
2011 1 170
2011 2 420 <--- 250 + 170
2011 3 750 <--- 330 + 250 + 170
2012 1 100
2012 2 300 <--- 200 + 100
2012 3 550 <--- 250 + 200 + 100
I wrote this code:
Select c1.YearMonth, Sum(c2.Value) CumulativeSumValue
From #Tbl c1, #Tbl c2
Where c1.YearMonth >= c2.YearMonth
Group By c1.YearMonth
Order By c1.YearMonth Asc
But its CumulativeSumValue is calculated twice for each YearMonth:
YearMonth CumulativeSumValue
201101 340 <--- 170 * 2
201102 840 <--- 420 * 2
201103 1500
201201 850
201202 1050
201203 1300
How can I achieve my desired result?
I wrote this query:
select year, (Sum (aa.[Value]) Over (partition by aa.Year Order By aa.Month)) as 'Cumulative Sum'
from #Tbl aa
But it returned multiple records for 2011:
Year Cumulative Sum
2011 170
2011 170
2011 420
2011 420
2011 750
2011 750
2012 100
2012 300
2012 550

You are creating a cartesian product here. In your ANSI-89 implicit JOIN (you really need to stop using those and switch to ANSI-92 syntax) you are joining on c1.YearMonth >= c2.YearMonth.
For your first month you have two rows with the same value of the year and month, so each of those 2 rows joins to the other 2; this results in 4 rows:
Year
Month
Value1
Value2
2011
1
70
70
2011
1
70
100
2011
1
100
70
2011
1
100
100
When you SUM this value you get 340, not 170, as you have 70+70+100+100.
Instead of a triangular JOIN however, you should be using a windowed SUM. As you want to also get aggregate nmonths into a single rows, you'll need to also aggregate inside the windowed SUM like so:
SELECT V.YearMonth,
SUM(SUM(V.Value)) OVER (PARTITION BY Year ORDER BY V.YearMonth) AS CumulativeSum
FROM (VALUES (2011, 1, 70, 201101),
(2011, 1, 100, 201101),
(2011, 2, 200, 201102),
(2011, 2, 50, 201102),
(2011, 3, 80, 201103),
(2011, 3, 250, 201103),
(2012, 1, 100, 201201),
(2012, 2, 200, 201202),
(2012, 3, 250, 201203)) V (Year, Month, Value, YearMonth)
GROUP BY V.YearMonth,
V.Year;

Related

Sales amounts of the top n selling vendors by month with other fields in bigquery

i have a table in bigquery like this (260000 rows):
vendor date item_price discount_price
x 2021-07-08 23:41:10 451,5 0
y 2021-06-14 10:22:10 41,7 0
z 2020-01-03 13:41:12 74 4
s 2020-04-12 01:14:58 88 12
....
exactly what I want is to group this data by month and find the sum of the sales of only the top 20 vendors in that month. Expected output:
month vendor_name(top20) sum_of_vendor's_sales sum_of_vendor's_discount item_count(sold)
2020-01 x1 10857 250 150
2020-01 x2 9685 410 50
2020-01 x3 3574 140 45
....
2021 01 x20 700 15 20
2020-02 y1 7421 280 120
2020-02 y2 6500 250 40
2020-02 y3 4500 200 70
.....
2020-02 y20 900 70 30
i tried this (source here). But The desired output could not be obtained.
select month,
(select sum(sum) from t.top_20_vendors) as sum_of_only_top20_vendor_sales
from (
select
format_datetime('%Y%m', date) month,
approx_top_sum(vendor, item_price, 20) top_20_vendors,count(item_price) as count_of_items,sum(discount_price)
from my_table
group by month
) t
Consider below approach
select
format_datetime('%Y%m', date) month,
vendor as vendor_name_top20,
sum(item_price) as sum_of_vendor_sales,
sum(discount_price) as sum_of_vendor_discount,
count(*) as item_count_sold
from your_table
group by vendor, month
qualify row_number() over(partition by month order by sum_of_vendor_sales desc) <= 20

Oracle - Getting the rows w/ condition of two columns having minimum values

I am a newbie to PLSQL. I would like to ask for your help.
I have a table below.
Item
Week
Qty
DMD_WK
DMD_QTY
ACC_DMD
WIP
H00978A510
26
300
26
0
0
1
H00978A510
26
300
27
0
0
2
H00978A510
26
300
28
300
300
3
H00978A510
26
300
29
100
400
3
H00978A510
26
300
30
100
500
4
first of all, I want to filter the records that has QTY < ACC_DMD. so the result will be below(I'm okay w/ this part)
Item
Week
Qty
DMD_WK
DMD_QTY
ACC_DMD
WIP
H00978A510
26
300
29
100
400
3
H00978A510
26
300
30
100
500
4
then, I need to get the row having the minimum DMD_WK and also having the minimum WIP grouped by item, week and qty after the filter being applied(Need help on this part)
so that the query will result in this:
Item
Week
Qty
DMD_WK
DMD_QTY
ACC_DMD
WIP
H00978A510
26
300
29
100
400
3
Hoping for your time and help, thanks in advance.
SELECT *
FROM YOURTABLE t
WHERE
DMD_WK = (SELECT MIN(tin.DMD_WK)
FROM YOURTABLE tin
WHERE tin.ITEM = t.ITEM AND tin.QTY < tin.ACC_DMD
GROUP BY tin.ITEM )
AND WIP = (SELECT MIN(tin.WIP)
FROM YOURTABLE tin
WHERE tin.ITEM = t.ITEM AND tin.QTY < tin.ACC_DMD
GROUP BY tin.ITEM )

Sum and Count by month, shown with last day of that month

I have a transaction table like this:
Trandate channelID branch amount
--------- --------- ------ ------
01/05/2019 1 2 2000
11/05/2019 1 2 2200
09/03/2020 1 2 5600
15/03/2020 1 2 600
12/10/2019 2 10 12000
12/10/2019 2 10 12000
15/11/2019 4 7 4400
15/02/2020 4 2 2500
I need to sum amount and count transactions by year and month. I tried this:
select DISTINCT
DATEPART(YEAR,a.TranDate) as [YearT],
DATEPART(MONTH,a.TranDate) as [monthT],
count(*) as [countoftran],
sum(a.Amount) as [amount],
a.Name as [branch],
a.ChannelName as [channelID]
from transactions as a
where a.TranDate>'20181231'
group by a.Name, a.ChannelName, DATEPART(YEAR,a.TranDate), DATEPART(MONTH,a.TranDate)
order by a.Name, YearT, MonthT
It works like charm. However, I will use this data on PowerBI thus I cannot show these results in a "line graphic" due to the year and month info being in separate columns.
I tried changing format on SQL to 'YYYYMM' alas powerBI doesn't recognise this column as date.
So, in the end, I need a result table looks like this:
YearT channelID branch Tamount TranT
--------- --------- ------ ------- -----
31/05/2019 1 2 4400 2
30/03/2020 1 2 7800 2
31/10/2019 2 10 24000 2
30/11/2019 4 7 4400 1
29/02/2020 4 2 2500 1
I have tried several little changes with no result.
Help is much appreciated.
You may try with the following statement:
SELECT
EOMONTH(DATEFROMPARTS(YEAR(Trandate), MONTH(Trandate), 1)) AS YearT,
branch, channelID,
SUM(amount) AS TAmount,
COUNT(*) AS TranT
FROM (VALUES
('20190501', 1, 2, 2000),
('20190511', 1, 2, 2200),
('20200309', 1, 2, 5600),
('20200315', 1, 2, 600),
('20191012', 2, 10, 12000),
('20191012', 2, 10, 12000),
('20191115', 4, 7, 4400),
('20200215', 4, 2, 2500)
) v (Trandate, channelID, branch, amount)
GROUP BY DATEFROMPARTS(YEAR(Trandate), MONTH(Trandate), 1), branch, channelID
ORDER BY DATEFROMPARTS(YEAR(Trandate), MONTH(Trandate), 1)
Result:
YearT branch channelID TAmount TranT
2019-05-31 2 1 4200 2
2019-10-31 10 2 24000 2
2019-11-30 7 4 4400 1
2020-02-29 2 4 2500 1
2020-03-31 2 1 6200 2

Trailing Twelve Month Formula using MS Access Database

I have a database named Cash_Flow_Statements and I need to create a saved query that calculates a Trailing Twelve Month (TTM) Free Cash Flow.
Here is an example of my database:
And here is the code for my saved query. I have used this code for other calculations so I know it works. I just don't know how to do a Trailing Twelve Month in SQL.
CREATE PROC " & Calculation & " AS _
SELECT Income_Statements.Ticker, Income_Statements.[Year], Income_Statements.Period, _
" & Formula & " AS TTM _
FROM (Income_Statements AS Income_Statements INNER JOIN Balance_Sheets AS Balance_Sheets ON (Income_Statements.Ticker = Balance_Sheets.Ticker) AND (Income_Statements.[Year] = Balance_Sheets.[Year]) AND (Income_Statements.Period = Balance_Sheets.Period)) _
INNER JOIN Cash_Flow_Statements AS Cash_Flow_Statements ON (Balance_Sheets.Ticker = Cash_Flow_Statements.Ticker) AND (Balance_Sheets.[Year] = Cash_Flow_Statements.[Year]) AND (Balance_Sheets.Period = Cash_Flow_Statements.Period)
I need the variable Formula to contain the equation that will calculate the Trailing Twelve Month for Free Cash Flow. Here is what the math looks like:
Free Cash Flow TTM = Sum(Operating Cash Flow MRQ1 + Operating Cash Flow MRQ2 + Operating Cash Flow MRQ3 + Operating Cash Flow MRQ4) - Sum(Capital Expenditures MRQ1 + Capital Expenditures MRQ2 + Capital Expenditures MRQ3 + Capital Expenditures MRQ4)
MRQ = Most Recent Quarter
I need this formula to be in SQL. How do I do this?
UPDATE
Per #Gord Thompson's answer below I have come up with this code:
SELECT Ticker, [Year], Period, (SELECT (SUM(su.Net_Cash_Flow_Operating) - SUM(su.Capital_Expenditures)) _
FROM (SELECT Ticker, [Year], Period, Net_Cash_Flow_Operating, Capital_Expenditures _
FROM Cash_Flow_Statements _
UNION ALL _
SELECT Ticker, [Year] + 1, Period - 4, Net_Cash_Flow_Operating, Capital_Expenditures _
FROM Cash_Flow_Statements) su _
WHERE su.Ticker = s.Ticker AND su.[Year] = s.[Year] AND (su.Period Between s.Period - 3 And s.Period)) _
AS SalesLast12Months _
FROM Cash_Flow_Statements AS s
When this is run by itself it returns exactly what I need. But as I mentioned in my original post I need to be able to set the variable Formula to contain the SQL statement and then insert it into the existing SQL statement.
So I've tried to adapt #Gord Thompson answer and I've come up this this.
Formula:
Formula = “(SELECT (SUM(su.Net_Cash_Flow_Operating) - SUM(su.Capital_Expenditures)) _
FROM (SELECT Ticker, [Year], Period, Net_Cash_Flow_Operating, Capital_Expenditures _
FROM Cash_Flow_Statements UNION ALL SELECT Ticker, [Year] + 1, Period - 4, Net_Cash_Flow_Operating, Capital_Expenditures _
FROM Cash_Flow_Statements) su _
WHERE su.Ticker = Cash_Flow_Statements.Ticker AND su.[Year] = Cash_Flow_Statements.[Year] AND (su.Period Between Cash_Flow_Statements.Period - 3 And Cash_Flow_Statements.Period))”
SQL statement:
"CREATE PROC " & Calculation & " AS SELECT Income_Statements.Ticker, Income_Statements.[Year], Income_Statements.Period, " & Formula & " AS TTM _
FROM (Income_Statements AS Income_Statements _
INNER JOIN Balance_Sheets AS Balance_Sheets ON (Income_Statements.Ticker = Balance_Sheets.Ticker) AND (Income_Statements.[Year] = Balance_Sheets.[Year]) AND (Income_Statements.Period = Balance_Sheets.Period)) _
INNER JOIN Cash_Flow_Statements AS Cash_Flow_Statements ON (Balance_Sheets.Ticker = Cash_Flow_Statements.Ticker) AND (Balance_Sheets.[Year] = Cash_Flow_Statements.[Year]) AND (Balance_Sheets.Period = Cash_Flow_Statements.Period)
This is the result:
Again this is very close but not quite what I need. I believe that I know what causes the code to not work when I adapt it but I don't know how to fix it yet. In #Gord Thompson code the last line is FROM Cash_Flow_Statements AS s. When I insert the variable formula into my existing SQL statement from my original post, the code is FROM (Income_Statements AS Income_Statements _
INNER JOIN Balance_Sheets AS Balance_Sheets ON (Income_Statements.Ticker = Balance_Sheets.Ticker) AND (Income_Statements.[Year] = Balance_Sheets.[Year]) AND (Income_Statements.Period = Balance_Sheets.Period)) _
INNER JOIN Cash_Flow_Statements AS Cash_Flow_Statements ON (Balance_Sheets.Ticker = Cash_Flow_Statements.Ticker) AND (Balance_Sheets.[Year] = Cash_Flow_Statements.[Year]) AND (Balance_Sheets.Period = Cash_Flow_Statements.Period). I'm almost positive that this is where the problems lies but do not know how to correct it.
Let's not get too hung up on the columns, let's concentrate on selecting the appropriate rows.
Say we had a table named [SalesSummary] containing
Item FiscalYear Quarter TotalSales
-------- ---------- ------- ----------
bicycles 2011 1 100
bicycles 2011 2 200
bicycles 2011 3 300
bicycles 2011 4 400
bicycles 2012 1 500
bicycles 2012 2 600
bicycles 2012 3 700
bicycles 2012 4 800
ham 2011 1 10
ham 2011 2 20
ham 2011 3 30
ham 2011 4 40
ham 2012 1 50
ham 2012 2 60
ham 2012 3 70
ham 2012 4 80
If we wanted to create a query that showed the quarterly figures along with the total sales for the past 12 months (current quarter plus the previous 3 quarters) one might think that we would have to start messing around with IIf() to handle the "wrap around" from one FiscalYear to the next. Fortunately, we don't, because we can just replicate the same data with "fake" quarters for the next FiscalYear: 2011_Q(4) becomes 2012_Q(0), 2011_Q(3) becomes 2012_Q(-1), and so on.
SELECT
Item,
FiscalYear,
Quarter,
TotalSales
FROM SalesSummary
UNION ALL
SELECT
Item,
FiscalYear + 1,
Quarter - 4,
TotalSales
FROM SalesSummary
returns
Item FiscalYear Quarter TotalSales
-------- ---------- ------- ----------
bicycles 2011 1 100
bicycles 2011 2 200
bicycles 2011 3 300
bicycles 2011 4 400
bicycles 2012 1 500
bicycles 2012 2 600
bicycles 2012 3 700
bicycles 2012 4 800
ham 2011 1 10
ham 2011 2 20
ham 2011 3 30
ham 2011 4 40
ham 2012 1 50
ham 2012 2 60
ham 2012 3 70
ham 2012 4 80
bicycles 2012 -3 100
bicycles 2012 -2 200
bicycles 2012 -1 300
bicycles 2012 0 400
bicycles 2013 -3 500
bicycles 2013 -2 600
bicycles 2013 -1 700
bicycles 2013 0 800
ham 2012 -3 10
ham 2012 -2 20
ham 2012 -1 30
ham 2012 0 40
ham 2013 -3 50
ham 2013 -2 60
ham 2013 -1 70
ham 2013 0 80
If we save that query as [SalesUnion] then we can use it to produce our totals like so
SELECT
Item,
FiscalYear,
Quarter,
TotalSales,
(
SELECT
SUM(su.TotalSales)
FROM SalesUnion su
WHERE su.Item = s.Item
AND su.FiscalYear = s.FiscalYear
AND (su.Quarter Between s.Quarter - 3 And s.Quarter)
) AS SalesLast12Months
FROM SalesSummary s
Or, if you don't want to rely on a saved query you can do it all in one go:
SELECT
Item,
FiscalYear,
Quarter,
TotalSales,
(
SELECT
SUM(su.TotalSales)
FROM
(
SELECT
Item,
FiscalYear,
Quarter,
TotalSales
FROM SalesSummary
UNION ALL
SELECT
Item,
FiscalYear + 1,
Quarter - 4,
TotalSales
FROM SalesSummary
) su
WHERE su.Item = s.Item
AND su.FiscalYear = s.FiscalYear
AND (su.Quarter Between s.Quarter - 3 And s.Quarter)
) AS SalesLast12Months
FROM SalesSummary s
Either way, the results are
Item FiscalYear Quarter TotalSales SalesLast12Months
-------- ---------- ------- ---------- -----------------
bicycles 2011 1 100 100
bicycles 2011 2 200 300
bicycles 2011 3 300 600
bicycles 2011 4 400 1000
bicycles 2012 1 500 1400
bicycles 2012 2 600 1800
bicycles 2012 3 700 2200
bicycles 2012 4 800 2600
ham 2011 1 10 10
ham 2011 2 20 30
ham 2011 3 30 60
ham 2011 4 40 100
ham 2012 1 50 140
ham 2012 2 60 180
ham 2012 3 70 220
ham 2012 4 80 260
The variable Formula should contain this:
SELECT (SUM(su.Net_Cash_Flow_Operating) - SUM(su.Capital_Expenditures)) FROM (SELECT Ticker, [Year], Period, Net_Cash_Flow_Operating, Capital_Expenditures FROM Cash_Flow_Statements UNION ALL SELECT Ticker, [Year] + 1, Period - 4, Net_Cash_Flow_Operating, Capital_Expenditures FROM Cash_Flow_Statements) su WHERE su.Ticker = c.Ticker AND su.[Year] = c.[Year] AND (su.Period Between c.Period - 3 And c.Period)

Join Three tables with Sum of column in access query

I have Three tables as shown below..
I need output as shown in output table
for this i need to join three tables and order output in month order
tbl_MonthList
MonthID MonthList
1 January
2 February
3 March
4 April
5 May
6 June
7 July
8 August
9 September
10 October
11 November
12 December
tbl_Amount:
Month_id Amount_Received Customer_id
3 500 aaa
3 1000 bbb
4 700 jjj
5 300 aaa
5 400 jjj
5 500 ppp
7 1000 aaa
10 1500 bbb
12 700 jjj
tbl_Month_Target
MonthID MonthF_L
1 10000
2 150000
3 1000
4 50000
5 5000
6 3000
7 20000
8 12000
9 34000
10 85000
11 34000
12 45000
I need output as shown below
Month Total_amount MonthF_L
January 0 10000
February 0 150000
March 2000 1000
April 700 50000
May 1200 5000
June 0 3000
July 1000 20000
August 0 12000
September 0 34000
October 1500 85000
November 0 34000
December 700 45000
SELECT ML.MonthList AS Month,
Sum(A.Amount_Received) AS Total_amount,
First(MT.MonthF_L) AS MonthF_L
FROM (tbl_MonthList AS ML
INNER JOIN tbl_Month_Target AS MT ON ML.MonthID = MT.MonthID)
LEFT JOIN tbl_Amount AS A ON ML.MonthID = A.Month_id
GROUP BY ML.MonthList, ML.MonthID
ORDER BY ML.MonthID
Note: In MS Access, multiple joins must be explicitly nested within parentheses
Try this:
select ml.MonthList, sum(a.Amount_Received), mt.MonthF_L from tbl_MonthList ml
left join tbl_Month_Target mt on mt.MonthID = ml.MonthID
left join tbl_Amount a on ml.Month_id = ml.MonthID
group by ml.MonthList, mt.MonthF_L