Hi guys. I'm fairly new to SQL, but learning quickly. I've got a situation where I need to query a list of jobs, select some that meet a specific criteria (easy) but I need to also select some other's that would be related to these (don't know how).Here's some sample data to illustrate:Source table:[code="sql"]create table projects(ProjectNo nvarchar (20),ProjectType nvarchar(10),Location nvarchar (10),)insert into projects (ProjectNo, ProjectType, Location) values ('abc123', 'new', '111');insert into projects (ProjectNo, ProjectType, Location) values ('abc124', 'std', '222');insert into projects (ProjectNo, ProjectType, Location) values ('abc125', 'new', '333');insert into projects (ProjectNo, ProjectType, Location) values ('abc126', 'std', '111');insert into projects (ProjectNo, ProjectType, Location) values ('abc127', 'std', '111');insert into projects (ProjectNo, ProjectType, Location) values ('abc128', 'std', '666');insert into projects (ProjectNo, ProjectType, Location) values ('abc129', 'std', '777');insert into projects (ProjectNo, ProjectType, Location) values ('abc134', 'std', '1332');insert into projects (ProjectNo, ProjectType, Location) values ('abc135', 'new', '1443');insert into projects (ProjectNo, ProjectType, Location) values ('abc136', 'new', '1554');insert into projects (ProjectNo, ProjectType, Location) values ('abc137', 'new', '1665');insert into projects (ProjectNo, ProjectType, Location) values ('abc138', 'std', '1776');insert into projects (ProjectNo, ProjectType, Location) values ('abc139', 'std', '1554');insert into projects (ProjectNo, ProjectType, Location) values ('abc140', 'std', '1998');insert into projects (ProjectNo, ProjectType, Location) values ('abc141', 'std', '1554');insert into projects (ProjectNo, ProjectType, Location) values ('abc142', 'std', '2220');[/code]I need to query the rows that have "new" as the project type. Easy. But for each one of the rows with ProjectType = "new" I also need to append any row that has a matching value in the "Location" field. Call these "children". So for project abc123, at location 111 with ProjectType="new", there are 2 other records tha are "children" of this record. abc126 and abc127.I'd like the result of my query to look like this. Each parent retrieved, along with any related "child" records. (Order is not important, I grouped by location and I added blank lines for readability)[code]Project ProjectType Locationabc123 new 111abc126 std 111abc127 std 111abc125 new 333abc135 new 1443abc136 new 1554abc139 std 1554abc141 std 1554abc137 new 1665[/code]Can someone help me with this query?Thanks very much.Scott
↧