SQL-方法论

写SQL时可以考虑的手段:

  1. 行转列
    1. 先分为多个临时表,然后JOIN到一起
    2. select uid,
             t1.name YuWen,
             t2.name ShuXue
      from (select uid,
                   name
            from tableA
            where naem = '语文') t1
               join (select uid,
                            name
                     from tableA
                     where naem = '数学') t2
                    on t1.uid = t2.uid;
    3. 用sum(if())
    4. select uid,
             sum(if(name = '语文')) YuWen,
             sum(if(name = '数学')) ShuXue
      from tableA
      group by uid;
  2. 列转行
    1. 先分为多个临时表,然后UNION到一起
    2. select uid,
             YuWen
      from tableA
      
      union all
      
      select uid,
             ShuXue
      from tableA;

你可能感兴趣的:(Hive,sql,数据库,hive)