select aa, '20031001',sum(cc) as cc,sum(dd) as dd
from 表
where cast(bb as datetime) < '2003-10-01'
group by aa
union
select *
from 表
where cast(bb as datetime) betwwen '2003-10-01' and '2003-10-05'

解决方案 »

  1.   

    select aa, '20031001之前',sum(cc) as cc,sum(dd) as dd
    from 表
    where cast(bb as datetime) < '2003-10-01'
    group by aa
    union
    select *
    from 表
    where cast(bb as datetime) betwwen '2003-10-01' and '2003-10-05'
      

  2.   

    select * from 表 where bb between   '20031005' and '20031001'select aa,sum(cc) as c,sum(dd) as d from 表 where bb<'20031001' group by aa
      

  3.   

    select aa,bb,cc,sum(cc),sum(dd) from test where bb BETWEEN '20031001' and '20031005'  group by aa,bb,cc
    union all
    select aa,bb,sum(cc),sum(dd)
    from test and bb <'20031001' group by aa,bb
      

  4.   

    select aa,'20031001之前' bb, sum(cc) cc,sum(dd) from 表 group by aa
    union all
    select aa,bb,cc,dd from 表 where cast(bb as datetime) between '20031001' and '20031005'
      

  5.   

    select aa,'20031001之前' bb, sum(cc) cc,sum(dd) from 表 where cast(bb as datetime) < '20031001' group by aa
    union all
    select aa,bb,cc,dd from 表 where cast(bb as datetime) between '20031001' and '20031005'
      

  6.   

    如果时间段内的也要分组就用这个:如果不要就是上面的select aa,'期初' bb,sum(cc) cc,sum(dd) from 表 where cast(bb as datetime) < '20031001' group by aa
    union all
    select aa,'期中',sum(cc) cc,sum(dd) from 表 where cast(bb as datetime) between '20031001' and '20031005' group by aa
      

  7.   

    select aa,bb,cc,dd from test where bb BETWEEN '20031001' and '20031005' 
    union all
    select aa,max(bb) bb,sum(cc) cc,sum(dd) dd
    from test and bb <'20031001' group by aa
      

  8.   

    如果還要選擇表中的其他字段呢,如表中還有 ee char(20) ff char(10) gg number(2),這些字段都要選上,出現在結果中,作為排序的條件。謝謝,分不夠可以再加!!
      

  9.   

    統計20031001之前的所有cc之和,所有dd之和,並且以aa分組那么这几个字段怎样取舍 ee char(20) ff char(10) gg number(2),
      

  10.   

    select * from (
    select aa,bb,cc,dd,ee,ff,gg from test where bb BETWEEN '20031001' and '20031005' 
    union all
    select aa,max(bb) bb,sum(cc) cc,sum(dd) dd,min(ee) ee,min(ff) ff,min(gg) ff
    from test and bb <'20031001' group by aa) a
    order by bb,.....
      

  11.   

    to caiyunxia(monkey) 
    楼主的ee 是char(20),不能使用min(ee)
      

  12.   

    认错的,char(20)类型也能用min()
      

  13.   

    --定义要查询的区间
    declare @bb1 char(10),@bb2 char(10)
    select @bb1='20031001',@bb2='20031005'--查询
    select aa,bb,cc,dd,ee,ff
    from(
    select sortid=1,* from 表 where bb between @bb1 and @bb2
    union all 
    select sortid=0,null,null,sum(cc),sum(dd),null,null from 表 where bb<@bb2
    ) a order by sortid,aa,bb
      

  14.   

    --上面没按aa分组,改一下:--定义要查询的区间
    declare @bb1 char(10),@bb2 char(10)
    select @bb1='20031001',@bb2='20031005'--查询
    select aa,bb,cc,dd,ee,ff
    from(
    select sortid=1,* from 表 where bb between @bb1 and @bb2
    union all 
    select sortid=0,aa,null,sum(cc),sum(dd),null,null from 表 where bb<@bb2 group by aa
    ) a order by aa,sortid,bb
      

  15.   

    --下面是测试--测试数据
    declare @表 table(aa char(10),bb char(10),cc float,dd float,ee char(20),ff char(10))
    insert into @表(aa,bb,cc,dd)
    select 'rrr','20031005',200.00,0
    union all select 'xxx','20031001',100.23,0
    union all select 'yyy','20030901',200.33,0
    union all select 'zzz','20030911',0,100--定义要查询的区间
    declare @bb1 char(10),@bb2 char(10)
    select @bb1='20031001',@bb2='20031005'--查询
    select aa,bb,cc,dd,ee,ff
    from(
    select sortid=1,* from @表 where bb between @bb1 and @bb2
    union all 
    select sortid=0,aa,null,sum(cc),sum(dd),null,null from @表 where bb<@bb2 group by aa
    ) a order by aa,sortid,bb
      

  16.   

    MAX
    返回表达式的最大值。语法
    MAX ( [ ALL | DISTINCT ] expression )expression常量、列名、函数以及算术运算符、按位运算符和字符串运算符的任意组合。MAX 可用于数字列、字符列和 datetime 列,但不能用于 bit 列。不允许使用聚合函数和子查询。
      

  17.   

    select aa,'期初' bb,sum(cc) cc,sum(dd) from 表 where cast(bb as datetime) < '20031001' group by aa
    union all
    select aa,'期中',sum(cc) cc,sum(dd) from 表 where cast(bb as datetime) between '20031001' and '20031005' group by aa
      

  18.   

    我是想達到這樣的效果,請大家看看這個網頁
    http://www.pdriver.com/bbs5/dispbbs.asp?boardID=14&ID=113894