我有一个表,如下所示,我现在要统计从2008年12月1日夜班到2008年12月7日的夜班中所有原料卷数的和,请问查询语句应该怎么写 id 日期 班次 班名 各班日历时间 原料卷数 成品卷数
14 2008-12-1 夜 甲 520 0 0
13 2008-12-1 白 乙 440 1 1
12 2008-12-1 中 丙 480 0 0
11 2008-12-2 夜 丁 520 0 0
10 2008-12-2 白 乙 440 0 0
9 2008-12-2 中 丙 480 0 0
8 2008-12-3 夜 丁 520 0 0
7 2008-12-3 白 甲 440 33 33
6 2008-12-3 中 乙 480 57 57
5 2008-12-4 夜 丙 520 82 82
3 2008-12-4 白 甲 440 28 28
4 2008-12-4 中 乙 480 69 69
23 2008-12-5 夜 丙 520 75 75
21 2008-12-5 白 丁 440 54 55
22 2008-12-5 中 甲 480 71 71
20 2008-12-6 夜 乙 520 89 89
19 2008-12-6 白 丁 440 49 49
18 2008-12-6 中 甲 480 44 44
17 2008-12-7 夜 乙 520 77 77
16 2008-12-7 白 丙 440 40 40

解决方案 »

  1.   


    1、从12.1到12.7 去掉12.1的白班,中班
    select sum(原料卷数),sum(原料卷数)-(select sum(原料卷数) from tb where 日期 = '2008-12-1' and  班次 in ('白','中'))
    from tb
    where 日期 between '2008-12-1' and '2008-12-7'2、是只求夜班的
    select sum(原料卷数)
    from tb
    where 日期 between '2008-12-1' and '2008-12-7' and 班次='夜'
    3、从12.1到12.7 去掉12.1的白班,中班、12.7的百班,中班select sum(原料卷数),sum(原料卷数)-(select sum(原料卷数) from tb where 日期 = '2008-12-1' and  班次 in ('白','中'))-(select sum(原料卷数) from tb where 日期 = '2008-12-7' and  班次 in ('白','中'))
    from tb
    where 日期 between '2008-12-1' and '2008-12-7' and 班次='夜'
      

  2.   

    --只求夜班?
    select sum(原料卷数) 原料卷数 from tb where 班次 = '夜' and 日期 between '2008-12-01' and '2008-12-07'--如果按班次分组求
    select 班次 , sum(原料卷数) 原料卷数 from tb where 日期 between '2008-12-01' and '2008-12-07' group by 班次--如果按班次,班名求
    select 班次 , 班名 , sum(原料卷数) 原料卷数 from tb where 日期 between '2008-12-01' and '2008-12-07' group by 班次 , 班名
      

  3.   

    select sum(原料卷数) 原料卷数 from tb where 班次 = '夜' and 日期 between '2008-12-01' and '2008-12-07' --如果按班次分组求 
    select 班次 , sum(原料卷数) 原料卷数 from tb where 日期 between '2008-12-01' and '2008-12-07' group by 班次 --如果按班次,班名求 
    select 班次 , 班名 , sum(原料卷数) 原料卷数 from tb where 日期 between '2008-12-01' and '2008-12-07' group by 班次 , 班名