表的结构是:
date           shift    
2011-01-20      甲班        
2011-01-20      乙班        
2011-01-20      丙班        
2011-01-20      丁班   
2011-01-21      甲班
2011-01-21      乙班
我想查询出某天却少哪个班,
比如我查询2011-01-21 的shift 应该是缺少 丙班和丁班
应该怎么查询

解决方案 »

  1.   


    with A as(
         select '甲' shift from dual union all
         select '乙' shift from dual union all
         select '丙' shift from dual union all
         select '丁' shift from dual
    ),
    B as(
         select '2011-01-20' dates from dual union all
         select '2011-01-21' dates from dual 
    ),
    C as(
         select '2011-01-20' dates,'甲' shift from dual union all
         select '2011-01-20' dates,'乙' shift from dual union all
         select '2011-01-20' dates,'丙' shift from dual union all
         select '2011-01-20' dates,'丁' shift from dual union all
         select '2011-01-21' dates,'甲' shift from dual union all
         select '2011-01-21' dates,'乙' shift from dual 
    )select B.dates,A.shift
    from A,B
    minus 
    select C.dates,C.shift 
    from C
    再按照日期条件  重构建下B表就好了
      

  2.   


    这是传说中的CTE(common table expression),和minus的使用吧!
      

  3.   

    建立一张表B,数据结构是
    shift



    丁然后
    select * from 表B where shift not in(select shift from 表A where date=你需要查的日期) 
      

  4.   

    我感觉思路应该是先看是不是四条,若不是四条再逐个看是少哪个;若是四条就是不少了。
    看是不是四条时用 select count(shift) from table_name group by date; 把不是四样的date记录下来,再以这个date为条件查具体是少哪个。
    新手,不知道对不对
      

  5.   

    四楼的想法挺有创意 
    但是有现成的minus 为啥不利用呢
      

  6.   

      sql语句改进下:
      select * from 表B where shift not in(select shift from 表A where date=你需要查的日期 group by shift)
      即可