My Table has a Key called KeyID, and a Foriegn Key called FKey,it has a series of tests mytest and is segmented by a type myType. It has a result of PassFail which is a bit and a date at which it occurred TimeStamp. What I want is how many FKeys passed the first time through the test. This is what I have, but it does not seem correct or proficient.
SELECT
MyType,
myTest,
Count(*) as NumberTested,
Sum(Convert(INT,(PassFail))) as PassedFirstTime
FROM MyTable MT
WHERE (TimeStamp BETWEEN @StartDate AND @EndDate) and MT.GroupID IN
--Narrow it down to the First KeyID by FKey, myTest, and myType
(Select top 1 KeyID from MyTable MT2 where MT.myType = TRG2.myType and TRG2.FKey = TRG.FKey and TRG2.myTest = TRG.myTest order by timestamp)
Group BY myType, myTest