Author |
Topic |
abomb
Starting Member
13 Posts |
Posted - 2008-12-29 : 13:56:30
|
This question is regarding a custom survey application database and has 6 relevant tables: substance- substanceIDfield- fieldIDsurveyfield- surveyfieldID- substanceID- fieldIDsurvey- surveyIDsurveyfieldanswer- surveyFieldID- supplierSurveyID- answersuppliersurvey- suppliersurveyID- surveyIDMy select statement should return all possible combinations of substanceID and fieldID for each suppliersurvey, and the answer or NULL depending if there is a record in surveyfield or surveyfieldanswer.There are 7 field, 31 substance, 14 suppliersurvey records so the query should return 3038 records.Can anyone help me solve this problem? |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-29 : 14:07:11
|
[code]select sy.*,t.*from suppliersurvey ssleft join (select sfa.supplierSurveyID,sfa.answer,sfa.surveyFieldID,s.*,f.*from surveyfieldanswer sfaon ss.supplierSurveyID=sfa.supplierSurveyIDinner join surveyfield sfon sf.surveyfieldID=sfa.surveyfieldIDinner join substance son s.substanceID=sf.substanceIDinner join field fon f. fieldID=sf.fieldID)ton t.supplierSurveyID=ss.supplierSurveyIDleft join survey syon sy.surveyID=ss.surveyID[/code] |
|
|
abomb
Starting Member
13 Posts |
Posted - 2008-12-29 : 14:11:47
|
Msg 156, Level 15, State 1, Line 8Incorrect syntax near the keyword 'on'. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-29 : 14:14:10
|
quote: Originally posted by abomb Msg 156, Level 15, State 1, Line 8Incorrect syntax near the keyword 'on'.
copy paste mistake.select sy.*,t.*from suppliersurvey ssleft join (select sfa.supplierSurveyID,sfa.answer,sfa.surveyFieldID,s.*,f.*from surveyfieldanswer sfainner join surveyfield sfon sf.surveyfieldID=sfa.surveyfieldIDinner join substance son s.substanceID=sf.substanceIDinner join field fon f. fieldID=sf.fieldID)ton t.supplierSurveyID=ss.supplierSurveyIDleft join survey syon sy.surveyID=ss.surveyID |
|
|
abomb
Starting Member
13 Posts |
Posted - 2008-12-29 : 14:25:10
|
that can't be right because it only returns 937 rows. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-29 : 14:37:32
|
ok.seems like what you're after is cross joinselect *from(select ss.suppliersurveyID,ss.surveyID,sy.*,s.substanceID,f.fieldIDfrom suppliersurvey ssinner join survey syon sy.surveyid=ss.surveyidcross join substance scross join field f)t1left join(select sfa.surveyfieldID,sfa.supplierSurveyID,sfa.answer,sf.substanceID,sf.fieldIDfrom surveyfield sfinner join surveyfieldanswer sfaon sfa.surveyfieldID=sf.surveyfieldID)t2on t2.supplierSurveyID=t1.supplierSurveyIDand t2.fieldID=t1.fieldIDand t2.substanceID=t1.substanceID |
|
|
abomb
Starting Member
13 Posts |
Posted - 2008-12-29 : 14:39:54
|
I kinda tweaked yours a bit and I think I got it.SELECT t.*, v.answerFROM (SELECT DISTINCT ss.supplierSurveyID, s.substanceID, f.fieldIDFROM Compliance.Substance s, Compliance.Field f, Compliance.SupplierSurvey ss)tLEFT JOIN (SELECT DISTINCT surveyFieldID, substanceID, fieldIDFROM Compliance.SurveyField)uON t.substanceID = u.substanceID AND t.fieldID = u.fieldIDLEFT JOIN (SELECT supplierSurveyID, surveyFieldID, answerFROM Compliance.SurveyFieldAnswer sfa)vON t.supplierSurveyID = v.supplierSurveyID AND u.surveyFieldID = v.surveyFieldID |
|
|
abomb
Starting Member
13 Posts |
Posted - 2008-12-29 : 14:54:09
|
thanks for your suggestion visakh16! |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-29 : 14:54:59
|
welcome |
|
|
gtrain
Starting Member
7 Posts |
Posted - 2008-12-30 : 19:31:53
|
abomb you in australia? |
|
|
abomb
Starting Member
13 Posts |
Posted - 2008-12-31 : 09:06:42
|
no, canada.why do you ask? |
|
|
|