Hello,
here's my code:
CREATE TABLE [dbo].[test1](
[ID] [int] IDENTITY(1,1) NOT NULL,
[ParentID] [int] NULL,
[ClassCode] [int] NULL,
[ClassValue] [int] NULL
)
INSERT INTO test1 (ParentID,ClassCode,ClassValue) VALUES (100,1,0)
INSERT INTO test1 (ParentID,ClassCode,ClassValue) VALUES (100,2,0)
INSERT INTO test1 (ParentID,ClassCode,ClassValue) VALUES (100,3,0)
INSERT INTO test1 (ParentID,ClassCode,ClassValue) VALUES (100,4,0)
INSERT INTO test1 (ParentID,ClassCode,ClassValue) VALUES (100,5,0)
INSERT INTO test1 (ParentID,ClassCode,ClassValue) VALUES (100,6,0)
DECLARE @ParentID int
SET @ParentID=100
SELECT
'A' as [@n],
(SELECT
ClassCode [@n],
(SELECT
'C' as [c/@n],ClassValue AS [c/v]
FROM test1 t1
WHERE t1.ParentID=@ParentID and t1.ID=t2.ID and t1.ParentID=t2.ParentID
FOR XML PATH(''),TYPE
)
FROM test1 t2
WHERE t2.ParentID=@ParentIDORDER BY t2.ClassCode
FOR XML PATH('cat'),TYPE
)
FROM test1 t3
WHERE t3.ParentID=@ParentID
GROUP BY ClassCode
FOR XML PATH('grp'),TYPE
The problem in my code is that I get the grp n='A' node 6 times instead of 1.
Any kind of help would be appreciated.
Thank you in advance.