Aggregating data to get a running total month on month - sql-server-2008

I have a table which holds the below data
This issue im having is that i need a running total for each month, I've managed to create this is an excel sheet pretty easily but when i try anything in SQL the data result varies.
The image below shows the sum of each paid amount by month, then a total of each one added onto it. I've edited excel to show the formula and the result of the formula. Also have the result i get from SQL 2008 when using (example only)
***UPDATE - The result set im trying to achieve that is in the excel document is for example month 117 + Month 118 gives Month118 TotalToDate, then month 118 + 119 gives Months 119 Total to Date.
Not sure how else to explain this?
( select sum(paid) from #tmp005 t2 where t2.[monthid] <=
t5.[monthid] ) as paid
Really feel that this is less complicated than what I think!

As I understand this you are trying to get a running total month by month, the below CTE should do what you want.
--create table #temp (M_ID Int, Paid Float)
--Insert Into #temp VALUES (116, '50.00'), (117, '50.00'),(117, '5.00'),(117, '20.00'),(117, '10.00'),(117, '75.40'),(118, '125.00'),(118, '200.00'),(118, '5.00')
;WITH y AS
(
SELECT M_ID, paid, rn = ROW_NUMBER() OVER (ORDER BY M_ID)
FROM #temp
), x AS
(
SELECT M_ID, rn, paid, rt = paid
FROM y
WHERE rn = 1
UNION ALL
SELECT y.M_ID, y.rn, y.paid, x.rt + y.paid
FROM x INNER JOIN y
ON y.rn = x.rn + 1
)
SELECT M_ID, MAX(rt) as RunningTotal
FROM x
Group By M_ID
OPTION (MAXRECURSION 10000);
It is based on the first 3 M_ID of your sample data, just change around the #temp to your specific table, I didn't know whether you had another unique identifier in the table which is why I had to use the ROW_NUMBER()but this should order it correctly based on the M_ID field.

I guess that you are storing the month in a separated table and using M_ID to reference it. So, to get the sum of each month do this:
SELECT [M_ID]
,sum([Paid])
FROM #tmp005
GROUP BY [M_ID]

I think I'd use a correlated sub query:-
select r.m_id,
(
select sum(csq.paid)
from #tmp005 csq
where csq.m_id<=r.m_id
)
from (
select distinct m_id
from #tmp005
) r
Hopefully you can figure out how to apply it to your circumstance/schema.

Related

How to create a MySQL Query with accumulation on a created field?

I have two tables, Invoice and Receiving, and I am using MySQL. I want to generate a balance sheet from these two tables. The result should accumulate a balance over time based on the I_Total column from Invoice and the CR_Amount column from Receiving as shown in the figures below. I have tried many queries with different joins but I am not getting the desired output.
How can I achieve the desired balance sheet with a query or function in MySQL?
If we ignore the balance column for a moment, and assuming that the date column is DATE datatype, then getting the result shown can be achieved with something like this:
SELECT t.date
, t.debit
, t.credit
FROM ( SELECT i.i_date AS date
, i.i_total AS debit
, 0 AS credit
, 'i' AS i_or_r
, i.i_id AS id
FROM invoice i
UNION ALL
SELECT r.r_date AS date
, 0 AS debit
, r.r_total AS credit
, 'r' AS i_or_r
, r.r_id AS id
FROM receiving r
) t
ORDER
BY t.date
, t.i_or_r
, t.id
To get the balance, we could do that processing on the client side, as the rows are retrieved.
NOTE: MySQL 8.0 introduces window functions, which have been available in other RDBMS such as SQL Server and Oracle (calls them "Analytic Functions").
Without window functions, to get it done in the SQL is going to be ugly.
We could make use of unsupported usage of user-defined variables. Using this approach, we would basically emulate the processing that we would do on the client side, fetching through the result that query (processing each row in order) to add/subtract from a "running balance" in a user-defined variable.
The "ugly" part about this is that it relies on behavior that is not guaranteed. The MySQL Reference Manual includes warning about it.)
Or, to get the result using pure SQL, we could use a couple of complicated looking correlated subqueries to sum up the debit and credit amounts up to the current row, and do that for each row.
It looks like we are applying debits and credits towards the balance in a similar order that a bank does, applying all of the debits and credits in date order. And on each date, we apply the debits first, and then the credits.
From the sample data and expected result, it's not clear if debits are applied ordered by amount in ascending order, or by id in ascending order.
With the sample data, we get the same balance result either way. Assuming that i_id is unique in invoice, and cr_id is unique in receiving, we can get the balance by applying credits and debits using id order as a discriminator when we're at the current date.
(If we need to apply credits on the same date in ascending amount order, the subqueries would be a little more complicated, to take into account the possibility that two credits on the same date could be for the same amount.)
SELECT t.date
, t.debit
, t.credit
, ( SELECT SUM(bi.i_total)
FROM invoice bi
WHERE bi.i_date <= t.date
AND ( bi.i_date < t.date
OR ( t.i_or_r = 'i' AND bi.i_id <= t.id )
)
)
- ( SELECT SUM(br.cr_amount)
FROM receiving br
WHERE br.cr_date <= t.date
AND ( br.cr_date < t.date
OR ( t.i_or_r = 'r' AND br.cr_id <= t.id )
OR t.i_or_r = 'i'
)
) AS balance
FROM ( SELECT i.i_date AS date
, i.i_total AS debit
, 0 AS credit
, 'i' AS i_or_r
, i.i_id AS id
FROM invoice i
UNION ALL
SELECT r.cr_date AS date
, 0 AS debit
, r.cr_amount AS credit
, 'r' AS i_or_r
, r.cr_id AS id
FROM receiving r
) t
ORDER
BY t.date
, t.i_or_r
, t.id
Try This
SELECT *, SUM(Debit) OVER(ORDER BY dt,debit ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)-SUM(Credit) OVER(ORDER BY dt,debit ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) Balance
FROM (
SELECT ID,Dt,Amount AS Debit,0 AS Credit FROM Debit
UNION ALL
SELECT ID,Dt,0 AS Debit,Amount AS Credit FROM Credit
)X
ORDER BY DT
DEMO

Displaying data with respect to specific date?

I am trying to make a reporting system where I need to display report
for each date.
These is my table schema for selected_items
This is stock_list
I am using php in the back-end and java in the front end to display
the data. I tried a couple of queries to get the desired output but so
far I am not able to get it.These are some of the queries i used.
SELECT
COALESCE(stock_list.date, selected_items.date) AS date,
SUM( stock_list.qty ) AS StockSum,
SUM( stock_list.weight ) AS Stockweight,
COUNT( selected_items.barcode ) AS BilledItems,
SUM( selected_items.weight ) AS Billedweight
FROM stock_list join selected_items
ON stock_list.date = selected_items.date
GROUP BY COALESCE(stock_list.date, selected_items.date)
ORDER BY COALESCE(stock_list.date, selected_items.date);
This gives me the first five columns but the output gives me wrong values.
Then I also tried Union.
SELECT SUM( qty ) AS StockSum, SUM( weight ) AS Stockweight
FROM `stock_list`
WHERE DATE LIKE '08-Jan-2016'
UNION SELECT COUNT( barcode ) AS BilledItems, SUM( weight ) AS Billedweight
FROM `selected_items`
WHERE DATE LIKE '08-Jan-2016'
UNION SELECT SUM( qty ) AS TotalStock, SUM( weight ) AS TotalWeight
FROM `stock_list`;
Here I get the correct values for four columns but the problem is the >result is displayed in two columns when I would like it to be in 4 columns.
Can anyone guide me please I have figured the java part of it but I am not good at php and mysql.
Thank you
Unfortunately, SQL Fiddle crashed while I was trying to execute this query
SELECT sl.date AS date, B.qtySum AS StockSum, B.weightSum AS Stockweight,
C.barcodeCount AS BilledItems, C.weightSum AS Billedweight
FROM stock_list sl
JOIN (SELECT SUM(qty) as qtySum, SUM(weight) as weightSum
FROM STOCK_LIST GROUP BY date) AS B
ON B.date = sl.date
JOIN (SELECT SUM (weight) AS weightSum, COUNT(barcode) AS barcodeCount
FROM SELECTED_ITEMS GROUP BY date) AS C
ON C.date = sl.date;
As it was tried here. The problem with joins is that the rows will be joined multiple times and thus, the sum goes awry. For example, you have four rows that are joined from the second table and so the sum is four times higher as it should. With subqueries you can avoid this problem as you count and sum up variables before joining them and therefore, the numbers should fit. Alas, I couldn't run the query so I'm not 100% sure it works, but it should be the right approach.

Cumulative SQL to work out no of payers

Currently trying to create a query that shows how many accounts have paid month on month but on a cumulative basis (penetration). So as an example I have a table with Month paid and account number, which shows what month that account paid.
Month | AccountNo
Jan-14 | 123456
Feb-14 | 321654
So using the above the result set would show
Month | Payers
Jan-14 | 1
Feb-14 | 2
being because one account paid in Jan, then one in Feb meaning that there have been by the end of Feb 2 payments overall, but only one in Jan. Tried a few inner joins back onto the table itself with a t1.Month >= t2.Month as i would for a normal cumulative query but the result is always out.
Any questions please ask, unsure if the above will be clear to anyone but me.
If you have date in the table then you can try the following query.
SELECT [Month]
,(SELECT COUNT(AccountNo)
FROM theTable i
-- This is to make sure to add until the last day of the current month.
WHERE i.[Date] <= DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,o.[Date])+1,0)) AS CumulativeCount
FROM theTable o
Ok, several things. You need to have an actual date field, as you can't order by the month column you have.
You need to consider there may be gaps in the months - i.e. some months where there is no payment (not sure if that is true or not)
I'd recommend a recursive common table expression to do the actual aggregation
Heres how it works out:
-- setup
DECLARE #t TABLE ([Month] NCHAR(6), AccountNo INT)
INSERT #t ( [Month], AccountNo )
VALUES ( 'Jan-14',123456),('Feb-14',456789),('Apr-14',567890)
-- assume no payments in march
; WITH
t2 AS -- get a date column we can sort on
(
SELECT [Month],
CONVERT(DATETIME, '01 ' + REPLACE([Month], '-',' '), 6) AS MonthStart,
AccountNo
FROM #t
),
t3 AS -- group by to get the number of payments in each month
(
SELECT [Month], MonthStart, COUNT(1) AS PaymentCount FROM t2
GROUP BY t2.[Month], t2.MonthStart
),
t4 AS -- get a row number column to order by (accounting for gaps)
(
SELECT [Month], MonthStart, PaymentCount,
ROW_NUMBER() OVER (ORDER BY MonthStart) AS rn FROM t3
),
t5 AS -- recursive common table expression to aggregate subsequent rows
(
SELECT [Month], MonthStart, PaymentCount AS CumulativePaymentCount, rn
FROM t4 WHERE rn = 1
UNION ALL
SELECT t4.[Month], t4.MonthStart,
t4.PaymentCount + t5.CumulativePaymentCount AS CumulativePaymentCount, t4.rn
FROM t5 JOIN t4 ON t5.rn + 1 = t4.rn
)
SELECT [Month], CumulativePaymentCount FROM t5 -- select desired results
and the results...
Month CumulativePaymentCount
Jan-14 1
Feb-14 2
Apr-14 3
If your month column is date type then its easy to work on else you need some additional conversion for it. Here the query goes...
create table example (
MONTHS datetime,
AccountNo INT
)
GO
insert into example values ('01/Jan/2009',300345)
insert into example values ('01/Feb/2009',300346)
insert into example values ('01/Feb/2009',300347)
insert into example values ('01/Mar/2009',300348)
insert into example values ('01/Feb/2009',300349)
insert into example values ('01/Mar/2009',300350)
SELECT distinct datepart (m,months),
(SELECT count(accountno)
FROM example b
WHERE datepart (m,b.MONTHS) <= datepart (m,a.MONTHS)) AS Total FROM example a

How to GROUP BY consecutive data (date in this case)

I have a products table and a sales table that keeps record of how many items a given product sold during each date. Of course, not all products have sales everyday.
I need to generate a report that tells me how many consecutive days a product has had sales (from the latest date to the past) and how many items it sold during those days only.
I'd like to tell you how many things I've tried so far, but the only succesful (and slow, recursive) ones are solutions inside my application and not inside SQL, which is what I want.
I also have browsed several similar questions on SO but I haven't found one that lets me have a clear idea of what I really need.
I've setup a SQLFiddle here to show you what I'm talking about. There you will see the only query I can think of, which doesn't give me the result I need. I also added comments there showing what the result of the query should be.
I hope someone here knows how to accomplish that. Thanks in advance for any comments!
Francisco
http://sqlfiddle.com/#!2/20108/1
Here is a store procedure that do the job
CREATE PROCEDURE myProc()
BEGIN
-- Drop and create the temp table
DROP TABLE IF EXISTS reached;
CREATE TABLE reached (
sku CHAR(32) PRIMARY KEY,
record_date date,
nb int,
total int)
ENGINE=HEAP;
-- Initial insert, the starting point is the MAX sales record_date of each product
INSERT INTO reached
SELECT products.sku, max(sales.record_date), 0, 0
FROM products
join sales on sales.sku = products.sku
group by products.sku;
-- loop until there is no more updated rows
iterloop: LOOP
-- Update the temptable with the values of the date - 1 row if found
update reached
join sales on sales.sku=reached.sku and sales.record_date=reached.record_date
set reached.record_date = reached.record_date - INTERVAL 1 day,
reached.nb=reached.nb+1,
reached.total=reached.total + sales.items;
-- If no more rows are updated it means we hit the most longest days_sold
IF ROW_COUNT() = 0 THEN
LEAVE iterloop;
END IF;
END LOOP iterloop;
-- select the results of the temp table
SELECT products.sku, products.title, products.price, reached.total as sales, reached.nb as days_sold
from reached
join products on products.sku=reached.sku;
END//
Then you just have to do
call myProc()
A solution in pure SQL without store procedure : Fiddle
SELECT sku
, COUNT(1) AS consecutive_days
, SUM(items) AS items
FROM
(
SELECT sku
, items
-- generate a new guid for each group of consecutive date
-- ie : starting with day_before is null
, #guid := IF(#sku = sku and day_before IS NULL, UUID(), #guid) AS uuid
, #sku := sku AS dummy_sku
FROM
(
SELECT currents.sku
, befores.record_date as day_before
, currents.items
FROM sales currents
LEFT JOIN sales befores
ON currents.sku = befores.sku
AND currents.record_date = befores.record_date + INTERVAL 1 DAY
ORDER BY currents.sku, currents.record_date
) AS main_join
CROSS JOIN (SELECT #sku:=0) foo_sku
CROSS JOIN (SELECT #guid:=UUID()) foo_guid
) AS result_to_group
GROUP BY uuid, sku
The query is really not that hard. Declare variables via cross join (SELECT #type:=0) type. Then in the selects, you can set variables value row by row. It is necessary for simulating Rank function.
select
p.*,
sum(s.items) sales,
count(s.record_date) days_sold
from
products p
join
sales s
on
s.sku = p.sku
where record_date between '2013-04-18 00:00:00' and '2013-04-26 00:00:00'
group by sku;

T-SQL: Find the last occurence of a aggregate state before today

I have a table with inventory transactions. A simplified example:
--Inventory Transactions
Date Sold Purchased Balance(not in table)
Today 1 -5
Yesterday 6 -4
5 days ago 5 +2
10 days ago 103 -3
20 days ago 100 +100
Requirements indicate that a report should contain the day since an article had negative balance (stockout). In the example above it would mean yesterday as the answer.
I'm trying to translate this into SQL but I'm having some trouble. I have tried using a CTE:
with Stockouts as (
select getdate() as [Date],
(calculation) as Balance
from [Inventory Transactions]
--some constraints to get the correct article are omitted
union all
select dateadd(dd, -1, Stockouts.[Date]) as [Date],
Stockouts.Balance - (calculation) as Balance
from [Inventory Transactions]
inner join Stockouts
)
But there is the problem that I cannot use a subquery in the recursive part (to find the last transaction before the current one) and an inner join will stop looping when there is no transaction on a certain date (so the dateadd part will fail as well).
What would be the best approach to solve this issue?
I think the best approach is to use OUTER APPLY like so:
DECLARE #InventoryTransactions TABLE ([Date] DATE, Sold INT, Purchased INT)
INSERT #InventoryTransactions VALUES
('20120504', 1, 0),
('20120503', 6, 0),
('20120501', 0, 5),
('20120425', 103, 0),
('20120415', 0, 100)
SELECT trans.Date,
trans.Sold,
trans.Purchased,
ISNULL(Balance, 0) [BalanceIn],
ISNULL(Balance, 0) + (Purchased - Sold) [BalanceOut]
FROM #InventoryTransactions trans
OUTER APPLY
( SELECT SUM(Purchased - Sold) [Balance]
FROM #InventoryTransactions bal
WHERE Bal.Date < trans.Date
) bal
Your approach is not well suited to recursion. If you require all dates then it would be best to create a date table and LEFT JOIN the results from the above to the table containing all dates. It is probably best to have a permanent table of dates (something like dbo.Calendar) as they are usable in a number of situations, but you can always create a temp table using either Loops, a CTE, or system views to manipulate it. The question on how to generate a list of incrementing dates has been answered before
EDIT
Just re-read your requirements and I think this is a better approach to get what you actually want (uses the same sample data).
;WITH Transactions AS
( SELECT trans.Date,
trans.Sold,
trans.Purchased,
ISNULL(Balance, 0) [BalanceIn],
ISNULL(Balance, 0) + (Purchased - Sold) [BalanceOut]
FROM #InventoryTransactions trans
OUTER APPLY
( SELECT SUM(Purchased - Sold) [Balance]
FROM #InventoryTransactions bal
WHERE Bal.Date < trans.Date
) bal
)
SELECT DATEDIFF(DAY, MAX(Date), CURRENT_TIMESTAMP) [Days Since Negative Balance]
FROM Transactions
WHERE BalanceIn > 0
EDIT 2
I Have create an SQL Fiddle to demonstrate the difference in query plans between OUTER APPLY and Recursion. You can see that the CTE is masses more work, and when running the same data on my local machine it tells me that when running the two in the same batch the outer apply method has a relative batch cost of 17% less than a quarter of the 83% taken up by the Recursive CTE method.
If you want to do it in a recursive cte. This could be a suggestion:
Test data:
DECLARE #T TABLE(Date DATETIME,Sold INT, Purchased INT)
INSERT INTO #T
VALUES
(GETDATE(),1,NULL),
(GETDATE()-1,6,NULL),
(GETDATE()-5,NULL,5),
(GETDATE()-10,103,NULL),
(GETDATE()-20,NULL,100)
Query
;WITH CTE
AS
(
SELECT ROW_NUMBER() OVER(ORDER BY Date ASC) AS RowNbr, t.* FROM #T AS T
)
, CTE2
AS
(
SELECT
CTE.RowNbr,
CTE.Date,
CTE.Sold,
CTE.Purchased,
(ISNULL(CTE.Purchased,0)-ISNULL(CTE.Sold,0)) AS Balance
FROM
CTE
WHERE
CTE.RowNbr=1
UNION ALL
SELECT
CTE.RowNbr,
CTE.Date,
CTE.Sold,
CTE.Purchased,
CTE2.Balance+ISNULL(CTE.Purchased,0)-ISNULL(CTE.Sold,0) AS Balance
FROM
CTE
JOIN CTE2
ON CTE.RowNbr=CTE2.RowNbr+1
)
SELECT * FROM CTE2 ORDER BY CTE2.RowNbr DESC
Output
5 2012-05-04 11:49:45.497 1 NULL -5
4 2012-05-03 11:49:45.497 6 NULL -4
3 2012-04-29 11:49:45.497 NULL 5 2
2 2012-04-24 11:49:45.497 103 NULL -3
1 2012-04-14 11:49:45.497 NULL 100 100