Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Get objects depending on ("all in")

Author  Topic 

cok
Starting Member

1 Post

Posted - 2007-10-24 : 14:31:28
Hi,
I have a problem. I have questions, to a question there can be arbitrary groups and to every group there can be arbitrary conditions. To have permission to “see” an question you have to have at least one condition from every group which is in the question.

I have three tables: Question, QuestionGroup and GroupCondition.

Question
QuestionId | Bla | Bla
15 | as | asd
16 | ef | asdas

QuestionGroup
fkey_GroupID | fkey_QuestionID
1 | 15
2 | 15
3 | 16

GroupCondition
Pkey_GroupId | fkey_ConditionId
1 | 101
1 | 102
2 | 103
3 | 101
3 | 102
3 | 103

So I need a select which returns all QuestionId from Question depending on the conditions I have. So to get QuestionId (15) I need following: (101 or 102) and (103), to get acess to QuestionId 16 I need (101 or 102 or 103). I hope I have been clear enough.. :)

Best regards

carl

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2007-10-24 : 16:34:35
[code]DECLARE @Question TABLE (QuestionID INT)

INSERT @Question
SELECT 15
UNION ALL SELECT 16

DECLARE @QuestionGroup TABLE(GroupID INT, QuestionID INT)

INSERT @QuestionGroup
SELECT 1, 15
UNION ALL SELECT 2, 15
UNION ALL SELECT 3, 16

DECLARE @GroupCondition TABLE (GroupId INT, ConditionId INT)

INSERT @GroupCondition
SELECT 1, 101
UNION ALL SELECT 1, 102
UNION ALL SELECT 2, 103
UNION ALL SELECT 3, 101
UNION ALL SELECT 3, 102
UNION ALL SELECT 3, 103


SELECT
Condition.ConditionID
FROM
@GroupCondition AS Condition
INNER JOIN
@QuestionGroup AS QuestionGroup
ON Condition.GroupID = QuestionGroup.GroupID
WHERE
QuestionGroup.QuestionID = 15[/code]
Go to Top of Page
   

- Advertisement -