大力!QXJ让我找你! 我没法贴数据,几十万条,呵呵,只是让你看看我的在一定程度上的逻辑错在哪?问题呢是讲清了,结构呢就是上面的有unitno,roomno,isfinish,feeyear,feemonth,feename 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我要得到的结果集就是roomno unitno feeyear feemonth isfinish1-101 1 2003 1 11-101 1 2003 2 11-101 1 2003 3 11-102 1 2003 1 11-102 1 2003 2 11-102 1 2003 3 11-103 1 2003 1 11-103 1 2003 2 11-103 1 2003 3 11-101 1 2003 1 12-101 2 2003 2 12-101 2 2003 3 12-102 2 2003 1 12-102 2 2003 2 12-102 2 2003 3 12-103 2 2003 1 12-103 2 2003 2 12-103 2 2003 3 13-101 3 2003 1 13-101 3 2003 2 13-101 3 2003 3 13-102 3 2003 1 13-102 3 2003 2 13-102 3 2003 3 13-103 3 2003 1 13-103 3 2003 2 13-103 3 2003 3 1这是三个楼里共九个住户的,2003年123月的数据,isfinish是钱交了没,交了为1,没交为0用那个存储过程是实现输入年份,季度,开始楼号,结束楼号,就要得到某用户的在这一季度的数据最后成为year roomno unitno qua1 qua2 qua32003 1-101 1 1 1 12003 1-102 1 1 1 12003 1-103 1 1 1 12003 2-101 1 1 1 12003 2-102 1 1 1 12003 2-103 1 1 1 12003 3-101 1 1 1 12003 3-102 1 1 1 12003 3-103 1 1 1 1可现在得到的是year roomno unitno qua1 qua2 qua32003 1-101 1 1 1 12003 1-102 1 1 1 12003 1-103 1 1 1 12003 2-101 1 null null null2003 2-102 1 null null null2003 2-103 1 null null null2003 3-101 1 null null null2003 3-102 1 null null null2003 3-103 1 null null null如果要查的不是从1号楼开始的而是从2号楼开始的year roomno unitno qua1 qua2 qua32003 2-101 1 1 1 12003 2-102 1 1 1 12003 2-103 1 1 1 12003 3-101 1 null null null2003 3-102 1 null null null2003 3-103 1 null null null我想不通怎么会这样? 看了半天,以为自己有点懂了,却又不懂,f1*f2*f3不知道干什么。如果从总体来说,特别是后面补充的数据,存储过程可以改写:用case when ...group by会简单很多。 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,@结束层号 intselect @年=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 2 1 12003 1-102 1 1 1 12003 1-103 1 1 1 12003 2-101 2 0 1 12003 2-102 2 1 1 12003 2-103 2 1 1 12003 3-101 3 1 1 12003 3-102 3 1 1 12003 3-103 3 1 1 1(所影响的行数为 9 行) feeyear roomno unitno qua1 qua2 qua3 ----------- ---------- ----------- ----------- ----------- ----------- 2003 1-101 1 2 1 12003 1-102 1 1 1 12003 1-103 1 1 1 12003 2-101 2 0 1 12003 2-102 2 1 1 12003 2-103 2 1 1 12003 3-101 3 1 1 12003 3-102 3 1 1 12003 3-103 3 1 1 1(所影响的行数为 9 行) 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,@结束层号 intselect @年=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 12003 1-102 1 1 1 12003 1-103 1 1 1 12003 2-101 2 1 1 12003 2-102 2 1 1 12003 2-103 2 1 1 12003 3-101 3 1 1 12003 3-102 3 1 1 12003 3-103 3 1 1 1(所影响的行数为 9 行) Sql 2005 Reporting Services 矩阵中的小计如何自定义? (有图) 执行一个2句SQL的简单存储过程,对结果奇怪,请教大家。 小弟是个新的我SQL管理员,遇到一个对我非常难的问题,请各位高手帮帮忙 这两个问题怎么解决? 这个SQL语句怎样写 两个表的问题,超难!哪个大侠帮忙解决一下啊,谢谢啦。 如何在SQL中递归查询数据? update字段错误 如何实现这样的SQL语句? 高分求救:如何用JAVA来实现SQL Server的备份和恢复 关于数据的导入导出的问题,在线等待!! 请大家帮忙,关于数据库排序问题?
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我想不通怎么会这样?
如果从总体来说,特别是后面补充的数据,存储过程可以改写:
用case when ...group by
会简单很多。
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 你的表
----------- ---------- ----------- ----------- ----------- -----------
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 行)
----------- ---------- ----------- ----------- ----------- -----------
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 行)
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 行)