Need help with SQL query..

  • [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.

  • [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..

  • 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 - MCSomething someday:-)
    Right way to ask for help!!
    http://www.sqlservercentral.com/articles/Best+Practices/61537/
    I post so I can see my avatar :hehe:
    I want a personal webpage 😎
    I want to win the lotto 😀
    I want a gf like Tiffa :w00t: Oh wait I'm married!:-D

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply