| 
                
                    | 
                            
                                | Author | Topic |  
                                    | macdca44Starting Member
 
 
                                        10 Posts | 
                                            
                                            |  Posted - 2015-02-15 : 10:36:59 
 |  
                                            | I have some sybase queries im trying to write in microsoft sql , and the left jins dont appear to work in the same way as they did.  I have outer joine and have tried embedded selects to avoid having something in the where that makes the left join into an = join, but im still not getting what I want.I want the script below to display one row only, where there is a primary flag on oncologyst and primary on doctor, and its giving me 2 rows, the one I want and one with a blank oncologust - can someone help?ThanksSELECT  DISTINCTPatient.PatientSer,Patient.CreationDate,Course.CourseId, CHI=Patient.PatientId2,   Oncologist=(Select (Doctor.AliasName)  FROM Doctor  WHERE   Patient.PatientSer = Course.PatientSer AND Patient.PatientSer = PatientDoctor.PatientSerAND  PatientDoctor.ResourceSer = Doctor.ResourceSerAND PatientDoctor.OncologistFlag = 1 and  PatientDoctor.PrimaryFlag = 1 )  --Oncologist = CASE WHEN PatientDoctor.OncologistFlag = 1 AND PatientDoctor.PrimaryFlag = 1  THEN Doctor.AliasName END   		          FROM PatientJOIN CourseON Patient.PatientSer = Course.PatientSerJOIN PatientDoctorON  Patient.PatientSer = PatientDoctor.PatientSer JOIN DoctorON  PatientDoctor.ResourceSer = Doctor.ResourceSer WHERE--PatientDoctor.OncologistFlag = 1 AND--PatientDoctor.PrimaryFlag = 1 AND( Upper(Substring(Patient.PatientId,1,1)) not in ('A' , 'B' , 'C' , 'D' , 'E' , 'F' , 'G' , 'H' , 'I' , 'J' , 'K' , 'L' , 'M' , 'N' , 'O' , 'P' , 'Q' , 'R' , 'S' , 'T' , 'U' , 'V' , 'W' , 'X' , 'Y' , 'Z') ) AND ( Upper(Substring(Course.CourseId,1,1)) not in ('A' , 'C' , 'D' , 'E' , 'F' , 'G' , 'H' , 'I' , 'J' , 'K' , 'L' , 'M' , 'N' , 'O' , 'P' , 'Q' , 'R' , 'S' , 'T' , 'U' , 'V' , 'W' , 'X' , 'Y' , 'Z') ) AND Patient.PatientId = 'X'ORDER BY Patient.PatientId ; |  |  
                                    | gbrittonMaster Smack Fu Yak Hacker
 
 
                                    2780 Posts | 
                                        
                                          |  Posted - 2015-02-15 : 14:21:29 
 |  
                                          | Let's simplify!  What do you get with this: SELECT DISTINCTPatient.PatientSer,Patient.CreationDate,Course.CourseId, CHI=Patient.PatientId2, Oncologist= Doctor.aliasnameFROM PatientJOIN CourseON Patient.PatientSer = Course.PatientSerJOIN PatientDoctorON Patient.PatientSer = PatientDoctor.PatientSer JOIN DoctorON PatientDoctor.ResourceSer = Doctor.ResourceSer WHERE Patient.PatiendId = 'X' AND      PatientDoctor.OncologistFlag = 1 AND      PatientDoctor.PrimaryFlag = 1 ANDNote that your checks for alpha characters in the WHERE clause can be simplified to: left(<column>, 1) not like '[a-zA-Z]'but the first one would eliminate PatientID='X' and the last condition (AND PatientID = 'X') only accepts patient X.  Both cannot be true at the same time, or am I misreading this? |  
                                          |  |  |  
                                    | macdca44Starting Member
 
 
                                    10 Posts | 
                                        
                                          |  Posted - 2015-02-18 : 08:48:54 
 |  
                                          | I want the query to give me all patients whether or not they have a course or an oncologist, so the oncologost row would be blank if they had no primary oncologist,  therefore all outer joins are required but seems to give me duplicate rows.  One which is OK and one where oncologist is blank.  I only want a blank row for oncologist of there was none assigned? |  
                                          |  |  |  
                                    | gbrittonMaster Smack Fu Yak Hacker
 
 
                                    2780 Posts | 
                                        
                                          |  Posted - 2015-02-18 : 08:58:24 
 |  
                                          | quote:You say that all OUTER joins are required, but the query you posted is not using OUTER joins.So, change my query to use LEFT JOINS and see what you get.  If you seem to get duplicate rows, then take the query apart and look at the results with no joins, with the first join, with the second and so on.  This will show you where the extra rows come in which will help you understand why and how to filter them out.Originally posted by macdca44
 I want the query to give me all patients whether or not they have a course or an oncologist, so the oncologost row would be blank if they had no primary oncologist,  therefore all outer joins are required but seems to give me duplicate rows.  One which is OK and one where oncologist is blank.  I only want a blank row for oncologist of there was none assigned?
 
 |  
                                          |  |  |  
                                    | macdca44Starting Member
 
 
                                    10 Posts | 
                                        
                                          |  Posted - 2015-02-18 : 09:03:58 
 |  
                                          | This is more like what im trying to do:SELECT  DISTINCTPatient.PatientId,Patient.PatientSer,Patient.CreationDate,CHI=Patient.PatientId2,   Oncologist=(Select (Doctor.AliasName)  FROM Doctor  WHERE  Patient.PatientSer = PatientDoctor.PatientSerAND  PatientDoctor.ResourceSer = Doctor.ResourceSerAND PatientDoctor.OncologistFlag = 1 and  PatientDoctor.PrimaryFlag = 1 )   		          FROM PatientLEFT OUTER JOIN PatientDoctorON  Patient.PatientSer = PatientDoctor.PatientSer LEFT OUTER JOIN DoctorON  PatientDoctor.ResourceSer = Doctor.ResourceSer WHERE( Upper(Substring(Patient.PatientId,1,1)) not in ('A' , 'B' , 'C' , 'D' , 'E' , 'F' , 'G' , 'H' , 'I' , 'J' , 'K' , 'L' , 'M' , 'N' , 'O' , 'P' , 'Q' , 'R' , 'S' , 'T' , 'U' , 'V' , 'W' , 'X' , 'Y' , 'Z') ) ORDER BY Patient.PatientId ; |  
                                          |  |  |  
                                    | gbrittonMaster Smack Fu Yak Hacker
 
 
                                    2780 Posts | 
                                        
                                          |  Posted - 2015-02-18 : 10:36:07 
 |  
                                          | OK (please use my simplification in the WHERE clause).  What do you get with your modified query? |  
                                          |  |  |  
                                    | macdca44Starting Member
 
 
                                    10 Posts | 
                                        
                                          |  Posted - 2015-02-19 : 11:17:45 
 |  
                                          | I think that gives me what I need, thanks you |  
                                          |  |  |  
                                |  |  |  |