DECLARE @pilotID INT
SET @pilotID = 1
DECLARE @birthDate DATE
SET @birthDate = (SELECT birthDate FROM Pilot WHERE pilotID = @pilotID)
DECLARE @diffInYears INT
SET @diffInYears = DATEDIFF(yy, @birthDate, GETDATE())
DECLARE @currentBirthDate DATE
SET @currentBirthDate = (DATEADD(yyyy, @diffInYears - 1, @birthDate))
DECLARE @firstSixMonthStart DATE
DECLARE @firstSixMonthEnd DATE
DECLARE @secondSixMonthStart DATE
DECLARE @secondSixMonthEnd DATE
SET @firstSixMonthStart = (DATEADD(dd,-(DAY(DATEADD(m,1,@currentBirthDate))-1),DATEADD(m,1,@currentBirthDate)))
SET @firstSixMonthEnd = (DATEADD(dd, -DAY(DATEADD(m,1,@currentBirthDate)), DATEADD(m,7,@currentBirthDate)))
SET @secondSixMonthStart = (DATEADD(dd,-(DAY(DATEADD(m,1,@currentBirthDate))-1),DATEADD(m,7,@currentBirthDate)))
SET @secondSixMonthEnd = (DATEADD(dd, -DAY(DATEADD(m,1,@currentBirthDate)), DATEADD(m,13,@currentBirthDate)))
DECLARE @semiAnnualStart AS DATE
DECLARE @semiAnnualEnd AS DATE
DECLARE @annualStart AS DATE
DECLARE @annualEnd AS DATE
SET @semiAnnualStart = CASE
WHEN GETDATE() > (DATEADD(dd, -DAY(DATEADD(m,1,@firstSixMonthEnd)), DATEADD(m,7,@firstSixMonthEnd)))
THEN (DATEADD(yyyy, 1, @firstSixMonthStart))
WHEN GETDATE() > @firstSixMonthEnd
THEN @secondSixMonthStart
ELSE @firstSixMonthStart
END
SET @semiAnnualEnd = CASE
WHEN GETDATE() > (DATEADD(dd, -DAY(DATEADD(m,1,@firstSixMonthEnd)), DATEADD(m,7,@firstSixMonthEnd)))
THEN (DATEADD(yyyy, 1, @firstSixMonthEnd))
WHEN GETDATE() > @firstSixMonthEnd
THEN @secondSixMonthEnd
ELSE @firstSixMonthEnd
END
SET @annualStart = CASE
WHEN GETDATE() > @secondSixMonthEnd THEN (DATEADD(yyyy, 1, @firstSixMonthStart))
ELSE @firstSixMonthStart
END
SET @annualEnd = CASE
WHEN GETDATE() > @secondSixMonthEnd THEN (DATEADD(yyyy, 1, @secondSixMonthEnd))
ELSE @secondSixMonthEnd
END
SELECT @semiAnnualStart semiStart, @semiAnnualEnd semiEnd,
@annualStart annualStart, @annualEnd annualEnd,
@firstSixMonthStart firstStart, @firstSixMonthEnd firstEnd,
@secondSixMonthStart secondStart, @secondSixMonthEnd secondEnd,
COUNT(*) semiSorties, ISNULL(SUM(hours), 0) semiSortieHours
FROM PilotLog
WHERE pilotID = @pilotID
AND topLevelPosition = 'AVO'
AND flightDate BETWEEN @semiAnnualStart AND @semiAnnualEnd
結果:
semiStart semiEnd annualStart annualEnd firstStart firstEnd secondStart secondEnd semiSorties semiSortieHours
2012-05-01 2012-10-31 2011-11-01 2012-10-31 2011-11-01 2012-04-30 2012-05-01 2012-10-31 1 1.7
This ended up working... Problem is, I'm going to need to do this for every pilot on the overall summary page. Additionally, I'm going to need to calculate this kind of sortie information for snapshots that she wants. She wants to go back to any particular month and see a snapshot, listing each flight as well as the sortie stats they had upon completing that flight. This SQL should work for these situations, it's just I'll have to change it up a bit here and there.
(EDIT: Why doesn't my code scroll horizontally..? I don't want it to wrap. Nevermind that's just internet explorer looks like)