1

I have 2 tables as follows (sample data shown):

TableName: A
ID              Type
1  Bug
2  Requirement
3  Task
4  Specification
5  Bug
6  Specification
7  Production Issue
8  Production Issue
9  Bug
10          Task

Tablename: B
ID  RelatedID
1  2
1  7
5  8
5  4
9  6
9  10

I want to fetch all the bugs that have atleast one related production issue or bugs that have no related production issue.

Expected output will be as shown below (since these are the bugs with at least one related production issue)

output
1
5
5
  • Not very clear what you are looking for, perhaps include an example of your expected output? Commented Feb 23, 2010 at 21:29
  • i would refine how your tables connect together, as it stands now it is not very clear, what is related id, why are the primary keys for table b repeated and not auto incremented? Commented Feb 23, 2010 at 21:34
  • This is just a join, it isn't recursive in any way. If you wanted to enumerate all directly- and indirectly-related issues, that would be recursive. Commented Feb 23, 2010 at 21:40
  • Why is 9 omitted? I'm really confused what your algorithm is here. All three bugs have one Production Issue and one other kind of issue. They all seem to be in the same boat. This isn't really very clear. Commented Feb 23, 2010 at 21:43
  • @David My bad. I wanted to place 6 against 9. Instead I placed 7. So in the above case output will have 9. Commented Feb 23, 2010 at 21:56

1 Answer 1

1

Aliases are the way to go here

SELECT pri.Type AS 'Primary Type', rel.Type AS 'Related Type' 
FROM A AS pri 
   INNER JOIN B ON B.ID = pri.ID
   INNER JOIN A AS rel ON B.RelatedID = rel.ID
WHERE pri.Type = 'Bug' AND rel.Type = 'Production Issue;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.