大力!QXJ让我找你!

上海老李 2003-11-03 11:26:58
我的存储过程是下面的
ALTER PROCEDURE monthlist @curyear char(4),@quarter char(2),@unitno char(2),@endunitno char(2)

AS
declare @month1 varchar(2)
declare @month2 varchar(2)
declare @month3 varchar(2)
if (@quarter='1')
begin
select @month1='1'
select @month2='2'
select @month3='3'
end
if (@quarter='2')
begin
select @month1='4'
select @month2='5'
select @month3='6'
end
if (@quarter='3')
begin
select @month1='7'
select @month2='8'
select @month3='9'
end
if (@quarter='4')
begin
select @month1='10'
select @month2='11'
select @month3='12'
end
select distinct len(roomno),convert(int,unitno,0),roomno,ownername,f1*f2*f3 as f4,* from
(select @quarter as qua,@curyear as curyear,@month1 as month1,@month2 as month2,@month3 as month3,aa.roomno as roomno,aa.unitno as unitno,aa.ownername,convert(int,d.finish1,0) as f1,convert(int,d2.finish2,0) as f2,convert(int,d3.finish3,0) as f3

from roominfo aa



left JOIN
(SELECT roomno,isfinish as finish1
FROM Fee
WHERE (FeeName = '物业管理费')
AND (FeeYear=@curyear)and (feemonth=@month1)
AND unitno=@unitno) d
on aa.roomno=d.roomno

left JOIN
(SELECT roomno, isfinish as finish2
FROM Fee
WHERE (FeeName = '物业管理费')
AND (FeeYear=@curyear)and (feemonth=@month2)
AND unitno=@unitno) d2
on aa.roomno=d2.roomno

left JOIN
(SELECT roomno, isfinish as finish3
FROM Fee
WHERE (FeeName = '物业管理费')
AND (FeeYear=@curyear)and (feemonth=@month3)
AND unitno=@unitno) d3
on aa.roomno=d3.roomno
where (convert(int,unitno,0)>=convert(int,@unitno,0)) and (convert(int,unitno,0)<=convert(int,@endunitno,0))

) bb

order by len(roomno),convert(int,unitno,0),roomno
(2003-11-03 10:30:27) 耗子
所得到的数据是要从1到4号楼得到数据,可是只有1号楼的正确,其它得到的数据的F1,F2,F3等是NULL值
[↑]
(2003-11-03 10:31:06) 耗子
当改变1为2,2的是正确的,3,4不正确
[↑]
(2003-11-03 10:31:20) 耗子
当改成3,4就不正确
[↑]
我不知为什么

2 2-702 师成健 1 1 2003 1 2 3 2-702 2 师成健 1 1 1
2 2-801 戴荣良 1 1 2003 1 2 3 2-801 2 戴荣良 1 1 1
2 2-802 董鸣鹤 1 1 2003 1 2 3 2-802 2 董鸣鹤 1 1 1
2 2-901 沈培英 1 1 2003 1 2 3 2-901 2 沈培英 1 1 1
2 2-902 吴菊生 1 1 2003 1 2 3 2-902 2 吴菊生 1 1 1
3 3-101 褚金娣 NULL 1 2003 1 2 3 3-101 3 褚金娣 NULL NULL NULL
3 3-102 赵林生 NULL 1 2003 1 2 3 3-102 3 赵林生 NULL NULL NULL
3 3-103 陈培生 NULL 1 2003 1 2 3 3-103 3 陈培生 NULL NULL NULL
3 3-201 张平 NULL 1 2003 1 2 3 3-201 3 张平 NULL NULL NULL
3 3-202 赵芝萍 NULL 1 2003 1 2 3 3-202 3 赵芝萍 NULL NULL NULL
3 3-203 唐晓澐 NULL 1 2003 1 2 3 3-203 3 唐晓澐 NULL NULL NULL
3 3-301 陈培良 NULL 1 2003 1 2 3 3-301 3 陈培良 NULL NULL NULL
换了有多个单元号,所有后边的就得到的f1,f2,f3都是空值
...全文
106 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
上海老李 2003-11-03
  • 打赏
  • 举报
回复
我要得到的结果集就是
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

我想不通怎么会这样?
pengdali 2003-11-03
  • 打赏
  • 举报
回复
贴出部分源结果集,和对应的目标结果集
上海老李 2003-11-03
  • 打赏
  • 举报
回复
我没法贴数据,几十万条,呵呵,只是让你看看我的在一定程度上的逻辑错在哪?问题呢是讲清了,结构呢就是上面的有unitno,roomno,isfinish,feeyear,feemonth,feename
txlicenhe 2003-11-03
  • 打赏
  • 举报
回复
1:找大力的贴子一般会有人看。
2:太长的贴子,楼主如果将表结构和数据的测试环境贴出来可能会好些,让其它人直接复制就能运行最好。
zjcxc 元老 2003-11-03
  • 打赏
  • 举报
回复
太长了,没看懂目的.

等大力吧.
上海老李 2003-11-03
  • 打赏
  • 举报
回复
学习,你的简单多了,呵呵,我的就太复杂,而且效率不高了
lvltt 2003-11-03
  • 打赏
  • 举报
回复
学习
pengdali 2003-11-03
  • 打赏
  • 举报
回复
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,@结束层号=3

select 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,unitno

go

drop 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 行)
上海老李 2003-11-03
  • 打赏
  • 举报
回复
谢谢大家的参与,我画蛇添了足,故引出一些问题,现已解决,谢谢诸位参与
pengdali 2003-11-03
  • 打赏
  • 举报
回复
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 行)
pengdali 2003-11-03
  • 打赏
  • 举报
回复
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 行)
pengdali 2003-11-03
  • 打赏
  • 举报
回复
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,@结束层号=3

select 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,unitno

go

drop table 你的表
samuelpan 2003-11-03
  • 打赏
  • 举报
回复
看了半天,以为自己有点懂了,却又不懂,f1*f2*f3不知道干什么。
如果从总体来说,特别是后面补充的数据,存储过程可以改写:
用case when ...group by
会简单很多。

34,874

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧