I solve the problem, where there is a table Move with fields MoveDate and MoveCount. The Move table is filled with approximately the following data:
MoveDate | MoveCount ________________________________ 30.07.2015 | +5 30.07.2015 | -2 31.07.2015 | +1 31.07.2015 | +9 31.07.2015 | +7 31.07.2015 | -8 31.07.2015 | +5 01.08.2015 | +9 03.08.2015 | -7 03.08.2015 | +5 03.08.2015 | -7 07.08.2015 | +2 08.08.2015 | +5
The task says that it is necessary to make samples, where to make a weekly change, even if these changes were not. For example, from 02.07 to 03.07 no changes occurred. So the balance remains the same. And to withdraw even those days in which there were no operations and changes in the number of films.
It should be like this:
MoveDate | MoveCount ________________________________ 30.07.2015 | 3 31.07.2015 | 17 01.08.2015 | 26 02.08.2015 | NULL 03.08.2015 | 17 04.08.2015 | NULL 05.08.2015 | NULL 06.08.2015 | NULL 07.08.2015 | 19 08.08.2015 | 24
I decided to start this way, I created an additional table CDate_ where a new record is created every day with the current date. Event created by trigger. The structure of this table is as follows:
CDate ________________________ id | Date_ | Count_
When I write this query:
SELECT CDate_.Date_, Sum(M.MoveCount_) AS SumMoveCount_ FROM MovementGoods AS M RIGHT JOIN CDate_ ON M.MoveDate_=CDate_.Date_ GROUP BY CDate_.Date_;
That result I get a set of dates, where changes are recorded in the balance of the quantity of goods. If they are, then there is a positive or negative balance for that day.
Date_ | SumMoveCount_ ________________________________ 29.07.2015 | 30.07.2015 | 3 31.07.2015 | 14 01.08.2015 | 9 02.08.2015 | 03.08.2015 | -9 04.08.2015 | 05.08.2015 | 06.08.2015 | 07.08.2015 | 2 08.08.2015 | -5
However, this does not give me the desired, what I would like to see. I use both standard SQL and possibly T-SQL. Therefore, I would like to ask whether it is possible to somehow do what I showed in the second table, without resorting to additional tables. I do not know all the features of T-SQL, therefore I ask. Immediately I say that it is not interesting to iterate over the cursor or to perform this task with a whole set of requests. I have done so far in the form of subqueries, but this, however, is not quite what I would like to receive. I would be grateful for the answers.