I’m working in Microsoft SQL Server 2005 and need to do an analysis of players who qualify for our 50+ tournament on Tuesdays –> To qualify, they need to get 5+ points on Mon or Tues that week before 2pm, and the points are not cumulative (must get a min of 5 on Mon or on Tues before 2pm). The code I am using is below but so far only contains a date range for a specific Monday and Tuesday. I need all Mondays and Tuesdays over a 2 month timeframe. I am new to this (kinda thrown in my lap as an extra duty so I am having to learn on the fly)…
So far, as I stated, I have been able to succeed on my own in getting all the information I need but it is only specific to 1 week. Everything I have found online so far has been more confusing than I can explain and nobody has referenced the code I provided, instead giving me completely new code that I cannot use…
SELECT
dbo.CombinedPts.Account, dbo.CombinedPts.FirstName,
dbo.CombinedPts.LastName,
ISNULL(dbo.CombinedPts.EGSPts, 0) AS EGS,
ISNULL(dbo.CombinedPts.IRPts, 0) AS IR,
DATENAME(dw, dbo.CombinedPts.Date) AS WkDay,
DATEDIFF(YY, dbo.PlayerMaster.Birthdate, {fn current_date()}) - CASE WHEN (MONTH(dbo.PlayerMaster.Birthdate)=MONTH({fn current_date()}) AND DAY(dbo.PlayerMaster.Birthdate) > DAY({fn current_date()}) OR MONTH (dbo.PlayerMaster.Birthdate) > MONTH ({fn current_date()})) THEN 1 ELSE 0 END AS Age
FROM
dbo.CombinedPts, dbo.PlayerMaster
WHERE
(DATEDIFF(YY, dbo.PlayerMaster.Birthdate, {fn current_date()}) >= 50)
AND (dbo.CombinedPts.EGSPts >= 5 OR dbo.CombinedPts.IRPts >= 5)
AND (dbo.CombinedPts.Account = dbo.PlayerMaster.Account)
AND (Date BETWEEN '10/11/2010 00:00:00' AND '10/11/2010 13:59:59' OR Date BETWEEN '10/12/2010 00:00:00' AND '10/12/2010 13:59:59')
AND (DATEPART(dw, dbo.CombinedPts.Date) = 2 OR DATEPART(dw, dbo.CombinedPts.Date) = 3)
GROUP BY
dbo.CombinedPts.Account,
dbo.CombinedPts.FirstName,
dbo.CombinedPts.LastName,
DATENAME(dw, dbo.CombinedPts.Date),
dbo.CombinedPts.EGSPts,
dbo.CombinedPts.IRPts,
dbo.PlayerMaster.Birthdate
ORDER BY
dbo.CombinedPts.Account
As a side note, in our SQL Server 2005, I do not have permissions to create subqueries (I have become an avid user of UNION ALL –> which has proved useless in the query so far) nor do I have permission to generate tables, so everything I do has to be in one query… And thus far, all help that has made some sense has been using tables and subqueries.
It’s a little different structure, but mostly the same. I used aliases instead of full references. It’s also untested, but parses cleanly.
The
AND DATEDIFF(m,cp.Date,GETDATE()) < 2gives you the 2 month window.EDIT – Added the clauses for the Day and Time.
AND (DATEPART(dw, cp.Date) in (2,3) AND DATEPART(hh,cp.Date) > 14)gives you only Monday and Tuesday after 2:00pm (1400 hours)