August 18, 2011 at 9:29 am
create Table TestTable
(
ParentId [varchar](1) NOT NULL,
ChildId [varchar](2) NOT NULL,
RecordType [varchar](1) NOT NULL,
)
INSERT INTO TestTable(ParentId,ChildId,RecordType) Values('1','1','A')
INSERT INTO TestTable(ParentId,ChildId,RecordType) Values('1','2','A')
INSERT INTO TestTable(ParentId,ChildId,RecordType) Values('1','3','I')
INSERT INTO TestTable(ParentId,ChildId,RecordType) Values('1','4','A')
INSERT INTO TestTable(ParentId,ChildId,RecordType) Values('2','5','A')
INSERT INTO TestTable(ParentId,ChildId,RecordType) Values('2','6','A')
INSERT INTO TestTable(ParentId,ChildId,RecordType) Values('2','7','A')
INSERT INTO TestTable(ParentId,ChildId,RecordType) Values('2','8','A')
INSERT INTO TestTable(ParentId,ChildId,RecordType) Values('3','9','A')
INSERT INTO TestTable(ParentId,ChildId,RecordType) Values('3','10','I')
INSERT INTO TestTable(ParentId,ChildId,RecordType) Values('4','11','A')
INSERT INTO TestTable(ParentId,ChildId,RecordType) Values('4','12','A')
Would like to create a query that satisfies below conditions
If any child record related to a parent record has a RecordType Of 'I'
I would like all the child records within the parent record flagged as 'Closed'
If all child records related to a parent record has a RecordType Of 'A'
I would like all the child records within the parent record flagged as 'Open'
In the example data above i want the result to look like below
ParentIdChildIdRecordTypeOpen_Closed
11AClosed
12AClosed
13IClosed
14AClosed
25AOpen
26AOpen
27AOpen
28AOpen
39AClosed
310IClosed
411AOpen
412AOpen
Parent Ids 1 and 3 have one child that has a record Type of 'I' so all the children are marked as 'Closed'
Any help is appreciated.
August 18, 2011 at 9:42 am
i think this does what you wanted;
i grabbed a subquery of the items that would be marked as closed....everything else would still be open, right?
SELECT
#TestTable.ParentId,
#TestTable.ChildId,
#TestTable.RecordType,
CASE
WHEN MyCloseItems.ParentId IS NULL
THEN 'Open'
ELSE 'Closed'
END AS Open_Closed
FROM #TestTable
LEFT OUTER JOIN
(SELECT
ParentId,RecordType
FROM #TestTable
WHERE RecordType = 'I'
GROUP BY ParentId,RecordType) AS MyCloseItems
ON #TestTable.ParentId = MyCloseItems.ParentId
Lowell
August 18, 2011 at 12:08 pm
Awesome. This was exactly what i was looking for. Thanks you!!!
August 18, 2011 at 9:05 pm
Ummm... so what do you want done if the following row is add to the rows you posted?
INSERT INTO TestTable(ParentId,ChildId,RecordType) Values('8','20','I')
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply