Rolling Sum for Last 12 Months in SQL - sql

I'm trying to get the rolling sum for the past 12 months (Oct 2019-Sept 2020, etc.)> So far, I figured out to get the current year total (which I also want), however, I'm stuck on a legit 12 month rolling sum.
SELECT DATEADD(MONTH, DATEDIFF(Month, 0, ENTRY_DATE), 0) AS Payout_Month, SUM(PRINCIPAL_AMT) Payout_amt,
SUM(SUM(PRINCIPAL_AMT)) OVER (PARTITION BY YEAR(ENTRY_DATE) ORDER BY MIN(ENTRY_DATE)) as YearRollingSum,
SUM(SUM(PRINCIPAL_AMT)) OVER (PARTITION BY Year(ENTRY_DATE)
ORDER BY MIN(ENTRY_DATE)
ROWS BETWEEN 12 PRECEDING AND 1 PRECEDING
) AS TwelveMonthRollingSum
FROM ACCOUNTHISTORY
WHERE LEFT(TOKEN_STRING, 4) LIKE '%Py%'
AND FOCUS_TELLER_ID = 6056
AND PRINCIPAL_AMT > 0 AND PRINCIPAL_AMT < 25
AND ENTRY_DATE >= '01/01/2019'
GROUP BY DATEADD(MONTH, DATEDIFF(Month, 0, ENTRY_DATE), 0), YEAR(ENTRY_DATE)
Order BY DATEADD(MONTH, DATEDIFF(Month, 0, ENTRY_DATE), 0)
here's what my current output looks like
Payout_Month Payout_amt YearRollingSum TwelveMonthRollingSum
2019-01-01 00:00:00.000 5696.50 5696.50 NULL
2019-02-01 00:00:00.000 11205.60 16902.10 5696.50
2019-03-01 00:00:00.000 23341.50 40243.60 16902.10
2019-04-01 00:00:00.000 25592.80 65836.40 40243.60
2019-05-01 00:00:00.000 28148.30 93984.70 65836.40
2019-06-01 00:00:00.000 27190.90 121175.60 93984.70
2019-07-01 00:00:00.000 25079.80 146255.40 121175.60
2019-08-01 00:00:00.000 30206.90 176462.30 146255.40
2019-09-01 00:00:00.000 28000.80 204463.10 176462.30
2019-10-01 00:00:00.000 29076.60 233539.70 204463.10
2019-11-01 00:00:00.000 29001.30 262541.00 233539.70
2019-12-01 00:00:00.000 28366.00 290907.00 262541.00
2020-01-01 00:00:00.000 32062.40 32062.40 NULL
2020-02-01 00:00:00.000 28526.70 60589.10 32062.40
2020-03-01 00:00:00.000 29056.50 89645.60 60589.10
2020-04-01 00:00:00.000 28016.00 117661.60 89645.60
2020-05-01 00:00:00.000 25173.30 142834.90 117661.60
2020-06-01 00:00:00.000 27646.10 170481.00 142834.90
2020-07-01 00:00:00.000 36083.70 206564.70 170481.00
2020-08-01 00:00:00.000 34872.20 241436.90 206564.70
2020-09-01 00:00:00.000 35727.10 277164.00 241436.90
2020-10-01 00:00:00.000 34030.80 311194.80 277164.00
AS you can see, it resets at the beginning of the year for the last column. Any ideas?

Basically, you want to remove the partition by clause from the rolling 12 month sum. I would also suggest a few optimizations to the query:
select
x.payout_month,
sum(ah.principal_amt) payout_amt,
sum(sum(ah.principal_amt)) over (
partition by year(x.payout_month)
order by x.payout_month
) as yearrollingsum,
sum(sum(ah.principal_amt)) over (
order by x.payout_month
rows between 12 preceding and 1 preceding
) as twelvemonthrollingsum
from accounthistory ah
cross apply (values (datefromparts(year(ah.entry_date), month(entry_date), 1))) x(ah.payout_month)
where
left(ah.token_string, 4) like '%py%'
and ah.focus_teller_id = 6056
and ah.principal_amt > 0 and principal_amt < 25
and ah.entry_date >= '20190101'
group by x.payout_month
order by x.payout_month
The main change is that the payout_month is computed only once, in a lateral join, using datefromparts(). You can then use it all over the query, and consistently in the order by clauses of the window functions.
Note that your strategy will fail to produce a proper results if you ever have a month without any sale (the rows clause of the window function will spread over the preceding month, which is not what you want). If that's something that may happen, then an alternative is a subquery, or another lateral join.

Related

Get quarter start/end dates for more than a year (start year to current year)

I've been trying to get start and end dates range for each quarter given a specific date/year, like this:
SELECT DATEADD(mm, (quarter - 1) * 3, year_date) StartDate,
DATEADD(dd, 0, DATEADD(mm, quarter * 3, year_date)) EndDate
--quarter QuarterNo
FROM
(
SELECT '2012-01-01' year_date
) s CROSS JOIN
(
SELECT 1 quarter UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 4
) q
which produces the following output:
2012-01-01 00:00:00 2012-04-01 00:00:00
2012-04-01 00:00:00 2012-07-01 00:00:00
2012-07-01 00:00:00 2012-10-01 00:00:00
2012-10-01 00:00:00 2013-01-01 00:00:00
Problem: I need to do this for a given start_date and end_date, the problem being the end_date=current_day, so how can I achieve this:
2012-01-01 00:00:00 2012-04-01 00:00:00
2012-04-01 00:00:00 2012-07-01 00:00:00
2012-07-01 00:00:00 2012-10-01 00:00:00
2012-10-01 00:00:00 2013-01-01 00:00:00
... ...
2021-01-01 00:00:00 2021-01-06 00:00:00
I think here is what you want to do :
SET startdatevar AS DATEtime = '2020-01-10'
;WITH RECURSIVE cte AS (
SELECT startdatevar AS startdate , DATEADD(QUARTER, 1 , startdatevar) enddate , 1 quarter
UNION ALL
SELECT enddate , CASE WHEN DATEADD(QUARTER, 1 , enddate) > CURRENT_DATE() THEN GETDATE() ELSE DATEADD(QUARTER, 1 , enddate) END enddate, quarter + 1
FROM cte
WHERE
cte.enddate <= CURRENT_DATE()
and quarter < 4
)
SELECT * FROM cte
to use your code , if you want to have more than 4 quarters :
SET quarter_limit = DATEDIFF(quarter , <startdate>,<enddate>)
;WITH RECURSIVE cte(q, qDate,enddate) as
(
select 1,
DATEFROMPARTS(year('2012-01-01'::date), 1, 1) -- First quarter date
,time_slice('2012-01-01'::date, 3, 'MONTH', 'END')
UNION ALL
select q+1,
DATEADD(q, 1, qdate) -- next quarter start date
,time_slice(qdate::date, (q+1)*3, 'MONTH', 'END')
from cte
where q < quarter_limit -- limiting the number of next quarters
AND cte.endDate <= <enddate>
)
SELECT * FROM cte
After #eshirvana's answer, I came up with this slightly change after your answer:
WITH RECURSIVE cte(q, qDate,enddate) as
(
select 1,
DATEFROMPARTS(year('2012-01-01'::date), 1, 1) -- First quarter date
,time_slice('2012-01-01'::date, 3, 'MONTH', 'END')
UNION ALL
select q+1,
DATEADD(q, 1, qdate) -- next quarter start date
,time_slice(qdate::date, (q+1)*3, 'MONTH', 'END')
from cte
where q <4 -- limiting the number of next quarters
AND cte.endDate <= CURRENT_DATE()
)
SELECT * FROM cte
Which works fine for whatever year I pass there (2012 will produce 4 records, 2021 just one, since we're still on the first quarter right now).
[EDIT]: it still doesn't work as expected after your 2nd code sugestion:
WITH RECURSIVE cte(q, qDate,enddate) as
(
select 1,
DATEFROMPARTS(year('2012-01-01'::date), 1, 1) -- First quarter date
,CASE WHEN time_slice('2012-01-01'::date, 3, 'MONTH', 'END') > CURRENT_DATE
THEN current_date
ELSE time_slice('2012-01-01'::date, 3, 'MONTH', 'END')
END
UNION ALL
select q+1,
DATEADD(q, 1, qdate) -- next quarter start date
,time_slice(qdate::date, (q+1)*3, 'MONTH', 'END')
from cte
where q < DATEDIFF(quarter , '2012-01-01'::date,'2021-01-06'::date)
AND cte.endDate <= '2021-01-06'::date
)
SELECT * FROM cte
is outputing this:
Sorry #eshirvana, it doesn't work as expected though. It all goes well to some point, but it's not returning all the records. Instead, it produces less records and wrong one, like this:
1 2012-01-01 2012-04-01
2 2012-04-01 2012-07-01
3 2012-07-01 2012-10-01
4 2012-10-01 2013-01-01
5 2013-01-01 2013-10-01
6 2013-04-01 2013-07-01
7 2013-07-01 2013-10-01
8 2013-10-01 2014-01-01
9 2014-01-01 2015-01-01
10 2014-04-01 2015-01-01
11 2014-07-01 2016-10-01
12 2014-10-01 2015-01-01
13 2015-01-01 2015-07-01
14 2015-04-01 2015-07-01
15 2015-07-01 2018-10-01
16 2015-10-01 2018-01-01
17 2016-01-01 2016-10-01
18 2016-04-01 2019-07-01
19 2016-07-01 2017-07-01
20 2016-10-01 2020-01-01
21 2017-01-01 2017-04-01
22 2017-04-01 2019-07-01
23 2017-07-01 2021-10-01
Although my logic it's still not ok for not printing just Q1 dates for 2021, could this output issues be related to date format or something?
Now, it seems to be working, at least for 2012-01-01 till today (2021-01-06).
The code :
WITH RECURSIVE cte(q, qDate,enddate) as
(
select
-- it might not be the first quarter, so better to protect that:
quarter('2012-01-01'::date)::numeric
, DATEFROMPARTS(year('2012-01-01'::date), 1, 1) -- First quarter date
, CASE WHEN time_slice('2012-01-01'::date, 3, 'MONTH', 'END') > '2021-01-06'::date
THEN '2021-01-06'::date
ELSE time_slice('2012-01-01'::date, 3, 'MONTH', 'END')
END
UNION ALL
select q+1
, DATEADD(q, 1, qdate) -- next quarter start date
,CASE WHEN time_slice(DATEADD(q, 1, qdate), 3, 'MONTH', 'END')> '2021-01-06'::date
THEN '2021-01-06'::date
ELSE time_slice(DATEADD(q, 1, qdate), 3, 'MONTH', 'END')
END
from cte
where q <= DATEDIFF(quarter , '2012-01-01'::date,'2021-01-06'::date)
AND cte.endDate <= '2021-01-06'::date
)
SELECT * FROM cte
The output:
1 2012-01-01 2012-04-01
2 2012-04-01 2012-07-01
3 2012-07-01 2012-10-01
4 2012-10-01 2013-01-01
5 2013-01-01 2013-04-01
6 2013-04-01 2013-07-01
7 2013-07-01 2013-10-01
8 2013-10-01 2014-01-01
9 2014-01-01 2014-04-01
10 2014-04-01 2014-07-01
11 2014-07-01 2014-10-01
12 2014-10-01 2015-01-01
13 2015-01-01 2015-04-01
14 2015-04-01 2015-07-01
15 2015-07-01 2015-10-01
16 2015-10-01 2016-01-01
17 2016-01-01 2016-04-01
18 2016-04-01 2016-07-01
19 2016-07-01 2016-10-01
20 2016-10-01 2017-01-01
21 2017-01-01 2017-04-01
22 2017-04-01 2017-07-01
23 2017-07-01 2017-10-01
24 2017-10-01 2018-01-01
25 2018-01-01 2018-04-01
26 2018-04-01 2018-07-01
27 2018-07-01 2018-10-01
28 2018-10-01 2019-01-01
29 2019-01-01 2019-04-01
30 2019-04-01 2019-07-01
31 2019-07-01 2019-10-01
32 2019-10-01 2020-01-01
33 2020-01-01 2020-04-01
34 2020-04-01 2020-07-01
35 2020-07-01 2020-10-01
36 2020-10-01 2021-01-01
37 2021-01-01 2021-01-06
In case you're wondering: yes, the idea is to present the end_date as last_day of the month+one. But it could easily be adapted.
It's not pretty, but I think it's somehow easy to understand.

Is there a way to turn the results of a SELECT, FROM, GROUPBY statement into a new table?

Basically I need to put some tables from SQL into Excel to analyze it with a third-party program. However, when using Excel, the PivotTable date columns are in a specific format (d mm) and for the third-party program it will not accept it.
However, in SQL where I have done some aggregations on the raw data, the format that I have used for the aggregation statement is absolutely correct and I would like to turn those results into a separate table so that I can feed it to the external program. I will have an example of what I mean below.
This is the resulting view of what the SQL aggregation statement on the data looks like.
Time TADCount TenantId
2019-11-22 00:00:00.000 1 4754F795-2FB9-4647-B28F-2CF2412F0BA2
2019-11-29 00:00:00.000 1 4754F795-2FB9-4647-B28F-2CF2412F0BA2
2019-11-30 00:00:00.000 1 4754F795-2FB9-4647-B28F-2CF2412F0BA2
2019-12-07 00:00:00.000 11 4754F795-2FB9-4647-B28F-2CF2412F0BA2
2019-12-08 00:00:00.000 2 4754F795-2FB9-4647-B28F-2CF2412F0BA2
2019-11-23 00:00:00.000 1 FD85A7E0-BDF5-4A6E-9B69-34AF1A518CB0
2019-11-27 00:00:00.000 1 FD85A7E0-BDF5-4A6E-9B69-34AF1A518CB0
2019-11-28 00:00:00.000 1 FD85A7E0-BDF5-4A6E-9B69-34AF1A518CB0
2019-12-02 00:00:00.000 1 FD85A7E0-BDF5-4A6E-9B69-34AF1A518CB0
2019-12-04 00:00:00.000 1 FD85A7E0-BDF5-4A6E-9B69-34AF1A518CB0
2019-12-06 00:00:00.000 3 FD85A7E0-BDF5-4A6E-9B69-34AF1A518CB0
2019-11-24 00:00:00.000 1 1CB49648-420B-454A-B39D-4DAB361C18B8
2019-11-24 00:00:00.000 5 0E84F35A-D7C1-4BDE-B805-6C7691B9667F
2019-11-26 00:00:00.000 1 0E84F35A-D7C1-4BDE-B805-6C7691B9667F
2019-12-05 00:00:00.000 1 0E84F35A-D7C1-4BDE-B805-6C7691B9667F
2019-12-07 00:00:00.000 2 0E84F35A-D7C1-4BDE-B805-6C7691B9667F
For reference, this is the aggregation statement that I have made.
SELECT DATEADD(DAY, DATEDIFF(DAY, 0, [Time]), 0) AS Time
, Count([TenantId]) AS TADCount,
[TenantId]
FROM [dbo].[acms_data]
GROUP by DATEADD(DAY, DATEDIFF(DAY, 0, [Time]), 0),[TenantId]
Basically I would like to turn that view above the code statement into a table with exactly those values and headers into a separate table that can be interacted with.
SELECT INTO is what you want to use here. Assuming you have the permissions to create a new table the steps would be:
Create the table you want to insert the data in
Convert the SELECT query into a SELECT INTO query.
Here's an example from W3 Schools I've modified for your use case that should work. The main thing is that the new_table should already be created then modify your select query by adding an INTO:
SELECT DATEADD(DAY, DATEDIFF(DAY, 0, [Time]), 0) AS Time
, Count([TenantId]) AS TADCount,
[TenantId]
INTO [dbo].[new_table]
FROM [dbo].[acms_data]
GROUP by DATEADD(DAY, DATEDIFF(DAY, 0, [Time]), 0),[TenantId]

selecting a date n days ago excluding weekends

I have a table with daily dates starting from 31st December 1999 up to 31st December 2050, excluding weekends.
Say given a particular date, for this example lets use 2019-03-14. I want to pick the date that was 30 days previous (the number of days needs to be flexible as it won't always be 30), ignoring weekends which in this case would be 2019-02-01.
How to do this?
I wrote the query below & it indeed lists 30 days previous to the specified date.
select top 30 Date
from DateDimension
where IsWeekend = 0 and Date <= '2019-03-14'
order by Date desc
So I thought I could use the query below to get the correct answer of 2019-02-01
;with ds as
(
select top 30 Date
from DateDimension
where IsWeekend = 0 and Date <= '2019-03-14'
)
select min(Date) from ds
However this doesn't work. It returns me the first date in my table, 1999-12-31.
2019-03-14
2019-03-13
2019-03-12
2019-03-11
2019-03-08
2019-03-07
2019-03-06
2019-03-05
2019-03-04
2019-03-01
2019-02-28
2019-02-27
2019-02-26
2019-02-25
2019-02-22
2019-02-21
2019-02-20
2019-02-19
2019-02-18
2019-02-15
2019-02-14
2019-02-13
2019-02-12
2019-02-11
2019-02-08
2019-02-07
2019-02-06
2019-02-05
2019-02-04
2019-02-01
TOP is meaningless without an ORDER BY, so you could do something like
;with ds as
(
select top 30 Date
from DateDimension
where IsWeekend = 0 and Date <= '2019-03-14'
order by Date DESC
)
select min(Date) from ds;
even better would be to use the ANSI syntax instead of TOP:
select Date
from DateDimension
where IsWeekend = 0 and Date <= '2019-03-14'
order by Date DESC
OFFSET 30 ROWS FETCH NEXT 1 ROW ONLY;
DISCLAIMER - code not tested since you did not provide DDL and sample data
HTH

Compare values for consecutive dates of same month

I have a table
ID Value Date
1 10 2017-10-02 02:50:04.480
2 20 2017-10-01 07:28:53.593
3 30 2017-09-30 23:59:59.000
4 40 2017-09-30 23:59:59.000
5 50 2017-09-30 02:36:07.520
I compare Value with previous date. But, I don't need compare result between first day in current month and last day in previous month. For this table, I don't need to compare result between 2017-10-01 07:28:53.593 and 2017-09-30 23:59:59.000 How it can be done?
Result table for this example:
ID Value Date Diff
1 10 2017-10-02 02:50:04.480 10
2 20 2017-10-01 07:28:53.593 NULL
3 30 2017-09-30 23:59:59.000 10
4 40 2017-09-29 23:59:59.000 10
5 50 2017-09-28 02:36:07.520 NULL
You can use this.
SELECT * ,
LEAD(Value) OVER( PARTITION BY DATEPART(YEAR,[Date]), DATEPART(MONTH,[Date]) ORDER BY ID ) - Value AS Diff
FROM MyTable
ORDER BY ID
you can use a query like below
select *,
diff=LEAD(Value) OVER( PARTITION BY Month(Date),Year(Date) ORDER BY Date desc)-Value
from t
order by id asc
see working demo

View Missing Numbers in a Sequence by date

I have a table name "Numerator" has 3 columns: No_Fact, Numerat and Date. Like this:
No_Fact Numerat Date
1207020015 000713 2012-07-02 00:00:00.000
1207020016 000720 2012-07-02 00:00:00.000
1207020017 000722 2012-07-02 00:00:00.000
1207020019 000723 2012-07-02 00:00:00.000
1207020022 000724 2012-07-02 00:00:00.000
1207020021 000733 2012-07-02 00:00:00.000
1207020020 000734 2012-07-02 00:00:00.000
1207020018 000735 2012-07-02 00:00:00.000
1208120001 000766 2012-08-12 00:00:00.000
1208120002 000769 2012-08-12 00:00:00.000
1208230001 000777 2012-08-23 00:00:00.000
1208230002 000780 2012-08-23 00:00:00.000
I want to view some missing numbers between numbers inside Numerat column, so I have tried using the code that is:
SELECT A.Numerat+1 AS 'MISSING FROM', MIN(B.Numerat)-1 AS 'TO'
FROM Numerator A, Numerator B
WHERE A.Numerat < B.Numerat
GROUP BY A.Numerat
HAVING A.Numerat+1 < MIN(B.Numerat)
ORDER BY 1
And result set:
MISSING FROM TO
714 719
721 721
725 732
736 765
767 768
770 776
778 779
This is a code that nice.
But the sequence numbers is too many, to be incompatible with the need to be concise. So I only want to show part rows today (2012-08-23) only, likes this:
MISSING FROM TO
778 779
I am very grateful to anyone who can decode it, Thank You.
There's an interesting thing when the next line in the insert in the table,
I show began on August 23 2012:
No_Fact Numerat Date
1208230001 000777 2012-08-23 00:00:00.000
1208230002 000780 2012-08-23 00:00:00.000
1208250001 000782 2012-08-25 00:00:00.000
1208250002 000783 2012-08-25 00:00:00.000
1208250003 000784 2012-08-25 00:00:00.000
1208250004 000785 2012-08-25 00:00:00.000
1208250005 000786 2012-08-25 00:00:00.000
1208250006 000788 2012-08-25 00:00:00.000
1208250007 000789 2012-08-25 00:00:00.000
1208250008 000790 2012-08-25 00:00:00.000
1208250009 000793 2012-08-25 00:00:00.000
1208250010 000794 2012-08-25 00:00:00.000
As my original purpose, that I want to see the missing numbers in the Numerat column and to summarily I only just wanted to show a line I enter this day alone, then with advice from bluefeet I use the following code:
SELECT A.Numerat+1 AS 'MISSING FROM', MIN(B.Numerat)-1 AS 'TO'
FROM Numerator A
INNER JOIN Numerator B
ON A.Numerat < B.Numerat
AND a.Tanggal = DATEADD(DAY, DATEDIFF(DAY, 0, GetDate()), 0)
GROUP BY A.Numerat
HAVING A.Numerat+1 < MIN(B.Numerat)
ORDER BY 1
and achieving results:
MISSING FROM TO
787 787
791 792
If we look carefully there is a missing number that does not appear, that is:
MISSING FROM TO
781 781
But we do not know exactly, including the date do it? Is 23 or 25. In this case it is not important to know exactly falls on what the date this missing number, obviously there is one number missing, it should be noted, and due on the 24th of all public holidays, let us enter it on the 25th. Can anyone help?
Here are a few different ways to do this query, all have incorporated the date into it:
SELECT A.Numerat+1 AS 'MISSING FROM', MIN(B.Numerat)-1 AS 'TO'
FROM test A
INNER JOIN test B
ON A.Numerat < B.Numerat
AND a.dt = DATEADD(DAY, DATEDIFF(DAY, 0, GetDate()), 0)
GROUP BY A.Numerat
HAVING A.Numerat+1 < MIN(B.Numerat)
ORDER BY 1
See SQL Fiddle with Demo
Or
;with cte as
(
select numerat, dt,
row_number() over(partition by dt order by numerat) rn
from test
where dt = DATEADD(DAY, DATEDIFF(DAY, 0, GetDate()), 0)
)
select c.Numerat+1 MissingFrom, t.Numerat-1 [To]
from cte c
inner join test t
on c.Numerat < t.Numerat
where c.rn = 1
See SQL Fiddle with Demo
Or you can use a MAX() aggregate with no date:
SELECT max([Missing From]), max([to])
FROM
(
SELECT A.Numerat+1 AS 'MISSING FROM', MIN(B.Numerat)-1 AS 'TO'
FROM test A
INNER JOIN test B
ON A.Numerat < B.Numerat
GROUP BY A.Numerat
HAVING A.Numerat+1 < MIN(B.Numerat)
) x
See SQL Fiddle with Demo