我没法贴数据,几十万条,呵呵,只是让你看看我的在一定程度上的逻辑错在哪?问题呢是讲清了,结构呢就是上面的有unitno,roomno,isfinish,feeyear,feemonth,feename

解决方案 »

  1.   

    我要得到的结果集就是
    roomno    unitno  feeyear feemonth    isfinish
    1-101     1       2003    1            1
    1-101     1       2003    2            1
    1-101     1       2003    3            1
    1-102     1       2003    1            1
    1-102     1       2003    2            1
    1-102     1       2003    3            1
    1-103     1       2003    1            1
    1-103     1       2003    2            1
    1-103     1       2003    3            1
    1-101     1       2003    1            1
    2-101     2       2003    2            1
    2-101     2       2003    3            1
    2-102     2       2003    1            1
    2-102     2       2003    2            1
    2-102     2       2003    3            1
    2-103     2       2003    1            1
    2-103     2       2003    2            1
    2-103     2       2003    3            1
    3-101     3       2003    1            1
    3-101     3       2003    2            1
    3-101     3       2003    3            1
    3-102     3       2003    1            1
    3-102     3       2003    2            1
    3-102     3       2003    3            1
    3-103     3       2003    1            1
    3-103     3       2003    2            1
    3-103     3       2003    3            1
    这是三个楼里共九个住户的,2003年123月的数据,isfinish是钱交了没,交了为1,没交为0
    用那个存储过程是实现输入年份,季度,开始楼号,结束楼号,就要得到某用户的在这一季度的数据
    最后成为
    year   roomno   unitno   qua1  qua2 qua3
    2003   1-101    1         1    1    1
    2003   1-102    1         1    1    1
    2003   1-103    1         1    1    1
    2003   2-101    1         1    1    1
    2003   2-102    1         1    1    1
    2003   2-103    1         1    1    1
    2003   3-101    1         1    1    1
    2003   3-102    1         1    1    1
    2003   3-103    1         1    1    1
    可现在得到的是
    year   roomno   unitno   qua1  qua2 qua3
    2003   1-101    1         1    1    1
    2003   1-102    1         1    1    1
    2003   1-103    1         1    1    1
    2003   2-101    1         null    null    null
    2003   2-102    1         null    null    null
    2003   2-103    1         null    null    null
    2003   3-101    1         null    null    null
    2003   3-102    1         null    null    null
    2003   3-103    1         null    null    null
    如果要查的不是从1号楼开始的而是从2号楼开始的
    year   roomno   unitno   qua1  qua2 qua3
    2003   2-101    1         1    1    1
    2003   2-102    1         1    1    1
    2003   2-103    1         1    1    1
    2003   3-101    1         null    null    null
    2003   3-102    1         null    null    null
    2003   3-103    1         null    null    null我想不通怎么会这样?
      

  2.   

    看了半天,以为自己有点懂了,却又不懂,f1*f2*f3不知道干什么。
    如果从总体来说,特别是后面补充的数据,存储过程可以改写:
    用case when ...group by
    会简单很多。
      

  3.   

    create table 你的表(roomno varchar(10),unitno int,feeyear int,feemonth int,isfinish int)
    insert 你的表 values('1-101',     1       ,2003,    1,1)
    insert 你的表 values('1-101',     1       ,2003 ,   2,1)
    insert 你的表 values('1-101',     1       ,2003  ,  3,1)
    insert 你的表 values('1-102',     1      , 2003   , 1,1)
    insert 你的表 values('1-102',     1      , 2003    ,2,1)
    insert 你的表 values('1-102',     1     ,  2003    ,3,1)
    insert 你的表 values('1-103',     1    ,   2003    ,1,1)
    insert 你的表 values('1-103',     1   ,    2003    ,2,1)
    insert 你的表 values('1-103',     1  ,     2003    ,3,1)
    insert 你的表 values('1-101',     1 ,      2003    ,1,1)
    insert 你的表 values('2-101',     2,       2003    ,2,1)
    insert 你的表 values('2-101',     2       ,2003    ,3,1)
    insert 你的表 values('2-102',     2      , 2003    ,1,1)
    insert 你的表 values('2-102',     2     ,  2003    ,2,1)
    insert 你的表 values('2-102',     2    ,   2003    ,3,1)
    insert 你的表 values('2-103',     2   ,    2003    ,1,1)
    insert 你的表 values('2-103',     2  ,     2003    ,2,1)
    insert 你的表 values('2-103',     2 ,      2003    ,3,1)
    insert 你的表 values('3-101',     3,       2003    ,1,1)
    insert 你的表 values('3-101',     3       ,2003    ,2,1)
    insert 你的表 values('3-101',     3      , 2003    ,3,1)
    insert 你的表 values('3-102',     3       ,2003    ,1,1)
    insert 你的表 values('3-102',     3     ,  2003    ,2,1)
    insert 你的表 values('3-102',     3    ,   2003    ,3,1)
    insert 你的表 values('3-103',     3   ,    2003    ,1,1)
    insert 你的表 values('3-103',     3  ,     2003    ,2,1)
    insert 你的表 values('3-103',     3 ,      2003    ,3,1)declare @年 int,@季度 int,@开始层号 int,@结束层号 int
    select @年=2003,@季度=1,@开始层号=1,@结束层号=3select feeyear,roomno,unitno,
    sum(case when feemonth=@季度*3-2 then isfinish else 0 end) qua1,
    sum(case when feemonth=@季度*3-1 then isfinish else 0 end) qua2,
    sum(case when feemonth=@季度*3-0 then isfinish else 0 end) qua3
     from 你的表 where feeyear=@年 and feemonth between @季度*3-2 and @季度*3 and unitno between @开始层号 and @结束层号
    group by feeyear,roomno,unitnogodrop table 你的表
      

  4.   

    feeyear     roomno     unitno      qua1        qua2        qua3        
    ----------- ---------- ----------- ----------- ----------- ----------- 
    2003        1-101      1           2           1           1
    2003        1-102      1           1           1           1
    2003        1-103      1           1           1           1
    2003        2-101      2           0           1           1
    2003        2-102      2           1           1           1
    2003        2-103      2           1           1           1
    2003        3-101      3           1           1           1
    2003        3-102      3           1           1           1
    2003        3-103      3           1           1           1(所影响的行数为 9 行)
      

  5.   

    feeyear     roomno     unitno      qua1        qua2        qua3        
    ----------- ---------- ----------- ----------- ----------- ----------- 
    2003        1-101      1           2           1           1
    2003        1-102      1           1           1           1
    2003        1-103      1           1           1           1
    2003        2-101      2           0           1           1
    2003        2-102      2           1           1           1
    2003        2-103      2           1           1           1
    2003        3-101      3           1           1           1
    2003        3-102      3           1           1           1
    2003        3-103      3           1           1           1(所影响的行数为 9 行)
      

  6.   

    create table 你的表(roomno varchar(10),unitno int,feeyear int,feemonth int,isfinish int)
    insert 你的表 values('1-101',     1       ,2003,    1,1)
    insert 你的表 values('1-101',     1       ,2003 ,   2,1)
    insert 你的表 values('1-101',     1       ,2003  ,  3,1)
    insert 你的表 values('1-102',     1      , 2003   , 1,1)
    insert 你的表 values('1-102',     1      , 2003    ,2,1)
    insert 你的表 values('1-102',     1     ,  2003    ,3,1)
    insert 你的表 values('1-103',     1    ,   2003    ,1,1)
    insert 你的表 values('1-103',     1   ,    2003    ,2,1)
    insert 你的表 values('1-103',     1  ,     2003    ,3,1)
    insert 你的表 values('2-101',     2 ,      2003    ,1,1)
    insert 你的表 values('2-101',     2,       2003    ,2,1)
    insert 你的表 values('2-101',     2       ,2003    ,3,1)
    insert 你的表 values('2-102',     2      , 2003    ,1,1)
    insert 你的表 values('2-102',     2     ,  2003    ,2,1)
    insert 你的表 values('2-102',     2    ,   2003    ,3,1)
    insert 你的表 values('2-103',     2   ,    2003    ,1,1)
    insert 你的表 values('2-103',     2  ,     2003    ,2,1)
    insert 你的表 values('2-103',     2 ,      2003    ,3,1)
    insert 你的表 values('3-101',     3,       2003    ,1,1)
    insert 你的表 values('3-101',     3       ,2003    ,2,1)
    insert 你的表 values('3-101',     3      , 2003    ,3,1)
    insert 你的表 values('3-102',     3       ,2003    ,1,1)
    insert 你的表 values('3-102',     3     ,  2003    ,2,1)
    insert 你的表 values('3-102',     3    ,   2003    ,3,1)
    insert 你的表 values('3-103',     3   ,    2003    ,1,1)
    insert 你的表 values('3-103',     3  ,     2003    ,2,1)
    insert 你的表 values('3-103',     3 ,      2003    ,3,1)declare @年 int,@季度 int,@开始层号 int,@结束层号 int
    select @年=2003,@季度=1,@开始层号=1,@结束层号=3select feeyear,roomno,unitno,
    sum(case when feemonth=@季度*3-2 then isfinish else 0 end) qua1,
    sum(case when feemonth=@季度*3-1 then isfinish else 0 end) qua2,
    sum(case when feemonth=@季度*3-0 then isfinish else 0 end) qua3
     from 你的表 where feeyear=@年 and feemonth between @季度*3-2 and @季度*3 and unitno between @开始层号 and @结束层号
    group by feeyear,roomno,unitnogodrop table 你的表
    --------------------------------------------------
    结果:
    feeyear     roomno     unitno      qua1        qua2        qua3        
    ----------- ---------- ----------- ----------- ----------- ----------- 
    2003        1-101      1           1           1           1
    2003        1-102      1           1           1           1
    2003        1-103      1           1           1           1
    2003        2-101      2           1           1           1
    2003        2-102      2           1           1           1
    2003        2-103      2           1           1           1
    2003        3-101      3           1           1           1
    2003        3-102      3           1           1           1
    2003        3-103      3           1           1           1(所影响的行数为 9 行)