Current location: Hot Scripts Forums » Other Discussions » Database » SQL query question

SQL query question

Reply
  #1 (permalink)  
Old 11-05-09, 11:05 AM
landing's Avatar
landing landing is offline
Coding Addict
 
Join Date: Jul 2006
Location: Scotland
Posts: 302
Thanks: 0
Thanked 0 Times in 0 Posts
SQL query question

Hi there

I'm trying to establish if the following is possible using SQL on it's own (I know it can be done by means of scripting)

I have the following table

Code:
year (int)
course (string)
rep_id (int)
The table describes "course representitives" in a particular year. I'm trying to find a way to list the rep_id of all reps who have been a rep in 2 consecutive years. I can't for the life of me establish the SQL.

Hope someone can help.


Thanks
__________________
Always sanitise your data


Best regards
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 11-05-09, 05:07 PM
carters-site's Avatar
carters-site carters-site is offline
Newbie Coder
 
Join Date: Sep 2009
Location: Moline, IL
Posts: 73
Thanks: 1
Thanked 1 Time in 1 Post
I could not come up with a very good SQL statement maybe someone else can.

Here is a mySQL solution...(I think will work I didn't not bother to test.)
Basically I built a function that you can test the result against on a per row basis to see if the current row year has another row in the able that matches the rep and course and is also only 1 year difference. As long as that result is 1 or greater then you should keep that row.

Code:
CREATE FUNCTION getYearcount(course VARCHAR(100), rep_id INT, YEAR INT)
RETURNS INT
READS SQL DATA

DETERMINISTIC
  BEGIN
  
  DECLARE numRep;
  
     SET @tmpCourse = course;
     SET @tmpRep_id = rep_id; 
     SET @tmpYear = year;
     
     SELECT count(rep_id) FROM course_reps WHERE course=@tmpCourse AND rep_id = @tmpRep_id 
     AND (year = @tmpYear - 1 OR year = @tmpYear + 1) 
     INTO numRep; 
     
     RETURN numRep;
  END
Your sql would be something like this.

Code:
SELECT rep_id FROM reps WHERE getYearcount(course, rep_id, year) > 0;
Should only return rows with the results you want... This is in theory and untested code. Hopefully that helps you out. Oh by the way this needs MySQL 5 with function support.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
OOP C-Sharp DB Access Wrapper Question digioz ASP.NET 1 09-08-08 10:54 AM
Question on sql query ajs PHP 1 03-10-05 05:27 AM
Declared Functions skipper23 PHP 4 12-17-03 11:06 AM
index page not showing up skipper23 PHP 3 12-15-03 02:10 PM
change my field in this example sal21 ASP 3 07-14-03 03:49 AM


All times are GMT -5. The time now is 09:12 PM.
vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.