Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Select first (Minimum) avilable course


Select first (Minimum) avilable course

Author
Message
Roy Rogers
Roy Rogers
SSC-Addicted
SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)

Group: General Forum Members
Points: 458 Visits: 90
DECLARE @Courses TABLE
(
CourseID INT,
Course VARCHAR(256)
)

INSERT @Courses
SELECT 1, 'Course1' UNION
SELECT 2, 'Course2' UNION
SELECT 3, 'Course3' UNION
SELECT 4, 'Course4'


DECLARE @EnRollment TABLE
(
CourseID INT,
Student VARCHAR(256)
)

INSERT @EnRollment
SELECT 1, 'Student1' UNION
SELECT 1, 'Student2' UNION
SELECT 1, 'Student4' UNION
SELECT 1, 'Student5' UNION
SELECT 1, 'Student6' UNION
SELECT 2, 'Student1' UNION
SELECT 2, 'Student4' UNION
SELECT 2, 'Student5' UNION
SELECT 3, 'Student1' UNION
SELECT 3, 'Student5' UNION
SELECT 3, 'Student6' UNION
SELECT 4, 'Student1'

/*

I need the minimum eligiple course for a student if I pass student

Example

Student1 is enrolled in all courses I need a - null
Student2 Enroled 1 next 2 is not enrolled I need - 2
Student3 did not enroll any so I need - 1
Student5 Enrolled in 1, 2, 3, So I need - 4

*/

DECLARE @NewCourse INT

DECLARE @Student VARCHAR(256)
SELECT @Student = 'Student5'

SELECT @NewCourse = SQLQuery


SELECT @NewCourse -- This should return 4

Regards,
gova
GSquared
GSquared
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14375 Visits: 9729
Try this:


select @NewCourse =
(select min(Courses.CourseID)
from @Courses Courses
left outer join @Enrollment Enrollment
on Courses.CourseID = Enrollment.CourseID
and Student = @Student
where Enrollment.CourseID is null);



See if that'll do what you need.

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Roy Rogers
Roy Rogers
SSC-Addicted
SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)

Group: General Forum Members
Points: 458 Visits: 90
Perfect.

I have to admit I am stupid.

Thanks a million.

Regards,
gova
GSquared
GSquared
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14375 Visits: 9729
Not stupid. Just have stuff to learn about SQL. Who doesn't?

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search