2009-08-28 164 views
0

我遇到了这个查询语句让我在结果中重复GoalText的问题。有任何想法吗?返回重复信息的SQL查询

完整的查询语句:

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));

结果:

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

各个查询组成的查询:

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

结果:共有444条记录在此查询,但你应该明白。

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

结果:

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

结果:

QuestionID 
6000 
6001 
6002 
6003 
6004 
6005 
6006 
6007 
6008 
6009

回答

7

你将两个表连接在一起。显然,第一个表中的记录匹配第二个表中的多个记录。发生这种情况时,来自第一个表的记录会在第二个表中匹配的每个记录的结果集中重复。

+0

感谢您告诉我发生了什么事。任何解决我的问题的建议? – NMan 2009-08-28 14:55:55

+4

这取决于:你想展示什么? – 2009-08-28 14:56:28

+2

简单的答案是只写入你的连接,以便第一个表只匹配第二个中的一个结果。但为了做到这一点,我们需要更好地理解什么标准应该确切地定义应该是哪一行。 – 2009-08-28 15:03:31

1

当第一个表中的行连接到第二个表中的多个行时,您显然只希望它在查询输出中显示一次。在这种情况下,你想在这个单一输出行中显示第二个表中的WHICH行吗?

  • 最后一行输入?
  • 最大的目标是?
  • 或任何...

你需要回答这个问题之前,你可以写一个SQL查询做任何你的答案指定..

4

重新写你的查询,使其更具可读性,并原来subquerys(呸)到联接:

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 

这是goalid和/或goalreqid柱(s)表示,导致排出来复制。因为goaltext列与多个goalid/etc关联,所以在包含goalids时,您永远不会获得goaltext的单个条目。

+1

+1用于避免子查询£! – 2009-08-28 15:34:05