SQL show monh to date numbers


I use the following SQL statement to get the daily drill totals
 Sum([Performance].[Drilled Total]) AS Metres
I now have to get the month to date totals based on the [Start Date]

SELECT [Performance].[Date] As [Date],[Performance].[Shaft], Sum([Performance].[Drilled Total]) AS Metres,[vShafts_StartDate].[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([vShafts_StartDate].[StartDate] As Date) As [Month_Start], [vShafts_StartDate].[Target] as newTarget 
FROM [Performance] Inner Join [vShafts_StartDate] On [Performance].[Shaft] = [vShafts_StartDate].[Shaft]
Where [Date] >= [vShafts_StartDate].[StartDate] And [Date] <= '11 Mar 2016'And [Performance].[Shaft] = 'kopanang air'
GROUP BY [Performance].[Date],[Performance].Shaft,[vShafts_StartDate].[StartDate],[vShafts_StartDate].[Target]

Open in new window

Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?
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.

Brian CroweDatabase AdministratorCommented:
SELECT [Performance].[Date] As [Date],
	SUM([Performance].[Drilled Total]) AS Metres,
	[vShafts_StartDate].[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([vShafts_StartDate].[StartDate] As Date) As [Month_Start],
	[vShafts_StartDate].[Target] as newTarget,
	SUM(CASE WHEN YEAR([Performance].[Date]) = YEAR(GETDATE()) AND MONTH([Performance].[Date]) = MONTH(GETDATE()) THEN [Performance].[Drilled Total] ELSE 0 END) AS MTDDrillTodal
FROM [Performance]
INNER JOIN [vShafts_StartDate]
	ON [Performance].[Shaft] = [vShafts_StartDate].[Shaft]
WHERE [Performance].[Date] >= [vShafts_StartDate].[StartDate]
	AND [Performance].[Date] <= CAST(GETDATE() AS DATE)
	AND [Performance].[Shaft] = 'kopanang air'
GROUP BY [Performance].[Date], [Performance].Shaft, [vShafts_StartDate].[StartDate], [vShafts_StartDate].[Target]

Open in new window

Jim HornSQL Server Data DudeCommented:
Just to make sure we're not missing anything, give us a data mockup of both your current data and your desired return set.
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Here is an example of the  two columns that I want to get a cumulative total on. I have also attached a spreadsheet showing what all columns would look like

Cumulative TotalsBook2.xlsx
Scott PletcherSenior DBACommented:
SELECT [P].[Date] As [Date],[P].[Shaft],
    Cast(SS.[StartDate] As Date) As [Month_Start], SS.[Target] as newTarget,
    Sum([P].[Drilled Total]) AS Metres,SS.[Target] * Count(DISTINCT [P].[Date]) As Target, Avg([P].[Drilled Total]) As [Ave M/Mach/Shift], Sum([P].[Delay Hours 1]) AS Delays, Sum([P].[DWR Hours]) AS DWR, Sum([P].[Standing Time]) AS [Standing Time], Sum([P].[Grout Hours]) AS [Grout Hours], Sum([P].[Concrete Redrill]) AS ReDrill, Sum([P].Setup) AS Setups, Sum([P].[Transport Hours]) as [Transport Hours], Sum([P].[Photographic Survey]) as [Photographic Survey], Sum([P].[Day Rate]) as [Day Rate], Sum([P].[Blank Cap]) as [Blank Cap], Sum([P].[Mobilise]) as [Mobilise], Sum([P].[Set Up Stope]) as [Set Up Stope], Sum([P].[Interhole Move]) as [Interhole Move], Sum([P].[Grouting Borehole]) as [Grouting Borehole], Sum([P].[Plugging]) as [Plugging], Sum([P].[Trp Equip UG]) as [Trp Equip UG]
FROM [Performance] P
Inner Join [vShafts_StartDate] SS On [P].[Shaft] = [SS].[Shaft]
Where [P].[Date] >= [SS].[StartDate] And
      /*? SS.[StartDate] >= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0) And ?*/
      [P].[Date] >= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0) And
      [P].[Date] <  DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + 1, 0) And
      [P].[Shaft] = 'kopanang air'
GROUP BY [P].[Date],[P].Shaft,Cast([SS].[StartDate] As Date),[SS].[Target]

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
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thanks very much
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.