-1
我有兩個表,如下所示。得到的孩子數和記錄列表
declare @Table1 table (ID int, ColName varchar(20), ParentID int)
insert into @Table1
select 1,'Rec1',-1
union
select 2,'Rec2',1
union
select 3,'Rec3',1
union
select 4,'Rec4',2
declare @Table2 table (ColID int, Name varchar(10),ID int)
insert into @Table2
select 10,'asdf',1
union
select 20,'atsdf',2
union
select 30,'asurdf',3
union
select 40,'ahfsdf',4
union
select 50,'aesdf',2
union
select 60,'asdsfgf',1
union
select 70,'asdfdf',3
我想顯示頂級父計數,並在相關記錄下,如父和子計數。 第二個表數據應該來下第一臺父ID,從第一個表,孩子和第二表記錄的數量,第三個孩子,孩子和第二從表中的相關記錄計數下一子..
ID Name Count
1 Rec1 6
10 asdf NULL
20 atsdf NULL
2 Rec2 3
50 aesdf NULL
60 asdsfgf NULL
如果沒有孩子記錄,你想顯示父母嗎? – Adish
是的,我必須顯示 –
爲什麼atsdf在Rec1下,並且aesdf在Rec2下時都有ID 2?數6是否正確,是不是7?輸出表是否完整?如果不是,你能提供完整的輸出嗎? –