sql - Reverse the order in the recursive query that gets all parents of an item -


here query: link sqlfiddle

with fruit_tree  (    select [fruit_id], [parent_fruit]    ,cast(fruit_name varchar(max)) tree    fruit    [fruit_id] = 'b03'    union    select c.[fruit_id], c.[parent_fruit]    ,cast(tree + ',' + cast(c.fruit_name varchar(max)) varchar(max))    fruit c         join fruit_tree p on c.[fruit_id] = p.[parent_fruit]     , c.[fruit_id]<>c.[parent_fruit]  ) select * fruit_tree [parent_fruit] = '0' 

above query gives output as:

fruit_id    parent_fruit    tree fr03        0               golder beauty,banana 

but need output as:

fruit_id    parent_fruit    tree fr03        0               banana,golder beauty 

how achieve this? link sqlfiddle

if want reverse order in third column of output, change this:

,cast(tree + ',' + cast(c.fruit_name varchar(max)) varchar(max)) 

to:

,cast(cast(c.fruit_name varchar(max)) + ',' + tree varchar(max)) 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -