Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Need help with SQL query.. Expand / Collapse
Author
Message
Posted Monday, February 18, 2013 1:38 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, February 19, 2013 11:31 AM
Points: 2, Visits: 32
[tbl_question] : questionId, question [tbl_answer] : answerId, questionId, answer, [tblTaskAnswer]: taskId, answerId table_task: taskId, name,....,

So in tbl_question there are two question ids lets say 1 and 2. For question 1 there are multiple answers in answer table also for question 2 there are multiple answers in tbl_answer. But each task has record for question id 1 and question Id 2. (so each task you get two answers in question 1 -> team and question 2 -> area)

Now I want to write a query to task details with answers in for both of these questions. example, under team (question 1) there are answers A,B,C,D and under Area (question 2) there are answers W,X,Y,Z

so I want to select tasks which comes under answer A,B and W,X.

I used in clause but it doesn't filter exactly.
Post #1421339
Posted Monday, February 18, 2013 2:00 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, February 19, 2013 11:31 AM
Points: 2, Visits: 32
[tbl_question] : questionId, question [tbl_answer] : answerId, questionId, answer, [tblTaskAnswer]: taskId, answerId table_task: taskId, name,....,

So in tbl_question there are two question ids lets say 1 and 2. For question 1 there are multiple answers in answer table also for question 2 there are multiple answers in tbl_answer. But each task has record for question id 1 and question Id 2. (so each task you get two answers in question 1 -> team and question 2 -> area)

Now I want to write a query to task details with answers in for both of these questions. example, under team (question 1) there are answers A,B,C,D and under Area (question 2) there are answers W,X,Y,Z

so I want to select tasks which comes under answer A,B and W,X.

I used in clause but it doesn't filter exactly.

select top 6 tbl_Answer.AnValue, tbl_taskAnswer.TaskID from tbl_TaskAnswer
INNER JOIN tbl_Answer on tbl_Answer.AnswerID = tbl_TaskAnswer.AnswerID
INNER JOIN tbl_Question on tbl_Answer.QuestionID = tbl_Question.QuestionID

AND tbl_Question.OrgID = 1532 AND tbl_Answer.AnIsActive = 1
order by TaskID DESC


A 835106
W 835106
A 833791
X 833791
B 833472
Z 833472

now I want answers A, B and Z tasks..
Post #1421347
Posted Monday, February 18, 2013 11:27 PM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Thursday, June 13, 2013 11:20 AM
Points: 225, Visits: 462
Please read the link below in my signature about good ways of asking for help. You're question is hard to understand and incomplete from what I can tell. It's easier to help if you provide consumable sample code.

I don't see an "in" clause much less a "where" clause... and where did the OrgID come from?
It helps to have the table definitions and sample data.

something like this would make it very easy for others to help. Everyone here is a volunteer and have full time jobs. I do this to enrich myself as well as helping other.
declare @Question as table (QuestionID int, Question varchar(50))
declare @Answer as table (QuestionID int, AnswerID int, Answer varchar(50))
declare @TaskAnswer as table (TaskID int, AnswerID int)
declare @Task as table(TaskID int, TaskName varchar(50))

insert into @Question(QuestionID, Question)
values(1, 'Team')
,(2,'Area')

insert into @Answer(QuestionID, AnswerID, Answer)
values(1,1,'A')
,(1,2,'B')
,(1,3,'C')
,(1,4,'D')
,(2,5,'W')
,(2,6,'X')
,(2,7,'Y')
,(2,8,'Z')

insert into @Task(TaskID, TaskName)
values (1,'SomeTask')
,(2,'OtherTask')
,(3,'LastTask')

insert into @TaskAnswer(TaskID, AnswerID)
values(1,1)
,(1,2)
,(2,3)
,(3,5)

select * from @Question
select * from @Answer
select * from @Task
select * from @TaskAnswer

select q.Question
,a.Answer
,t.TaskName
from @Question q
inner join @Answer a
on q.QuestionID = a.QuestionID
inner join @TaskAnswer ta
on a.AnswerID = ta.AnswerID
inner join @Task t
on t.TaskID = ta.TaskID



---------------------------------------------------------------
Mike Hahn - Future MCM 2025
Right way to ask for help!!
http://www.sqlservercentral.com/articles/Best+Practices/61537/
I post so I can see my avatar
I want a personal webpage
I want to win the lotto
I want a gf like Tiffa
Post #1421442
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse