try itselect count(subyearmonth) as countmonth,count(subjyear) as countyear,subjdbsc from   table group by subjdbsc order by id desc

解决方案 »

  1.   

    try itselect count(subyearmonth) as countmonth,count(subjyear) as countyear,subjdbsc from   table group by subjdbsc order by id desc
      

  2.   

    谢谢!!
    edobnet(oоОoоО) 
    这样不行的,统计出来的年数据和月数据一样了
    yohomonkey(关在笼子里的猴) 
    小弟不才,能不能给个例子 
      

  3.   

    加一个distinct试试
    select count(distinct subyearmonth) as countmonth,count(distinct subjyear) as countyear,subjdbsc from   table group by subjdbsc order by id desc
      

  4.   

    分两次写就可以了,通过table来合并
      

  5.   

    不解:
    countmonth countyear subjdbsc
    2              3        ww
    问题:2---是2003-07有2个还是有两种不同的月份,还是取相同月份的最大值可以用UNION实现!
      

  6.   

    上面的意思是按年,月统计记录数,以subjdbsc分组
    取完数据后生成统计图
      

  7.   

    上面的意思是按年,月统计记录数,以subjdbsc分组
    取完数据后生成统计图
      

  8.   

    哦!楼主意思是整个表的数据分别用年和月统计:
    安年:
    ww有3条,qq有两条
    安月:
    2003-07:
    ww有两条,qq就一条
    Right?
      

  9.   

    搞定:
    排序的你自己改改吧。
    if EXISTS( SELECT name,type FROM sysobjects
    where name='t'and type='U')
    begin
    drop table t
    end
    create table t ( countmonth nvarchar(50),countyear nvarchar(50),subjdbsc nvarchar(50))declare @subjdbsc nvarchar(50)declare @CounYM nvarchar(10)
    declare @CounY nvarchar(10)
    declare crColumns cursor for
    select distinct subjdbsc from table2
    open crColumns
    fetch next from crColumns into @subjdbsc 
    while (@@Fetch_status<>-1)
    begin
    if (@@Fetch_status<>-2)
    begin 
    select @CounYM= count(subyearmonth) from table2 where  subjdbsc=@subjdbsc and subyearmonth='2003-07'
    select @CounY=count(subyear) from table2 where subjdbsc=@subjdbsc and subyear='2003'
    insert into t (countmonth ,countyear,subjdbsc) 
    values(@CounYM,@CounY,@subjdbsc)
    end
    fetch next from crColumns into @subjdbsc 
    end
    close crColumns
    deallocate crColumnsselect * from t
      

  10.   

    楼上的办法看了都头晕!
    依稀记得这个应该属于矩阵转置之类的问题(PIVOT),具体方法还要回头查一查!
      

  11.   

    作一个存储过程,不知道你数据库是怎么设计的,我写了一个,大致是:
    DECLARE @year  AS char(4) 
    DECLARE @month AS char(7)
    --SET @year = '2003'
    --SET @month = '2003-07'
              SELECT SUM(CASE pyear WHEN @year THEN 1 ELSE 0 end )AS countpyear, 
                  SUM(CASE pmonth WHEN   @month THEN 1 ELSE 0 end) 
                  AS countmonth, puser
            FROM tt
            GROUP BY puser
            ORDER BY countpyear
      

  12.   

    ljj77(小妖) 的方法可行完整的存储过程:
    create procedure sp @ym varchar(7) = '2003-07'
    asselect subjdbsc, 
          sum(case when subyear = left(@ym,4) then 1 else 0 end) countyear, 
          sum(case when subyearmonth = @ym then 1 else 0 end) countyearmonth
    from talbename
    group by subjdbsc
      

  13.   

    刚才我的SQL SERVER突然运行不了:( 搞了很久重启机器就没事了,晕
    不知楼主想要的结果是否这样:
    id      title    subjdbsc subyear subyearmonth
    1 qw qq 2003 2003-06
    2 we ww 2003 2003-06
    3 rt qq 2003 2003-07
    4 ty ww 2003 2003-07
    5 er ww 2003 2003-07
    6 qw ww 2002 2003-07
    7 we ww 2002 2003-05统计的得到的结果:
    subjdbc countyear countmonth
    ww 2 3
    qq 1 2如果是的话可参考以下的SQL语句(直接COPY到SQL查询分析器运行即可)/*
    CREATE TABLE table1(
      [id] int,
      title varchar(10),
      subjdbsc varchar(10),
      subyear varchar(10),
      subyearmonth varchar(10)
    )INSERT table1([id],title, subjdbsc, subyear, subyearmonth) VALUES(1,'qw','qq','2003','2003-06')
    INSERT table1([id],title, subjdbsc, subyear, subyearmonth) VALUES(2,'we','ww','2003','2003-06')
    INSERT table1([id],title, subjdbsc, subyear, subyearmonth) VALUES(3,'rt','qq','2003','2003-07')
    INSERT table1([id],title, subjdbsc, subyear, subyearmonth) VALUES(4,'ty','ww','2003','2003-07')
    INSERT table1([id],title, subjdbsc, subyear, subyearmonth) VALUES(5,'er','ww','2003','2003-07')
    INSERT table1([id],title, subjdbsc, subyear, subyearmonth) VALUES(6,'qw','ww','2002','2003-07')
    INSERT table1([id],title, subjdbsc, subyear, subyearmonth) VALUES(7,'we','ww','2002','2003-05')
    */SELECT a.subjdbsc,
           (SELECT COUNT(*) FROM (SELECT subyear FROM table1 
                                  WHERE subjdbsc = a.subjdbsc 
                                  GROUP BY subyear) b) AS countyear,
           (SELECT COUNT(*) FROM (SELECT subyearmonth FROM table1 
                                  WHERE subjdbsc = a.subjdbsc 
                                  GROUP BY subyearmonth) c) AS countmonth
    FROM table1 a
    GROUP BY subjdbsc
    ORDER BY countmonth desc,countyear desc
      

  14.   

    参考大件的表结构和内容,创建一个函数(sqlserver2000),如下:
    create function stat(@year char(4), @month char(2))
    returns table
    as
      return
      select subjdbsc, 
        yearqty=(select count(1) from table1 where subyear=@year and subjdbsc=t.subjdbsc),
        monthqty=(select count(1) from table1 where subyearmonth=@year+'-'+@month and subjdbsc=t.subjdbsc)
      from table1 t group by subjdbsc查询语句:
    select * from dbo.stat('2003', '07')
      

  15.   

    补充:为了提高效率,上述函数的最后一行:from table1 t group by subjdbsc
    可以改为:
    from table1 t where subyear=@year group by subjdbsc
    以避免非计算非本年数据