我试图通过sql查询获取父/子的结构。
我有这段代码:
state_code,description
A000
A010
B000
B010
B01A
B01B
B020
B02A
我想要一个像parent_id,element_id和desc
这样的结构这样:
A
A000
-01 (from A)
--A010
B
B000
-01
--B010
--B01A
-02
--
我尝试使用此代码,但遗憾的是它不起作用。
select distinct
SUBSTRING(REPLACE(state_cd,'0',''),1, LEN(REPLACE(state_cd, '0', '')) -1) AS parent_id, REPLACE(state_cd,'0','') state_id,
state_cd,
desc_1
from
CATEGORYSTRUCTURE
inner join
CATEGORYITEMS on CATEGORYSTRUCTURE.struct_id = CATEGORYITEMS.struct_id
left join
STATECODES on key_1 = state_cd
where
state_type = '22'
UNION ALL
select distinct
SUBSTRING(REPLACE(state_cd,'0',''),1, LEN(REPLACE(state_cd, '0', '')) -1) AS parent_id,
REPLACE(state_cd,'0','') state_id,
stat_cd,
desc_1
from
CATEGORYITEMS
inner join
STATECODES on key_1 = state_cd
where
state_type = '22'
我的结果:
parent_id stat_id stat_cd desc_1
O O000 11
P P000 Pa)
Q Q000 prod
R R000 Zus
S S000 Ver
T T000 Pack
A A1 A010 Get
A A1 A100 Kakt
A A3 A030 Kol
A A7 A070 Milk
A A8 A080 Spt
A1 A11 A110 Lo
A1 A1A A01A Hcht
我想要的是各个角色下的一切
A A7 A070 Milk
A A8 A080 Spt
A A11 A110 Lo
A1 A1A A01A Hcht
答案 0 :(得分:0)
我无法理解你想要什么,但我认为这个查询可以帮到你:
/ADSTART ACTION\(ADD\)/ { # for records where ADSTART occurs
print buf # output the buffer variable
buf="" # then empty the buffer
}
{ # for all records
# gsub(/^ +| +$/,"") # here you could trim leading and trailing space
buf=buf""$0" " # build the buffer
}
END { # in the end
print buf # output the remaining buffer
}