具有复杂case-when语句的SQL查询

时间:2017-06-14 07:13:25

标签: sql hive hiveql

我有一个包含以下架构的表

<Icon name="trash" className="trash-icon" onClick={() =>
  console.log(this.props.movies[i].id),
  this.props.onMovieClick(this.props.movies[i].id)
}/>

我想做点什么

id data rank usage 1 hi 19 H 2 he 14 M 3 me 9 L 4 ke 23 H 5 jo 11 M 6 km 2 L 7 lo 4 L 8 no 12 M 9 my 20 H

SQL不支持如下语句:

If max(rank) where usage='L'>=25 Then Select all records where usage='H' Else If max(rank) where usage='M'>=25 Select all records where usage='L' and records where usage='M' ELSE Select all records

那么,我应该怎么写这个查询呢?

2 个答案:

答案 0 :(得分:0)

select  id,data,rank,usage

from   (select  *
               ,max(case usage when 'L' then rank end) over () max_rank_L   
               ,max(case usage when 'M' then rank end) over () max_rank_M

        from    mytable 
        ) t

where   usage rlike case
                        when max_rank_L >= 25 then '^(H)$'
                        when max_rank_M >= 25 then '^(L|M)$'
                        else                       '()'
                    end

答案 1 :(得分:-1)

您可以在过滤器中应用逻辑。此查询应该为您提供所需内容:

AdPhoto