Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
g_r_a_robinson
Starting Member
45 Posts |
Posted - 2004-01-08 : 00:34:44
|
| I want to return all jobIDs (whether read or unread) but in case a jobID has both read and unread notes i just want to return the unread part? So in the case of below:jobID 167 unRead jobID 168 unRead jobID 168 Read jobID 169 Read the output would be would be jobID 167 unRead jobID 168 unRead jobID 169 Read I'm sure its not too difficult but i've tried a lot.. to no availsee below (and I thinks its long winded for what i need)CREATE PROCEDURE spGetJobsByUnreadAndReadByUserID @UserID INT ASSELECT DISTINCT Master_Jobs.JobID, Profiles.ProfileDescriptor, Backup_UserNotes.BackUp_Read, Master_Jobs.Job_Title, Master_Jobs.Contact, Master_Jobs.Due_Date, Master_Jobs.Due_Time, Master_Jobs.Next_Action, Master_Jobs.By_WhoFROM Master_Jobs INNER JOIN Note ON Master_Jobs.JobID = Note.FK_JobID INNER JOIN Backup_UserNotes ON Note.NoteID = Backup_UserNotes.BackUp_NoteID INNER JOIN User_Notes ON Note.NoteID = User_Notes.FK_UN_NoteID INNER JOIN Job_Assignments ON Master_Jobs.JobID = Job_Assignments.FK_Master_JobID INNER JOIN Profiles ON Master_Jobs.FK_ProfileID = Profiles.ProfileID INNER JOIN Users ON Backup_UserNotes.BackUp_UserID = Users.UserID AND User_Notes.FK_UN_UserID = Users.UserID AND Job_Assignments.UserID = Users.UserIDWHERE Users.UserID = @UserID AND-- Note.FK_UserID = User_Notes.FK_UN_UserID AND BackUp_Read = 'UnRead'ORDER BY BackUp_Read ASCSELECT DISTINCT Master_Jobs.JobID, Profiles.ProfileDescriptor, Backup_UserNotes.BackUp_Read, Master_Jobs.Job_Title, Master_Jobs.Contact, Master_Jobs.Due_Date, Master_Jobs.Due_Time, Master_Jobs.Next_Action, Master_Jobs.By_WhoFROM Master_Jobs INNER JOIN Note ON Master_Jobs.JobID = Note.FK_JobID INNER JOIN Backup_UserNotes ON Note.NoteID = Backup_UserNotes.BackUp_NoteID INNER JOIN User_Notes ON Note.NoteID = User_Notes.FK_UN_NoteID INNER JOIN Job_Assignments ON Master_Jobs.JobID = Job_Assignments.FK_Master_JobID INNER JOIN Profiles ON Master_Jobs.FK_ProfileID = Profiles.ProfileID INNER JOIN Users ON Backup_UserNotes.BackUp_UserID = Users.UserID AND User_Notes.FK_UN_UserID = Users.UserID AND Job_Assignments.UserID = Users.UserIDWHERE Users.UserID = @UserID AND-- Note.FK_UserID = User_Notes.FK_UN_UserID AND BackUp_Read = 'Read'ORDER BY BackUp_Read ASCGO |
|
|
Lewie
Starting Member
42 Posts |
Posted - 2004-01-08 : 05:21:08
|
| Assuming you only want one result set I have added a union... try this... not test. but it gives you an ideaCREATE PROCEDURE spGetJobsByUnreadAndReadByUserID@UserID INTASSELECT DISTINCT Master_Jobs.JobID, Profiles.ProfileDescriptor, Backup_UserNotes.BackUp_Read, Master_Jobs.Job_Title, Master_Jobs.Contact, Master_Jobs.Due_Date, Master_Jobs.Due_Time, Master_Jobs.Next_Action, Master_Jobs.By_WhoFROM Master_Jobs INNER JOIN Note ON Master_Jobs.JobID = Note.FK_JobID INNER JOIN Backup_UserNotes ON Note.NoteID = Backup_UserNotes.BackUp_NoteID INNER JOIN User_Notes ON Note.NoteID = User_Notes.FK_UN_NoteID INNER JOIN Job_Assignments ON Master_Jobs.JobID = Job_Assignments.FK_Master_JobID INNER JOIN Profiles ON Master_Jobs.FK_ProfileID = Profiles.ProfileID INNER JOIN Users ON Backup_UserNotes.BackUp_UserID = Users.UserID AND User_Notes.FK_UN_UserID = Users.UserID AND Job_Assignments.UserID = Users.UserIDWHERE Users.UserID = @UserID AND BackUp_Read = 'Read' AND Master_Jobs.JobID not in ( SELECT Master_Jobs.JobID FROM Master_Jobs INNER JOIN Note ON Master_Jobs.JobID = Note.FK_JobID INNER JOIN Backup_UserNotes ON Note.NoteID = Backup_UserNotes.BackUp_NoteID INNER JOIN User_Notes ON Note.NoteID = User_Notes.FK_UN_NoteID INNER JOIN Job_Assignments ON Master_Jobs.JobID = Job_Assignments.FK_Master_JobID INNER JOIN Profiles ON Master_Jobs.FK_ProfileID = Profiles.ProfileID INNER JOIN Users ON Backup_UserNotes.BackUp_UserID = Users.UserID AND User_Notes.FK_UN_UserID = Users.UserID AND Job_Assignments.UserID = Users.UserID WHERE Users.UserID = @UserID AND BackUp_Read = 'UnRead')unionSELECT DISTINCT Master_Jobs.JobID, Profiles.ProfileDescriptor, Backup_UserNotes.BackUp_Read, Master_Jobs.Job_Title, Master_Jobs.Contact, Master_Jobs.Due_Date, Master_Jobs.Due_Time, Master_Jobs.Next_Action, Master_Jobs.By_WhoFROM Master_Jobs INNER JOINNote ON Master_Jobs.JobID = Note.FK_JobID INNER JOINBackup_UserNotes ON Note.NoteID = Backup_UserNotes.BackUp_NoteID INNER JOINUser_Notes ON Note.NoteID = User_Notes.FK_UN_NoteID INNER JOINJob_Assignments ON Master_Jobs.JobID = Job_Assignments.FK_Master_JobID INNER JOINProfiles ON Master_Jobs.FK_ProfileID = Profiles.ProfileID INNER JOINUsers ON Backup_UserNotes.BackUp_UserID = Users.UserID AND User_Notes.FK_UN_UserID = Users.UserID AND Job_Assignments.UserID = Users.UserIDWHERE Users.UserID = @UserID AND-- Note.FK_UserID = User_Notes.FK_UN_UserID AND BackUp_Read = 'UnRead'ORDER BY BackUp_Read ASCGO |
 |
|
|
g_r_a_robinson
Starting Member
45 Posts |
Posted - 2004-01-08 : 05:39:27
|
| Thanks lewie. Im about to try it. Did you say that this will return only one record?Because I actually need to return many records from this query. |
 |
|
|
Lewie
Starting Member
42 Posts |
Posted - 2004-01-08 : 07:13:05
|
| Many records |
 |
|
|
g_r_a_robinson
Starting Member
45 Posts |
Posted - 2004-01-08 : 20:13:46
|
| It worked!!!Thanks so much, I've been on this for days!!!Much Appreciated!! |
 |
|
|
|
|
|
|
|