I have two tables QuestionTarget in which I have save the Target of question and Answer table in which i have Achieved value of that target .
Question Target Table
QuestionID,Targetvalue
1 55
2 67
Answer Table
AnswerID Question01 AchievedValue Question02 AchievedValue Question03 Achieved Value
Now I want to get the Target from the target table and AchievedValue from the answer table
My Query is this
"SELECT A.AnswerId,CdfPErsonName, A.Year, A.Quarter, QT.QuestionTarget,A.Question01Total,Question01 FROM
Answers AS A
LEFT JOIN
QuestionTarget AS QT ON QT.Question_ID=A.Question01 Where QT.Question_ID=1"
I also used LEFT OUTER JOIN INNER JOIN RIGHT JOIN NO ONE WORK THE PROBLEM IS THAT
below is the record set which comes from the query
Question01 QuestionTarget QuestionAchieved
1 53 12
2 77 12
The question Target Columns are good they are saved as 53,77 but in answer table i have save answer of 12 against 53 then why it is pulling 12 against 77 also that is the question of the week
Loading


Joginder BangerRank #360 · 7K reputationPosted Dec 30, 2014, 1:47 AM EST
your question is not clear. You are create the confusion just rearrange question. which link create you are between both table, means that i have no idea in which column behalf you are join.
have any primary key and foreign key in both table.
Joginder BangerRank #360 · 7K reputationPosted Dec 30, 2014, 1:54 AM EST
if above condition are true then you are using following query.
Note:- One Condition if you want show both are table have question id same.
"SELECT A.AnswerId,CdfPErsonName, A.Year, A.Quarter, QT.QuestionTarget,A.Question01Total,Question01 FROM
Answers AS A
Inner Join QuestionTarget AS QT ON QT.Question_ID=A.Question01 Where QT.Question_ID=1"
condition second:-
"SELECT A.AnswerId,CdfPErsonName, A.Year, A.Quarter, QT.QuestionTarget,A.Question01Total,Question01 FROM
Answers AS A
right Join QuestionTarget AS QT ON QT.Question_ID=A.Question01 Where QT.Question_ID=1"
Danish HabibRank #2,024 · 715 reputationPosted Dec 30, 2014, 1:56 AM EST
Joginder BangerRank #360 · 7K reputationPosted Dec 30, 2014, 2:08 AM EST
if you have not Primary key and foreign key ,this is bad idea a for you. Your database have lot of duplicate data. if your wish is no need join both table. No problem.
show your answer table data once again.
Joginder BangerRank #360 · 7K reputationPosted Dec 30, 2014, 2:18 AM EST
Question Table
ID QuestionTarget
1 53
2 77
Answer Table
id Answer
1 12
if you want show data have match the id answer table
try this
select * from questionTable Q
inner join Answertable A on Q.id=A.ID where Q.id=1
second Condition
if you want show the questiontable complete data and answer match only.
select * from questionTable Q
left join Answertable A on Q.id=A.ID where Q.id=1
Danish HabibRank #2,024 · 715 reputationPosted Dec 30, 2014, 2:31 AM EST
Danish HabibRank #2,024 · 715 reputationPosted Dec 30, 2014, 2:31 AM EST
Joginder BangerRank #360 · 7K reputationPosted Dec 30, 2014, 2:41 AM EST
Joginder BangerRank #360 · 7K reputationPosted Dec 30, 2014, 2:49 AM EST
create table #tempquestion (id int,question_respondid int)
create table #answertable (id int, answer_respone int)
insert into #tempquestion (id,question_respondid)values(1,20),(2,21)
insert into #answertable(id,answer_respone)values(1,12)
select Q.id,Q.question_respondid,A.answer_respone from #tempquestion Q
left join #answertable A on q.id=A.id
Joginder BangerRank #360 · 7K reputationPosted Dec 30, 2014, 2:50 AM EST
Danish HabibRank #2,024 · 715 reputationPosted Dec 30, 2014, 8:00 AM EST