所以我编写了一个XQuery来查明每个名称在我的XML文档中出现的次数。我可以得到结果,但每个名称都在输出处重复。谁也看不出有什么问题?非常感谢您的帮助。顺便说一句,我正在使用IBM DB2。
代码:
XQUERY
for $tmp in db2-fn:xmlcolumn('STARMA.STARMA_XML')
for $value in distinct-values($tmp//Star_Chef/Name)
let $count := count($tmp//Star_Chef[Name eq $value])
order by $count descending
return concat($value," ",$count);
输出:
Gary Lineker 6
Gary Lineker 6
Jamie Oliver 5
Jamie Oliver 5
Gordon Ramsey 4
Amir Khan 4
Gordon Ramsey 4
Amir Khan 4
Edwin Tye 3
Megan Fox 3
Edwin Tye 3
Megan Fox 3
Ellie Dee 2
Ellie Dee 2
Andy Lau 1
Andy Lau 1
16 record(s) selected.
答案 0 :(得分:0)
这是因为你的嵌套循环。
使用
let $tmp := db2-fn:xmlcolumn('STARMA.STARMA_XML')
或者单身:
for $value in distinct-values(db2-fn:xmlcolumn('STARMA.STARMA_XML'))
let $count := count($tmp//Star_Chef[Name eq $value])
order by $count descending
return concat($value," ",$count);