2009-08-28 8 views
0

Je rencontre un problème dans lequel cette instruction de requête me donne un objetTexte récurrent dans les résultats. Des idées?La requête SQL renvoyant des informations répétitives

L'instruction de requête complète:

Select g.GoalText, convert(nvarchar, g.GoalID) + '|' + convert(nvarchar, r.GoalReqID) as GoalID, GoalReqID 
from Goal g inner join GoalRequirement r 
on g.GoalID = r.GoalID 
where GoalReqID in 
    (Select GoalReqID 
    from GoalRequirement r inner join SurveyAnswer a 
    on r.QuestionID = a.QuestionID and ReqQuestionValue = SurveyAnswer 
    where a.CycleID = 93 and ReqBMILevel is null 
    and ReqEnergyBalance is null and SurveyAnswer = 1 and r.QuestionID in 
    (Select QuestionID from Question where QuestionParent = 6000));

Résultats:

GoalText GoalID GoalReqID 
Choose lasagna, ravioli, stuffed pasta 1-3 times a week instead of 4 or more times a week. 13|442 442 
Choose macaroni and cheese 1-3 times a week instead of 4 or more times a week. 14|443 443 
Choose meats in gravies- beef stew, chicken pot pie,… 1-3 times a week instead of 4 or more times a week. 15|444 444 
Choose spaghetti, meat sauce and/or meatballs, 1-3 times a week instead of 4 or more times a week. 16|445 445 
Choose tacos, burritos, enchiladas, nachos with meat and/or cheese 1-3 times a week instead of 4 or more times a week. 17|446 446 
Choose biscuits and sausage gravy 1-3 times a week instead of 4 or more times a week. 102|482 482 
Choose pizza- all types and calzones 1-3 times a week instead of 4 or more times a week. 12|483 483 
Choose hamburger/tuna/chicken noodle casseroles (includes “Helper”) 1-3 times a week instead of 4 or more times a week. 130|484 484 
Choose lasagna, ravioli, stuffed pasta 1-3 times a week instead of 4 or more times a week. 13|485 485 
Choose macaroni and cheese 1-3 times a week instead of 4 or more times a week. 14|486 486 
Choose meats in gravies- beef stew, chicken pot pie,… 1-3 times a week instead of 4 or more times a week. 15|487 487 
Choose spaghetti- marinara sauce only, 1-3 times a week instead of 4 or more times a week. 132|488 488 
Choose spaghetti, meat sauce and/or meatballs, 1-3 times a week instead of 4 or more times a week. 16|489 489 
Choose tacos, burritos, enchiladas, nachos with meat and/or cheese 1-3 times a week instead of 4 or more times a week. 17|490 490

Les requêtes individuelles qui composent la requête:

Select g.GoalText, convert(nvarchar, g.GoalID) + '|' + convert(nvarchar, r.GoalReqID) as GoalID, GoalReqID 
from Goal g inner join GoalRequirement r 
on g.GoalID = r.GoalID

Résultats: Il y a 444 enregistrements dans cette requête , mais vous devriez avoir l'idée.

GoalText GoalID GoalReqID 
Eat an additional 400-500 calories per day. 1|1 1 
Choose fried potatoes, French fries, hash browns, potato salad 1-3 times a week instead of 4 or more times a week. 2|2 2 
Choose fried potatoes, French fries, hash browns, potato salad 1-3 times a week instead of 4 or more times a week. 2|106 106 
Choose fried potatoes, French fries, hash browns, potato salad 1-3 times a week instead of 4 or more times a week. 2|144 144 
Choose fried potatoes, French fries, hash browns, potato salad 1-3 times a week instead of 4 or more times a week. 2|182 182 
Choose fried potatoes, French fries, hash browns, potato salad 1-3 times a week instead of 4 or more times a week. 2|219 219 
Choose fried potatoes, French fries, hash browns, potato salad 1-3 times a week instead of 4 or more times a week. 2|256 256 
Choose fried potatoes, French fries, hash browns, potato salad 1-3 times a week instead of 4 or more times a week. 2|293 293 
Choose fried potatoes, French fries, hash browns, potato salad 1-3 times a week instead of 4 or more times a week. 2|330 330 
Choose canned fruit, in heavy syrup, 1-3 times a week instead of 4 or more times a week. 3|331 331 
Choose canned fruit, in heavy syrup, 1-3 times a week instead of 4 or more times a week. 3|294 294 
Choose canned fruit, in heavy syrup, 1-3 times a week instead of 4 or more times a week. 3|257 257 
Choose canned fruit, in heavy syrup, 1-3 times a week instead of 4 or more times a week. 3|220 220 
Choose canned fruit, in heavy syrup, 1-3 times a week instead of 4 or more times a week. 3|183 183 
Choose canned fruit, in heavy syrup, 1-3 times a week instead of 4 or more times a week. 3|145 145 
Choose canned fruit, in heavy syrup, 1-3 times a week instead of 4 or more times a week. 3|107 107 
Choose canned fruit, in heavy syrup, 1-3 times a week instead of 4 or more times a week. 3|3 3 
Choose lower fat milk (nonfat, skim, 1%, 2%) instead of whole milk. 4|4 4 
Choose lower fat milk (nonfat, skim, 1%, 2%) instead of whole milk. 4|108 108 
Choose lower fat milk (nonfat, skim, 1%, 2%) instead of whole milk. 4|146 146 
Choose lower fat milk (nonfat, skim, 1%, 2%) instead of whole milk. 4|184 184 
Choose lower fat milk (nonfat, skim, 1%, 2%) instead of whole milk. 4|221 221
Select GoalReqID 
    from GoalRequirement r inner join SurveyAnswer a 
    on r.QuestionID = a.QuestionID and ReqQuestionValue = SurveyAnswer 
    where a.CycleID = 93 and ReqBMILevel is null 
    and ReqEnergyBalance is null and SurveyAnswer = 1

Résultats:

GoalReqID 
478 
479 
480 
481 
482 
440 
441 
483 
484 
485 
442 
443 
486 
487 
444 
488 
489 
445
Select QuestionID from Question where QuestionParent = 6000

Résultats:

QuestionID 
6000 
6001 
6002 
6003 
6004 
6005 
6006 
6007 
6008 
6009

Répondre

7

Vous joindre à deux tables. Évidemment, les enregistrements de la première table correspondent à plus d'un enregistrement de la deuxième table. Lorsque cela se produit, l'enregistrement de la première table est dupliqué dans le jeu de résultats pour chaque enregistrement de la deuxième table qu'il correspond.

+0

Merci de me dire ce qui se passe. Une suggestion pour résoudre mon problème? – NMan

+4

Cela dépend: qu'est-ce que vous essayez de montrer? –

+2

La réponse simple est simplement d'écrire votre jointure de sorte que la 1ère table ne corresponde qu'à un résultat du 2ème. Mais pour bien faire, nous devons mieux comprendre quels critères devraient définir exactement quelle ligne devrait être. –

1

Lorsqu'une ligne de la première table se joint à plusieurs lignes de la deuxième table, vous souhaitez apparemment qu'elle s'affiche une seule fois dans la sortie de la requête. Dans ce cas, QUELLE ligne de la 2ème table voulez-vous montrer dans cette ligne de sortie unique?

  • La ligne entrée en dernier?
  • avec le plus grand goalId?
  • ou quoi que ...

Vous devez répondre à cette question avant de pouvoir écrire une requête SQL pour faire tout ce que votre réponse précise ..

4

Re-écrit votre requête de sorte qu'il est plus facile à lire, et tourne subquerys (beurk) en JOIN:

SELECT g.goaltext 
     g.goalid, 
     gr.goalreqid 
    FROM GOAL g 
    JOIN GOALREQUIREMENT gr ON gr.goalid = g.goalid AND gr.reqbmilevel IS NULL AND gr.reqenergybalance IS NULL 
    JOIN JOIN SURVEYANSWER sa ON sa.questionid = gr.questionid AND sa.surveyanswer = gr.reqquestionvalue AND sa.surveyanswer = 1 
    JOIN QUESTION q ON q.questionid = gr.questionid 
WHERE sa.cycleid = 93 

Il est le goalid et/ou de la colonne goalreqid (s) qui provoque les lignes à sortir en double. Étant donné que la colonne de texte cible est associée à plusieurs goalid/etc, vous n'obtiendrez jamais d'entrées uniques pour le texte cible tout en incluant les ID de cible.

+1

+1 pour éviter les sous-requêtes £! –

Questions connexes