2017-11-28 5 views
0

最低値を取る: は、それぞれのproduct_idのために、少なくとも数は、各レベル分が空白除外と私はここにサンプルデータを持っている

期待される結果である何が必要なのさ

= 10093 3 183 184 185 

しかし、私は、同じ製品IDの複数の行を有するので、それはレベル4

Click here for example dataset- Product in different levels

select 
cc.product_id, 
substring_index(substring_index(concat(cc.path, '/////'), '/', 2), '/', -1) as level_2, 
substring_index(substring_index(concat(cc.path, '/////'), '/', 3), '/', -1) as level_3, 
substring_index(substring_index(concat(cc.path, '/////'), '/', 4), '/', -1) as level_4, 
substring_index(substring_index(concat(cc.path, '/////'), '/', 5), '/', -1) as level_5 

from 
cc 
184上にブランク選択あなたが 0値をフィルタリングするために集約する際

答えて

0

あなたはcase使用することができます。

select product_id, 
     min(case when level_2 + 0 > 0 then level_2 + 0 end) as min_level_2, 
     min(case when level_3 + 0 > 0 then level_3 + 0 end) as min_level_3, 
     min(case when level_4 + 0 > 0 then level_4 + 0 end) as min_level_4, 
     min(case when level_5 + 0 > 0 then level_5 + 0 end) as min_level_5 
from (select cc.product_id, 
      substring_index(substring_index(concat(cc.path, '/////'), '/', 2), '/', -1) as level_2, 
      substring_index(substring_index(concat(cc.path, '/////'), '/', 3), '/', -1) as level_3, 
      substring_index(substring_index(concat(cc.path, '/////'), '/', 4), '/', -1) as level_4, 
      substring_index(substring_index(concat(cc.path, '/////'), '/', 5), '/', -1) as level_5 
     from cc 
    ) cc 
group by product_id; 

注:またwhen level_2 <> ''などの条件を書くことができます。

関連する問題