SQL Count the unique dates

In the following SQL statement I have to count the number of unique dates. In the first column I am using Count([Performance].[Date])
but the same date is entered multiple times by the people who update the data. How do I get the count of unique dates?

		SELECT Count([Performance].[Date]) as Num, [Performance].[Shaft], Sum([Performance].[Drilled Total]) AS Metres,[Shafts].[Target] * Count([Performance].[Date]) As Target, Sum([Performance].[Drilled Total])/Count([Performance].[Date]) As [Ave M/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] <= '06 Nov 2015' GROUP BY [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.

Vitor MontalvãoMSSQL Senior EngineerCommented:
You can use the COUNT(DISTINCT date_colum_name) to only count unique dates.

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
Russell FoxDatabase DeveloperCommented:
	-- Cast to DATE to ensure it isn't a DATETIME:
	CAST([Performance].[Date] AS DATE) AS PerformanceDate,
	COUNT(DISTINCT CAST([Performance].[Date] AS DATE)) as Num, 
	SUM([Performance].[Drilled Total]) AS Metres,
	[Shafts].[Target] * Count([Performance].[Date]) As [Target], 
		-- To avoid divide-by-zero error:
		CASE WHEN COUNT(DISTINCT CAST([Performance].[Date] AS DATE)) = 0 
			THEN 0
			ELSE SUM([Performance].[Drilled Total])/COUNT(DISTINCT CAST([Performance].[Date] AS DATE)) 
	As [Ave M/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 CAST([Performance].[Date] AS DATE) >= [Shafts].[From] 
	AND CAST([Performance].[Date] AS DATE) <= '06 Nov 2015' 
GROUP BY [Performance].Shaft,

Open in new window

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.