SQL Month to Date

How do I convert the following SQL statement to show month to date numbers in the "Metres" column
I want to show all dates with the month to date number for each of them

SELECT [Performance].[Date] As [Date],[Performance].[Shaft], Sum([Performance].[Drilled Total]) AS Metres,[Shafts].[Target] * Count(DISTINCT [Performance].[Date]) As Target, Avg([Performance].[Drilled Total]) As [Ave M/Mach/Shift], Sum([Performance].[Delay Hours 1]) AS Delays, Sum([Performance].[DWR Hours]) AS DWR, Sum([Performance].[Standing Time]) AS [Standing Time], Sum([Performance].[Grout Hours]) AS [Grout Hours], Sum([Performance].[Concrete Redrill]) AS ReDrill, Sum([Performance].Setup) AS Setups, Sum([Performance].[Transport Hours]) as [Transport Hours], Sum([Performance].[Photographic Survey]) as [Photographic Survey], Sum([Performance].[Day Rate]) as [Day Rate], Sum([Performance].[Blank Cap]) as [Blank Cap], Sum([Performance].[Mobilise]) as [Mobilise], Sum([Performance].[Set Up Stope]) as [Set Up Stope], Sum([Performance].[Interhole Move]) as [Interhole Move], Sum([Performance].[Grouting Borehole]) as [Grouting Borehole], Sum([Performance].[Plugging]) as [Plugging], Sum([Performance].[Trp Equip UG]) as [Trp Equip UG], Cast([Shafts].[From] As Date) As [Month_Start]

 FROM [Performance] Inner Join [Shafts] On [Performance].[Shaft] = [Shafts].[Shaft] Where [Date] >= [Shafts].[From] And [Date] <= '07 Nov 2015'And [Performance].[Shaft] = 'richard air' 

GROUP BY [Performance].[Date],[Performance].Shaft,[Shafts].[From],[Shafts].[Target]

Open in new window

Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Olaf DoschkeSoftware DeveloperCommented:
What do you mean with month to date? Do you mean todays or current month?
The metres colum is Sum([Performance].[Drilled Total]), I don't know how that relates to dates or moths.

Do you want data grouped by months instead of per day? Then GROUP BY [Performance].[Date] must be changed to a month value, eg GROUP BY datediff(Month,0,[Performance].[Date])

The start date an be anything, important is the expression is the same per month.

But most probably I misundeerstand your overall problem.
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
A more simple way to describe it would be:
I want each Date to show the total from the Start_date
So if 5m is drilled over three dates then you would see

10-May-2015  5
11-May-2015  10
12-May-2014  15
Olaf DoschkeSoftware DeveloperCommented:
So you want a running sum instead of sum per date.

Then what you need is an OVER clause:

SUM([Performance].[Drilled Total]) OVER (ORDER BY something) AS [Running Total]

If you want this per month you need to PARTITION this by month, eg

SUM([Performance].[Drilled Total]) OVER (PARTITION BY datediff(Month,0,[Performance].[Date]) ORDER BY something) AS [Running Total], but you can't have larger partitions than groups and since you group by date that would lead to a sum per date anyway. Maybe you want to take away the grouping in that aspect.

Bye, Olaf.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Olaf DoschkeSoftware DeveloperCommented:
As an example (If you have adventureworks data installed):

Select s.SalesOrderID, s.SalesOrderDetailID,
Sum(s.LineTotal) over (partition by s.SalesOrderID order by s.SalesOrderDetailID) as [running order total]
from AdventureWorks2012.Sales.SalesOrderDetail s

Open in new window

This sums the linetotal of orderdetails as running sum, reset to 0 per order by the partitioning.

Bye, Olaf.
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Great answer. Thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.