SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Top 1
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

chipembele
Yak Posting Veteran

97 Posts

Posted - 08/09/2012 :  07:34:09  Show Profile  Reply with Quote
Hi
I'm trying to make my query return only the first instance of when they received a specific mark against them.

For example, I have a list of people with the specific mark and the dates and every record shows as follows

Ref No Name Mark Date
111111 Bob W 20/02/2012
111111 Bob W 05/04/2012
111111 Bob W 01/05/2012
111111 Bob W 20/06/2012
111111 Bob W 28/06/2012

I've tried TOP 1 but it just returns the first person.

This is my current SQL


SELECT     TOP (100) PERCENT RegisterSession.Date, StudentDetail.AcademicYearID, StudentDetail.RefNo, 
                      StudentDetail.FirstForename, StudentDetail.Surname, Offering.OfferingID, 
                      Offering.Code, Offering.Name, Register.RegisterID, Register.RegisterNo, 
                      Register.Title, MarkType.Mark, MarkType.Description, 
                      Enrolment.CompletionStatusID, Enrolment.StartDate, Enrolment.ExpectedEndDate, 
                      Enrolment.ActualEndDate, RegisterSession.Date AS Expr1
FROM         StudentDetail WITH (NOLOCK) INNER JOIN
                      Enrolment WITH (NOLOCK) ON Enrolment.StudentDetailID = StudentDetail.StudentDetailID INNER JOIN
                      Offering WITH (NOLOCK) ON Enrolment.OfferingID = Offering.OfferingID INNER JOIN
                      RegisterStudent WITH (NOLOCK) ON 
                      Enrolment.EnrolmentID = RegisterStudent.EnrolmentID INNER JOIN
                      Register WITH (NOLOCK) ON RegisterStudent.RegisterID = Register.RegisterID INNER JOIN
                      RegisterSession WITH (NOLOCK) ON Register.RegisterID = RegisterSession.RegisterID INNER JOIN
                      RegisterMark WITH (NOLOCK) ON 
                      RegisterSession.RegisterSessionID = RegisterMark.RegisterSessionID AND 
                      RegisterStudent.RegisterStudentID = RegisterMark.RegisterStudentID INNER JOIN
                      MarkType WITH (NOLOCK) ON RegisterMark.MarkTypeID = MarkType.MarkTypeID
GROUP BY StudentDetail.RefNo, StudentDetail.AcademicYearID, StudentDetail.FirstForename, 
                      StudentDetail.Surname, Offering.OfferingID, Offering.Code, Offering.Name, 
                      Register.RegisterID, Register.RegisterNo, Register.Title, 
                      Enrolment.CompletionStatusID, Enrolment.StartDate, Enrolment.ExpectedEndDate, 
                      Enrolment.ActualEndDate, MarkType.Mark, MarkType.Description, 
                      RegisterSession.Date
HAVING      (StudentDetail.AcademicYearID = '11/12') AND (Enrolment.CompletionStatusID = '1') AND 
                      (MarkType.Mark = 'W')
ORDER BY StudentDetail.RefNo, RegisterSession.Date


Can anyone point me in the right direction please?

SwePeso
Patron Saint of Lost Yaks

Sweden
29156 Posts

Posted - 08/09/2012 :  07:54:59  Show Profile  Visit SwePeso's Homepage  Reply with Quote
;WITH cteSource([Date], AcademicYearID, RefNo, FirstForename, Surname, OfferingID, Code, Name, RegisterID, RegisterNo, Title, Mark, [Description], CompletionStatusID, StartDate, ExpectedEndDate, ActualEndDate, Expr1, rn)
AS (
	SELECT		ses.[Date],
			sd.AcademicYearID,
			sd.RefNo,
			sd.FirstForename,
			sd.Surname,
			o.OfferingID,
			o.Code,
			o.Name,
			r.RegisterID,
			r.RegisterNo,
			r.Title,
			mt.Mark,
			mt.[Description],
			e.CompletionStatusID,
			e.StartDate,
			e.ExpectedEndDate,
			e.ActualEndDate,
			ses.[Date] AS Expr1,
			ROW_NUMBER() OVER (PARTITION BY sd.RefNo ORDER BY ses.[Date]) AS rn
	FROM		dbo.StudentDetail WITH (NOLOCK) AS sd
	INNER JOIN	dbo.Enrolment WITH (NOLOCK) AS e ON e.StudentDetailID = sd.StudentDetailID
				AND e.CompletionStatusID = '1'
	INNER JOIN	dbo.Offering WITH (NOLOCK) AS o ON o.OfferingID = e.OfferingID
	INNER JOIN	dbo.RegisterStudent WITH (NOLOCK) AS stu ON stu.EnrolmentID = e.EnrolmentID
	INNER JOIN	dbo.Register WITH (NOLOCK) AS r ON r.RegisterID = stu.RegisterID
	INNER JOIN	dbo.RegisterSession WITH (NOLOCK) AS ses ON ses.RegisterID = r.RegisterID
	INNER JOIN	dbo.RegisterMark WITH (NOLOCK) AS rm ON rm.RegisterSessionID = ses.RegisterSessionID
	INNER JOIN	dbo.MarkType WITH (NOLOCK) AS mt ON MarkType.MarkTypeID = rm.MarkTypeID
				AND mt.Mark = 'W'
	WHERE		sd.AcademicYearID = '11/12'
			AND rm.RegisterStudentID = rs.RegisterStudentID
)
SELECT		[Date],
		AcademicYearID,
		RefNo,
		FirstForename,
		Surname,
		OfferingID,
		Code,
		Name,
		RegisterID,
		RegisterNo,
		Title,
		Mark,
		[Description],
		CompletionStatusID,
		StartDate,
		ExpectedEndDate,
		ActualEndDate,
		Expr1
FROM		cteSource
WHERE		rn = 1
ORDER BY	RefNo;



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

chipembele
Yak Posting Veteran

97 Posts

Posted - 08/09/2012 :  08:56:42  Show Profile  Reply with Quote
THanks. worked it out. Works fine.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000