帮我看看我的存储进程执行结果杂不对呢,比如参数我传递的是@sdate 2005-05-23 14:59:59 和@sdate 2005-05-22 14:59:59

dm1cyg 2006-05-23 10:04:02
CREATE PROCEDURE rjstj
@sdate datetime,
@edate datetime
/*
处理厂商统计的sdate代表开始日期edate
代表结束日期
@sdate datetime,
*/
as
--set @sDate=convert(datetime,convert(varchar(10),getdate(),120)+' 14:59:59.997')
--set @edate=dateadd(dd,-1,@eDate)
declare @s varchar(8000)

--set @sdate='2006-01-10'
--set @edate='2006-01-07'
set @s='select 类别= isnull(类别,''总计'')'
select @s=@s+',['+类型名称+']=sum(case 类型名称 when '''+类型名称+''' then 销售单价*数量 else 0 end)'
from yphjxshz group by 类型名称 order by 类型名称
set @s=@s
+',[合计]= sum(销售单价*数量) from yphjxshz where datediff(ms,'''+convert(char(20),@sdate,120)+''',发生日期)<=0 and datediff(ms,'''+convert(char(20),@edate,120)+''',发生日期)>= 0 group by 类别 with rollup order by 合计'
exec(@s)
GO
执行完后和我用sum()统计出的合计差,不知道为什么
...全文
198 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
dm1cyg 2006-05-24
  • 打赏
  • 举报
回复
为什么和我用 select sum(销售单价*数量) as 金额,类别 from yphjxshz where 发生日期 between 2005-05-22 14:59:59 and 2005-05-23 14:59:59 group by 类别 求出来的,比他多了
dm1cyg 2006-05-24
  • 打赏
  • 举报
回复
少量的数据是正确的,多了就不对了,
昵称被占用了 2006-05-24
  • 打赏
  • 举报
回复


简直是鸵鸟
dm1cyg 2006-05-24
  • 打赏
  • 举报
回复
楼上大哥,帮我看看我后发点段的.我想把他改成下午15:00为每天的分界线
dm1cyg 2006-05-24
  • 打赏
  • 举报
回复
不改也一致
昵称被占用了 2006-05-24
  • 打赏
  • 举报
回复


改成between当然一致了

dm1cyg 2006-05-24
  • 打赏
  • 举报
回复
一致的,是数据有问题,

没用错误是数据有问题了,
帮看看下面的存储过程,我是想统计一个月的医生工作量,但有个要求是这样的,每天的都是昨天3点到今天3点算一个工作日,每月的20号,为结帐日期 ,21号为下个月的,帮我看看我这段那里出问题了
--创建存储过程
/*
write by cyg
date 2006-01-23
统计月医生工作量

*/
CREATE procedure sp_getInfo(@month datetime,@time as int=0) --@time,统计时间中小时的限定值
as
begin
--输入参数合法性判定
if @time<0 or @time>23
return

declare @s varchar(8000),@day int
set @s='select 医生名称'

--提取被统计时间范围内的数据,并插入临时表#t 21 00:00:00为21号00点00分00秒而 20 23:59:59为下月20号的开始
select * into #t from yphjxshz where dateadd(hh,24-@time,发生日期) between convert(char(8),dateadd(mm,-1,@month),120)+'21 00:00:00' and convert(char(8),@month,120)+'20 23:59:59'

--定义游标,获取当前统计范围内不重复的日期数据信息
declare t_cursor cursor for select distinct day(发生日期) from yphjxshz order by day(发生日期)

--打开游标
open t_cursor
fetch next from t_cursor into @day

--遍历游标,利用动态SQL实现交叉表查询
while @@fetch_status=0
begin
select @s=@s+',['+rtrim(@day)+'号'+类别+']=cast(sum(case when day(发生日期)='+rtrim(@day)+' and 类别='''+类别+''' then 数量*销售单价 else 0 end) as int)'
from (select distinct 类别 from yphjxshz) a
set @s=@s+',['+rtrim(@day)+'号合计]=cast(sum(case when day(发生日期)='+rtrim(@day)+' then 数量*销售单价 else 0 end) as int)'
fetch next from t_cursor into @day
end

--关闭游标
close t_cursor
deallocate t_cursor

--完成动态SQL语句的组织
set @s=@s+' from #t group by 医生名称'

--输出动态SQL语句的内容
print @s

--执行动态SQL
exec(@s)
end
GO
昵称被占用了 2006-05-24
  • 打赏
  • 举报
回复
怎么会一致?
查询的时间范围根本就不同
  • 打赏
  • 举报
回复
不应该不一致,你把表结构和数据贴出来,看看。
昵称被占用了 2006-05-24
  • 打赏
  • 举报
回复
直接写between多好,省得麻烦

set @s=@s
+',[合计]= sum(销售单价*数量) from yphjxshz where 发生日期 between '''+convert(char(20),@sdate,120)+''' and '''+convert(char(20),@edate,120)+''' group by 类别 with rollup order by 合计'

或者

set @s=@s
+',[合计]= sum(销售单价*数量) from yphjxshz where 发生日期>='''+convert(char(20),@sdate,120)+''' and 发生日期<='''+convert(char(20),@edate,120)+''' group by 类别 with rollup order by 合计'

用datediff效率低而且容易让自己糊涂
昵称被占用了 2006-05-24
  • 打赏
  • 举报
回复
时间处理问题


set @s=@s
+',[合计]= sum(销售单价*数量) from yphjxshz where datediff(ms,'''+convert(char(20),@sdate,120)+''',发生日期)<=0 and datediff(ms,'''+convert(char(20),@edate,120)+''',发生日期)>= 0 group by 类别 with rollup order by 合计'

其中
datediff(ms,'''+convert(char(20),@sdate,120)+''',发生日期)<=0
的结果就是 @sdate>=发生日期
datediff(ms,'''+convert(char(20),@edate,120)+''',发生日期)>= 0
的结果就是 @edate<=发生日期

整个语句取的时间范围是 not (发生日期 between 2005-05-22 14:59:59 and 2005-05-23 14:59:59)


wwh999 2006-05-24
  • 打赏
  • 举报
回复
需要数据..你贴出来..才好帮你论证..
liangpei2008 2006-05-23
  • 打赏
  • 举报
回复
给我yphjxshz的结构与数据,我试一下
dm1cyg 2006-05-23
  • 打赏
  • 举报
回复
执行完的结果和我发的一样,不对
wwh999 2006-05-23
  • 打赏
  • 举报
回复
老大都已经写了,关注 :)
dm1cyg 2006-05-23
  • 打赏
  • 举报
回复
zjcxc(邹建)
你给我发的杂执行呀
dm1cyg 2006-05-23
  • 打赏
  • 举报
回复
他求出的合计比我用
sum()求出的合计少呀
wwh999 2006-05-23
  • 打赏
  • 举报
回复
出现什么故障或错误提示?
zjcxc 元老 2006-05-23
  • 打赏
  • 举报
回复
CREATE PROCEDURE rjstj
@sdate datetime,
@edate datetime
/*
处理厂商统计的sdate代表开始日期edate
代表结束日期
@sdate datetime,
*/
as
--set @sDate=convert(datetime,convert(varchar(10),getdate(),120)+' 14:59:59.997')
--set @edate=dateadd(dd,-1,@eDate)
declare @s nvarchar(4000)

--set @sdate='2006-01-10'
--set @edate='2006-01-07'
set @s='select 类别= isnull(类别,''总计'')'
select @s=@s+',['+类型名称+']=sum(case 类型名称 when '''+类型名称+''' then 销售单价*数量 else 0 end)'
from yphjxshz group by 类型名称 order by 类型名称
set @s=@s
+',[合计]= sum(销售单价*数量) from yphjxshz where datediff(ms,@sdate,发生日期)<=0 and datediff(ms,@edate,发生日期)>= 0 group by 类别 with rollup order by 合计'
exec sp_executesql @s, N' @sdate datetime,@edate datetime', @sdate, @edate

GO

34,594

社区成员

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

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