zoukankan      html  css  js  c++  java
  • SQL解决"双重职位的查询"

    双重身份问题:

    create table role_tab (
    person char(5) not null,
    role  char(1) not null
    )
    insert into role_tab values('smith','O'),('smith','D'),('niu','O'),('jia','O'),('yong','O'),('yong','D')
    O代表高级职员,D代表主管,
    问题:现查询职员,如果是双重身份的则显示B,
    此题一看到就想到用union,和count(*)做。除了union外呢
    解惑1:
    select r.person,case when count(r.rol)=1 then  max(r.rol) else 'B'  end from role_tab r group by r.person
    解惑2:
    select person,substring('DOB' from sum(position(rol in 'DO')) for 1)  from role_tab  where role_tab where rol in ('O','D') group by person
  • 相关阅读:
    STL与泛型编程-练习2-GeekBand
    HashSet
    JAVA集合
    分布式锁1 Java常用技术方案
    JAVA 锁
    JAVA多线程二
    JAVA多线程一
    Redis pipeline and list
    mongo 安装
    Intersection of Two Arrays
  • 原文地址:https://www.cnblogs.com/vansky/p/8900273.html
Copyright © 2011-2022 走看看