June 25, 2011 at 3:34 am
I am attaching the table structure here as a doc. I am looking for a query that takes in WOId as an input and returns all the prevWOIds records. I mean to say that the result set shall return the rows that have history data. But problem I am facing is that for the first entry in the table, the prevWOId is NULL.
There is a self foreign key constraint on the table 's WOID column. As a result I am unable to insert any non null value in the prevWOId column in the first row.
Can someone help me with the query. Please find the expected result set and the table structure in the file attached.
June 25, 2011 at 3:57 am
Please have a look at the first link in my signature on how to provide sample data.
In your Excel file the data types per column are missing as well as such simple things like a table name. The data are not ready to use, so we'd have to invest the time to setup a test scenario instead of immediately start working on a solution.
Also, please clarify what the PrevWOId column refers to. There is no other numeric field in the table.
You could also have a look at BOL (BooksOnLine, the SQL Server help system usually installed together with SQL Server), section "recursive CTEs".
June 25, 2011 at 4:07 am
Sorry for the missing info. FYI all 3 columns are nvarchar(max). Table name is WODetails.
I am re attaching the sheet with updated info.
June 25, 2011 at 4:14 am
Seems like you didn't really found the time to read the article I pointed you at...
The data are still not ready to use.
I'd also question the concept of having all columns being varchar(max). Do you really expect the value in WOId to everexceed 8000 character? Or even 100? Or 20? I strongly recommend to fix the design.
June 27, 2011 at 4:37 am
I was able to find a solution for the problem and I am posting the same for the benefit of others:
http://msdn.microsoft.com/en-us/library/ms175972%28SQL.90%29.aspx
Look under section J of the article. This shall help you write a recursive CTE .
July 22, 2011 at 4:20 am
Thanks for posting the feedback for the solution you found.
As a side bar, it's a real shame that you didn't post data using the method that Lutz suggested... it's likely that someone would have taken more interest in your post and probably would have posted a tested solution.
Consider posting some readily consumable data in a manner outlined in the article Lutz provided the link for, for your next post.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply