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 |
|
beheer25
Starting Member
4 Posts |
Posted - 2009-04-15 : 10:40:52
|
Hi,I am developing a query that needs to join a subquery with a table. Simplified it looks like this:SELECT Detail.idThema, Detail.txtAfdeling, intStartPct, count(Detail.idRespondent)FROM (SELECT idThema, txtAfdeling, idRespondent, Score FROM AnotherTable) AS DetailLEFT OUTER JOIN enq_ThemaAdvies ON Detail.idThema = enq_ThemaAdvies.idThema AND Score >= intStartPct AND Score <= intEindPctGROUP BY Detail.idThema, intStartPct, Detail.txtAfdeling I have tried everything I know, but I can't get the outer join to work. It has somthing to do with the extra condition comparing "score" to intStartPct. Score is a calculated column in the subquery. As soon as I leave out that comparison, the empty rows from enq_ThemaAdvies are shown.Does anybody have any ideas?Kind regards, Mark |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-04-15 : 14:01:10
|
| how is score derived? you've not shown that. run subquery alone and check if you get correct values for score field. also look for presence of NULLs in score |
 |
|
|
beheer25
Starting Member
4 Posts |
Posted - 2009-04-15 : 15:11:13
|
| Hi,The application thi is for is a survey tool. I calculate a score for each respondent of a survey and count all scores that fall in a specific range by using the extra conditions in the join. There are no nulls in the Detail resultset. The complete resultset of the query is OK, but the ranges between intStartPct and intEindPct which do not have any respondents with a corresponding score are not shown in the resultset.What I get:idThema; txtAfdeling; intStartPct; count(idRepondent)1; Department1; 0; 21; Department1; 21; 31; Department1; 81; 4What I need:idThema; txtAfdeling; intStartPct; count(idRepondent)1; Department1; 0; 21; Department1; 21; 31; Department1; 41; NULL1; Department1; 61; NULL1; Department1; 81; 4Hope this makes it any clearer...Mark |
 |
|
|
asgast
Posting Yak Master
149 Posts |
Posted - 2009-04-16 : 03:38:43
|
| you can get the same result if you put additional join conditions in where clausealso giving us output results without test data is useless, i can't imagine what you have in your table, so seeing your results doesn't make it clearerAlso I don't get why you are using sub select here. What are you trying to gain from it?SELECT Detail.idThema, Detail.txtAfdeling, intStartPct, count(Detail.idRespondent)FROM (SELECT idThema, txtAfdeling, idRespondent, Score FROM AnotherTableLEFT OUTER JOIN enq_ThemaAdvies ON Detail.idThema = enq_ThemaAdvies.idThema WHERE Score >= intStartPct AND Score <= intEindPctGROUP BY Detail.idThema, intStartPct, Detail.txtAfdeling |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-04-16 : 11:04:06
|
quote: Originally posted by beheer25 Hi,The application thi is for is a survey tool. I calculate a score for each respondent of a survey and count all scores that fall in a specific range by using the extra conditions in the join. There are no nulls in the Detail resultset. The complete resultset of the query is OK, but the ranges between intStartPct and intEindPct which do not have any respondents with a corresponding score are not shown in the resultset.What I get:idThema; txtAfdeling; intStartPct; count(idRepondent)1; Department1; 0; 21; Department1; 21; 31; Department1; 81; 4What I need:idThema; txtAfdeling; intStartPct; count(idRepondent)1; Department1; 0; 21; Department1; 21; 31; Department1; 41; NULL1; Department1; 61; NULL1; Department1; 81; 4Hope this makes it any clearer...Mark
how will count(idRepondent) return null? it wont at any case. it will ignore nulls and give only 0 if value is all null |
 |
|
|
beheer25
Starting Member
4 Posts |
Posted - 2009-04-16 : 14:53:32
|
Hi,I thought this was an issue of me doing something wrong with an outer join. At least, the idea of an outer join is to return all results from one table regardless of the existance of matching rows in the second table. Or am I wrong here.The complete query I have now (including the way of calculating "Score": SELECT Detail.idThema, txtAfdeling, intStartPct, count(*) FROM ( SELECT idThema, enq_Respondent.idRespondent, txtAfdeling, enq_Vraag.idVraag, convert(real,intScore) / convert(real, maxScore) * @intMaxScore as Score FROM enq_Vraag INNER JOIN enq_Resultaat ON enq_Resultaat.idVraag = enq_Vraag.idVraag INNER JOIN enq_Respondent ON enq_Respondent.idRespondent = enq_Resultaat.idRespondent INNER JOIN enq_Persoon ON enq_Persoon.idPersoon = enq_Respondent.idPersoon INNER JOIN enq_Antwoord ON enq_Resultaat.idResultaat = enq_Antwoord.idResultaat INNER JOIN enq_MeerkeuzeAntwoord ON enq_Antwoord.intAntwoord = enq_MeerkeuzeAntwoord.idMeerkeuzeantwoord INNER JOIN enq_Pagina ON enq_Pagina.idPagina = enq_Vraag.idPagina INNER JOIN enq_VraagSubthema ON enq_VraagSubthema.idVraag = enq_Vraag.idVraag INNER JOIN enq_Subthema ON enq_VraagSubthema.idSubthema = enq_Subthema.idSubthema INNER JOIN ( SELECT enq_Vraag.idVraag, max(intScore) as maxScore FROM enq_Vraag INNER JOIN enq_MeerkeuzeAntwoord ON enq_Vraag.idVraag = enq_MeerkeuzeAntwoord.idVraag INNER JOIN enq_Pagina ON enq_Pagina.idPagina = enq_Vraag.idPagina INNER JOIN enq_VraagSubthema ON enq_VraagSubthema.idVraag = enq_Vraag.idVraag INNER JOIN enq_Subthema ON enq_VraagSubthema.idSubthema = enq_Subthema.idSubthema WHERE idEnquete = @idEnquete GROUP BY enq_Vraag.idVraag) AS MaxPerVraag ON MaxPerVraag.idVraag = enq_Vraag.idVraag WHERE enq_Pagina.idEnquete = @idEnquete AND enq_Respondent.blnAfgerond = 1 AND enq_Persoon.intAfdelingNiveau = @intOrganisatieNiveau ) as Detail LEFT OUTER JOIN enq_ThemaAdvies ON Detail.idThema = enq_ThemaAdvies.idThema AND Score >= intStartPct AND Score <= intEindPct GROUP BY Detail.idThema, intStartPct, txtAfdeling Perhaps now you see what I tried to spare you But still, any suggestions of how to correct this join are welcome.Thanks in advance,Mark |
 |
|
|
beheer25
Starting Member
4 Posts |
Posted - 2009-04-17 : 13:31:25
|
| Hi,I solved it by doing a trick with a cursor. Not very performance friendly, but in the situation the procedure is used good enough.Thanks for your input all...See you, Mark |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-04-17 : 13:43:34
|
| i dont think you need to use a cursor here. if you can provide some sample data and output you need out of it, somebody will be able to provide you an alternative |
 |
|
|
|
|
|
|
|