0

So my table is called prereqTable and looks something like

JobID   prereq
job254  job111
job345  job254
job432  job345

I need to find a query that returns all prereq for a job. Example: If I want prereqs for job432 they would be: job111, job254, job345

so prereq where jobid = "job432" is job345.. I need to somehow query job 345, then job254, and stop when there are no prereqs left.

Any ideas???

Adam Martin
  • 1,188
  • 1
  • 11
  • 24
user1726845
  • 109
  • 3
  • 10
  • What database server are you using? The query can be significantly different for SQL Server and MySQL, for example. – zedfoxus Oct 06 '15 at 17:34
  • I am using sql server – user1726845 Oct 06 '15 at 17:35
  • Cross join? That would return all prereq jobs with a JobID if thats what you are looking for. – Hozikimaru Oct 06 '15 at 17:38
  • That's a recursive query. This article might help : https://technet.microsoft.com/en-us/library/ms186243.aspx – Dave Clausen Oct 06 '15 at 17:45
  • Not sure if a cross join would really achieve what I'm doing, the recursive query sounds good, i'm familiar with recursion.. just not in sql, I guess I'll read up on recursive cte unless anyone else can think of an easier solution. – user1726845 Oct 06 '15 at 17:57
  • This is not a cross join, this is a recursive query and this has been answered literally 100s of times. Vote to close. – JonH Oct 06 '15 at 18:03
  • Agreed. @user1726845, it is wise to take a look at JonH's link and adjust it to your needs. Please feel free to close this question and open a new question with details if you have troubles adapting the query. – zedfoxus Oct 06 '15 at 19:43

0 Answers0