select rowid = identity(int,1,1),数量,年份 into #temp from table

解决方案 »

  1.   

    select count(1) as 总数 ,'2005' as 年份
    from 基本表
    where ((datediff(year,cssj,'2005-01-01')=60) and (xb='男')) or
          ((datediff(year,cssj,'2005-01-01')=55) and (xb='女'))
    union all
    select count(1) as 总数,'2006' as 年份
    from 基本表
    where ((datediff(year,cssj,'2005-01-01') between 60 and 61) and (xb='男')) or
          ((datediff(year,cssj,'2005-01-01') between 55 and 56) and (xb='女'))
    union all
    select count(1) as 总数,'2007' as 年份
    from 基本表
    where ((datediff(year,cssj,'2005-01-01') between 60 and 62) and (xb='男')) or
          ((datediff(year,cssj,'2005-01-01') between 55 and 57) and (xb='女'))
      

  2.   

    declare @t table(sl int,nf int)
    insert @t select 120,2005 union all
    select 190,2006 union all 
    select 240,2007 select rowid = identity(int,1,1),sl,nf into #temp from @t
    select * from #temp
    select aa.sl - b.sl2 ,aa.nf,aa.rowid from #temp aa,
    (select isnull(sl,0) as sl2,rowid from #temp   ) b 
    where aa.rowid *= b.rowid +1drop table #temp/*            nf          rowid       
    ----------- ----------- ----------- 
    120         2005        1
    70          2006        2
    50          2007        3*/
      

  3.   

    create table #t (a int ,b int)insert into #t values(120,2005)
    insert into #t values(70,2006)
    insert into #t values(50,2007)
    select isnull((select sum(a) from #t where b<tt.b),(select min(a) from #t)),b
    from #t tt
    order by b