Getting Running Totals Per Year in SQL Server - sql

I'm trying to get Running Totals for a column that dispays totals by month. I would like it to reset when a new year begins.
SELECT DATEADD(MONTH, DATEDIFF(Month, 0, ENTRY_DATE), 0) AS Payout_Month,SUM(PRINCIPAL_AMT)
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 >= '07/01/2019'
GROUP BY DATEADD(MONTH, DATEDIFF(Month, 0, ENTRY_DATE), 0)
Order BY DATEADD(MONTH, DATEDIFF(Month, 0, ENTRY_DATE), 0)
Here's what I 'd like it to display
Payout_Month Payout_Sum RollingSum
11/1/2019 15 15
12/1/2019 22 37
1/1/2020 17 17
2/1/2020 12 19
Etc. How could I create a third column that does this? Or if I can't create a third column, even if i were to get rid of Payout_Sum and just have the Rolling_sum, that would work as well.

Use window functions:
SELECT DATEADD(MONTH, DATEDIFF(Month, 0, ENTRY_DATE), 0) AS Payout_Month,
SUM(PRINCIPAL_AMT),
SUM(SUM(PRINCIPAL_AMT)) OVER (PARTITION BY YEAR(ENTRY_DATE) ORDER BY MIN(ENTRY_DATE)) as running_sum

SELECT DATEADD(MONTH, DATEDIFF(Month, 0, ENTRY_DATE), 0) AS Payout_Month,
SUM(PRINCIPAL_AMT) Payout_Sum
,RollingSum = SUM(PRINCIPAL_AMT) over (PARTITION BY DATEADD(MONTH,
DATEDIFF(Month, 0, ENTRY_DATE), 0) order by DATEADD(MONTH, DATEDIFF(Month, 0,
ENTRY_DATE), 0))
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 >= '07/01/2019'
GROUP BY DATEADD(MONTH, DATEDIFF(Month, 0, ENTRY_DATE), 0)
Order BY DATEADD(MONTH, DATEDIFF(Month, 0, ENTRY_DATE), 0)

Related

Can anyone check query I want to extract years and month columns too

Want to extract data month wise for last 2 years. Below query actually calculating 1 month data.
SELECT CAST (g1.no_of_member_cancelled AS DECIMAL(10, 2)) /
CAST (g2.no_of_live_member AS DECIMAL(10, 2)) AS x,
g1.homebranch,
g1.locationname
FROM (SELECT Count(*) AS No_of_Member_Cancelled,
M.homebranch,
M.locationname
FROM ax.memberships M
WHERE M.activeend BETWEEN Dateadd(month, Datediff(month, 0, Getdate())
- 1, 0)
AND
Dateadd(month, Datediff(month, 0,
Getdate())
, -1)
GROUP BY M.homebranch,
M.locationname) AS g1
INNER JOIN (SELECT Count(*)AS No_of_Live_Member,
M.homebranch,
M.locationname
FROM ax.memberships M
WHERE M.activestart BETWEEN Dateadd(month, Datediff(month, 0
,
Getdate()) - 1,
0) AND
Dateadd(month, Datediff(month,
0, Getdate(
)), -1)
GROUP BY M.homebranch,
M.locationname
) AS g2
ON g1.homebranch = g2.homebranch
AND g1.locationname = g2.locationname
Want to extract data month wise for last 2 years. Below query actually calculating 1 month data.

picking out data from 2 months ago only

I'm currently using this which gives me all the data from the previous month. So if i run it today in November, i get all of Octobers data.
Now i need to tweak this so it only shows data in September, and when we're in December will only show October etc
[DateTimeOfCall] between DATEADD(month, datediff(month, 0, getdate())-1, 0) and DATEADD(month, datediff(month, 0, getdate())+0, 0)
Thank you
My guess would be
[DateTimeOfCall] between DATEADD(month, datediff(month, 0, getdate())-2, 0)
and DATEADD(month, datediff(month, 0, getdate())-1, 0)
SELECT * FROM table1
WHERE mydate BETWEEN DATE_SUB(now(), INTERVAL 3 MONTH) AND now()

6 month rolling data

This query is currently pulling back 6 month data, so if I run it today I get data from 01/03/2016 to 06/09/2016. What I would like is the data to stop at 31/08/2016.
And then the following month start at 01/04/2016 to 30/09/2016 and so on.
Many thanks
select i.Date
from table as i
where i.Date >= Dateadd(Month, Datediff(Month, 0, DATEADD(m, -6, current_timestamp)), 0)
order by i.Date desc
Try :
select i.Date
from table as i
where i.Date between
Dateadd(Month, Datediff(Month, 0, DATEADD(m, -6, current_timestamp)), 0)
AND DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE())-1, 0)
order by i.Date desc

Does SQL automatically calculate month to month start and end dates?

I am trying to get total amount of sales sold in 1 month. For example, from 4/1/15 - 4/30/15.
I am getting a span of 3/30/15 - 4/30/15.
Here is my SQL:
SELECT Customer.custno
, Customer.enteredDate AS 'Date Entered'
, COUNT(BasicPolInfo.polid) AS 'Number of Policies'
, SUM(COUNT(BasicPolInfo.polid)) OVER (ORDER BY Customer.custno ROWS
BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) AS TotalAmount
FROM Customer
INNER JOIN BasicPolInfo ON Customer.custid = BasicPolInfo.custid
WHERE BasicPolInfo.polid IS NOT NULL
AND Customer.firstname IS NOT NULL
AND Customer.enteredDate BETWEEN DATEADD(MONTH, -1, GETDATE()) AND
DATEADD(MONTH, 0, GETDATE())
GROUP BY Customer.custno
, Customer.firstname
, Customer.lastname
, Customer.entereddate
ORDER BY Customer.enteredDate ASC
The results I am getting are from the dates 2015-04-30 to 2015-03-30. I am trying to get 2015-04-30 to 2015-04-01.
And for the next month to also be from 2015-05-31 to 2015-05-01
Any help would be appreciated! Thank you!
Since you are on 2012 you can use EOMONTH()
WHERE Customer.enteredDate >= DATEADD(DAY, 1, EOMONTH(GETDATE(), -1))
AND < DATEADD(DAY, 1, EOMONTH(GETDATE()))
or you could match the month and year only.
where month(Customer.enteredDate) = month(getDate())
and year(Customer.enteredDate) = year(getDate())
Customer.enteredDate >= DATEADD(MONTH, DATEDIFF(MONTH, 0, current_timestamp), 0)
AND Customer.enteredDate < DATEADD(MONTH, DATEDIFF(MONTH, 0, current_timestamp) + 1, 0)

Grouping by day from 2 tables

I have 2 tables: "orders" and "visits". in the orders table i'm saving some details about the order including "userIP" and "orderDate". in the visits table i'm saving details everytime a user visiting my web page including "userIP" and "visitDate". i'm using ASP.NET and SQL SERVER 2005.
i want to create a statistic table whitin i save the number of users visited and the number of users that ordered BOTH GROUPED BY DAY so far i got:
select count(userIP) as NumOfOrders,
dateadd(dd, datediff(dd, 0, orderDate),0) as Date
from Orders
group by dateadd(dd, datediff(dd, 0, orderDate), 0)
this works fine and give me the number of orders grouped by day,
but how do i add the total visits grouped by day to this?
I would do this:
SELECT v.userIP, NumOfVisits, NumOfOrders, v.Date
FROM (
SELECT userIP, count(*) as NumOfVisits,
dateadd(dd, datediff(dd, 0, visitDate),0) as Date
FROM visits
GROUP BY userIP, dateadd(dd, datediff(dd, 0, orderDate), 0)) v
LEFT JOIN (
SELECT userIp, count(*) as NumOfOrders,
dateadd(dd, datediff(dd, 0, orderDate),0) as Date
FROM orders
GROUP BY UserIP, dateadd(dd, datediff(dd, 0, orderDate), 0)) o
ON o.UserIP = v.UserIP
AND o.Date = v.Date
and your result should be like:
78.34.5.11 | 3 | 1 | 2009.10.06
78.34.5.19 | 9 | 0 | 2009.10.06
if you don't need to group by userIP, you can do this:
SELECT NumOfVisits, NumOfOrders, v.Date
FROM (
SELECT count(*) as NumOfVisits,
dateadd(dd, datediff(dd, 0, visitDate),0) as Date
FROM visits
GROUP BY dateadd(dd, datediff(dd, 0, visitDate), 0)) v
LEFT JOIN (
SELECT count(*) as NumOfOrders,
dateadd(dd, datediff(dd, 0, orderDate),0) as Date
FROM orders
GROUP BY dateadd(dd, datediff(dd, 0, orderDate), 0)) o
ON o.Date = v.Date
and your result will look like:
12 | 1 | 2009.10.06
SELECT COALESCE(O1.Date, V1.Date) Date,
COALESCE(O1.NumOfOrders, 0) NumOfOrders,
COALESCE(V1.TotalVisits, 0) TotalVisits
FROM
(select dateadd(dd,0, datediff(dd, 0, O.orderDate)) Date,
count(O.userIP) NumOfOrders
from Orders O
group by dateadd(dd,0, datediff(dd, 0, O.orderDate))) O1
FULL JOIN
(select dateadd(dd,0, datediff(dd, 0, V.visitDate)) Date,
count(V.userIP) TotalVisits
from Visits V
group by dateadd(dd,0, datediff(dd, 0, V.visitDate))) V1
on O1.Date = V1.Date