原数据: 年份 类型 
2009 小孩 
2009 老人 
2007 小孩 
2008 成年人 
2006 老人 
2005 老人 
2007 小孩 
2006 成年人 
想统计成: 
年份 小孩 成年人 老人 并且.年份不是统计当年的.比如2009.统计出来的是2009以下的和.也就是2005+2006+2007+2008+2009才是2009的. 
2008统计的是2005+2006+2007+2008,以此类推... -------------------------以下可以解决-----------------------但又有新问题了---------------------
---测试数据---
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([年份] int,[类型] varchar(6))
insert [tb]
select 2009,'小孩' union all
select 2009,'老人' union all
select 2007,'小孩' union all
select 2008,'成年人' union all
select 2006,'老人' union all
select 2005,'老人' union all
select 2007,'小孩' union all
select 2006,'成年人'
 
---查询---
select 
年份,
小孩=(select count(1) from tb where 类型='小孩' and 年份<=t.年份),
成年人=(select count(1) from tb where 类型='成年人' and 年份<=t.年份), 
老人=(select count(1) from tb where 类型='老人' and 年份<=t.年份) 
from tb t
group by 年份---结果---
年份          小孩          成年人         老人          
----------- ----------- ----------- ----------- 
2005        0           0           1
2006        0           1           2
2007        2           1           2
2008        2           2           2
2009        3           2           3(所影响的行数为 5 行)
新问题::::::::::::::::::::::数据是::
时间       类型 
2009-01-01 小孩 
2009-02-02 老人 
2007-09-12 小孩 
2008-12-12 成年人 
2006-11-12 老人 
2005-12-12 老人 
2007-05-06 小孩 
2006-04-03 成年人 呵呵.其他要求和上面要求一样...求解.

解决方案 »

  1.   

    select 
    YEAR(时间)年份, 
    小孩=(select count(1) from tb where 类型='小孩' and 年份 <=t.年份), 
    成年人=(select count(1) from tb where 类型='成年人' and 年份 <=t.年份), 
    老人=(select count(1) from tb where 类型='老人' and 年份 <=t.年份) 
    from tb t 
    group by YEAR(时间)
      

  2.   

    ---测试数据---
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([时间] datetime,[类型] varchar(6))
    insert [tb]
    select '2009-01-01','小孩' union all
    select '2009-02-02','老人' union all
    select '2007-09-12','小孩' union all
    select '2008-12-12','成年人' union all
    select '2006-11-12','老人' union all
    select '2005-12-12','老人' union all
    select '2007-05-06','小孩' union all
    select '2006-04-03','成年人'
     
    ---查询---
    select distinct
    year(时间) as 年份,
    小孩=(select count(1) from tb where 类型='小孩' and datediff(yy,时间,t.时间)>=0),
    成年人=(select count(1) from tb where 类型='成年人' and datediff(yy,时间,t.时间)>=0),
    老人=(select count(1) from tb where 类型='老人' and datediff(yy,时间,t.时间)>=0)
    from tb t  ---结果---
    年份          小孩          成年人         老人          
    ----------- ----------- ----------- ----------- 
    2005        0           0           1
    2006        0           1           2
    2007        2           1           2
    2008        2           2           2
    2009        3           2           3(所影响的行数为 5 行)
      

  3.   

    ----------------------------------------------------------------
    -- Author  :SQL77(只为思齐老)
    -- Date    :2010-02-25 17:33:10
    -- Version:
    --      Microsoft SQL Server  2000 - 8.00.194 (Intel X86) 
    -- Aug  6 2000 00:57:48 
    -- Copyright (c) 1988-2000 Microsoft Corporation
    -- Desktop Engine on Windows NT 5.1 (Build 2600: Service Pack 3)
    --
    ----------------------------------------------------------------
    --> 测试数据:#TB
    if object_id('tempdb.dbo.#TB') is not null drop table #TB
    go 
    create table #TB([时间] datetime,[类型] varchar(6))
    insert #TB
    select '2009-01-01','小孩' union all
    select '2009-02-02','老人' union all
    select '2007-09-12','小孩' union all
    select '2008-12-12','成年人' union all
    select '2006-11-12','老人' union all
    select '2005-12-12','老人' union all
    select '2007-05-06','小孩' union all
    select '2006-04-03','成年人'
    --------------开始查询-------------------------
    select 
    YEAR(时间)年份, 
    小孩=SUM(CASE WHEN  类型='小孩' and YEAR(时间) <=YEAR(T.时间) THEN 1 ELSE 0 END), 
    成年人=SUM(CASE WHEN 类型='成年人' and YEAR(时间) <=YEAR(T.时间) THEN 1 ELSE 0 END), 
    老人=SUM(CASE WHEN  类型='老人' and YEAR(时间) <=YEAR(T.时间) THEN 1 ELSE 0 END) 
    from #TB T
    group by YEAR(时间)
    ----------------结果----------------------------
    /* (所影响的行数为 8 行)年份          小孩          成年人         老人          
    ----------- ----------- ----------- ----------- 
    2005        0           0           1
    2006        0           1           1
    2007        2           0           0
    2008        0           1           0
    2009        1           0           1(所影响的行数为 5 行)
    */
      

  4.   

    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([年份] int,[类型] varchar(6))
    insert [tb]
    select 2009,'小孩' union all
    select 2009,'老人' union all
    select 2007,'小孩' union all
    select 2008,'成年人' union all
    select 2006,'老人' union all
    select 2005,'老人' union all
    select 2007,'小孩' union all
    select 2006,'成年人'select 年份,
    isnull((select sum(1) from tb where 类型='小孩' and 年份<=t.年份),0) '小孩',
    isnull((select sum(1) from tb where 类型='成年人' and 年份<=t.年份),0) '成年人',
    (select sum(1) from tb where 类型='老人' and 年份<=t.年份) '老人'
    from tb t group by 年份年份          小孩          成年人         老人
    ----------- ----------- ----------- -----------
    2005        0           0           1
    2006        0           1           2
    2007        2           1           2
    2008        2           2           2
    2009        3           2           3(5 行受影响)
      

  5.   

    select 
    year(时间), 
    小孩=sum(case when 类型='小孩' and year(时间) <=year(t.时间) then 1 else 0 end), 
    成年人=sum(case when 类型='成年人' and year(时间) <=year(t.时间) then 1 else 0 end), 
    老人=sum(case when 类型='老人' and year(时间) <=year(t.时间) then 1 else 0 end) 
    from ta t 
    group by year(时间)