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

Danish HabibPosted Dec 30, 2014, 1:00 PM
Joginder BangerPosted Dec 30, 2014, 7:50 AM
Joginder BangerPosted Dec 30, 2014, 7:49 AM
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 BangerPosted Dec 30, 2014, 7:41 AM
Danish HabibPosted Dec 30, 2014, 7:31 AM
Danish HabibPosted Dec 30, 2014, 7:31 AM
Joginder BangerPosted Dec 30, 2014, 7:18 AM
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
Joginder BangerPosted Dec 30, 2014, 7:08 AM
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.
Danish HabibPosted Dec 30, 2014, 6:56 AM
Joginder BangerPosted Dec 30, 2014, 6:54 AM
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"
Joginder BangerPosted Dec 30, 2014, 6:47 AM
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.