1
我那裏有父母與子女產品的數據。父子關係 - TSQL
每個父母得到了一個唯一的代碼(P1),並在他們的帳戶中的鏈接代碼(L1)和父母的每個孩子得到了一個單獨的代碼(C12),但它們共享相同的帳戶沒有。
我想指望家長和孩子
這裏是一個樣本數據集
create table #Something
(
Line_no int,
code varchar(5),
AccountNo char(5)
)
一些樣本數據
INSERT #Something
select 12311, 'P1c', 'Ac115' union all
select 12311, 'L1', 'Ac115' union all
select 123, 'C1', 'Ac115' union all
select 1222, 'C1', 'Ac115' union all
select 1243, 'C1', 'Ac115' union all
select 433, 'P1a', 'Ac111' union all
select 433, 'L1', 'Ac111' union all
select 4331, 'C1', 'Ac111' union all
select 543, 'C1', 'Ac222' union all
select 544, 'C1', 'Ac222' union all
select 4322, 'P1b', 'Ac222' union all
select 4322, 'L1', 'Ac222' union all
select 8766,'P1d' , 'Ab111' union all
select 8766,'L1' , 'Ab111' union all
select 8767,'C1', 'Ab111' union all
select 8789,'P1d', 'Ab119' union all
select 8766,'L1', 'Ab119' union all
select 876654,'C1', 'Ab119' union all
select 876655,'C1', 'Ab119' union all
select 876698,'P1a', 'Ab117' union all
select 876698,'L1', 'Ab117' union all
select 987,'C1', 'Ab117' union all
select 555444,'P1d','Xcv' union all
select 555444,'L1','Xcv' union all
select 6754,'C1','Xcv'
SELECT * from #Something
drop table #Something
所需的輸出是:
[Parent code] [Parent line Count] [Child line Count]
P1c 1 3
P1a 2 2
P1b 1 2
P1d 3 4
謝謝
你怎麼告訴'P..'是父母和'C..'是一個孩子? –