Alter PROCEDURE [dbo].[statisticsMoneyInfo]
@DTime datetime, --时间
@DTimeEnd datetime, --时间
@PSTypeID int,
@iPageSize int --页面大小
,@iIndexPage int --多少页
,@xiadan money output --下单
,@cedan money output --撤单
,@jiangli1 money output --奖励
,@jiangli2 money output --奖励2
AS
BEGIN
Select UserID
,row_number() over (order by userId asc) as rowNum --行号,ID排序
into #temp100 --存入临时表
from UserInfo where AId=0
select UserID as userid into #userIdTable from #temp100 where rowNum between @iPageSize*(@iIndexPage-1)+1 and (@iPageSize*@iIndexPage)--查询要统计的*代理UserId
--#userIdTable所有的要统计的UserId 都在这个里面。下面是对每个userid统计,不知道不用循环怎么统计,
DECLARE @AgentId int; --
set @AgentId=0
SELECT @AgentId = count(*) FROM #userIdTable
while @AgentId>0
begin
select
@AgentId as 用户ID,
sum(case ChangeMoneyInfo.ChangeTypeID when 1 then changeMoney else 0 end) as 下单,
sum(case ChangeMoneyInfo.ChangeTypeID when 3 then changeMoney else 0 end) as 撤单,
sum(case ChangeMoneyInfo.ChangeTypeID when 2 then changeMoney else 0 end) as 奖励,
sum(case ChangeMoneyInfo.ChangeTypeID when 4 then changeMoney else 0 end) as 奖励2
into #temp --存入临时表
from dbo.ChangeMoneyInfo
where 1=1
and PSTypeID=isnull(@PSTypeID,PSTypeID)
and (ChangeMoneyInfo.userId in ((select UserID from Userinfo where AllAgentId like '%|'+cast(@AgentId as varchar(10))+'|%' or UserID=@AgentId)))--团队会员)
and ChangeTime>=Isnull(@DTime,ChangeTime)
and ChangeTime<=Isnull(@DTimeEnd,ChangeTime)
delete from #userIdTable where userid=@AgentId
end
drop table #userIdTable drop table #temp
END
消息 2714,级别 16,状态 6,过程 statisticsMoneyInfo,第 27 行
数据库中已存在名为 '#temp' 的对象。
报了一个这样的错误,还有我不想用循环。。。怎么统计。。。#userIdTable 这个临时表就包含了所有要统计的userId
23 个解决方案
#1
Alter PROCEDURE [dbo].[statisticsMoneyInfo]
@DTime datetime, --时间
@DTimeEnd datetime, --时间
@PSTypeID int,
@iPageSize int --页面大小
,@iIndexPage int --多少页
,@xiadan money output --下单
,@cedan money output --撤单
,@jiangli1 money output --奖励
,@jiangli2 money output --奖励2
AS
BEGIN
CREATE TABLE #temp100(USERID INT,rowNum int)
CREATE TABLE #temp (用户ID INT,下单 INT,撤单 INT ,奖励 INT,奖励2 INT )--存入临时表
INSERT INTO into #temp100
Select UserID
,row_number() over (order by userId asc) as rowNum --行号,ID排序
--存入临时表
from UserInfo where AId=0
select UserID as userid into #userIdTable from #temp100 where rowNum between @iPageSize*(@iIndexPage-1)+1 and (@iPageSize*@iIndexPage)--查询要统计的*代理UserId
--#userIdTable所有的要统计的UserId 都在这个里面。下面是对每个userid统计,不知道不用循环怎么统计,
DECLARE @AgentId int; --
set @AgentId=0
SELECT @AgentId = count(*) FROM #userIdTable
while @AgentId>0
begin
INSERT into #temp --存入临时表
select
@AgentId as 用户ID,
sum(case ChangeMoneyInfo.ChangeTypeID when 1 then changeMoney else 0 end) as 下单,
sum(case ChangeMoneyInfo.ChangeTypeID when 3 then changeMoney else 0 end) as 撤单,
sum(case ChangeMoneyInfo.ChangeTypeID when 2 then changeMoney else 0 end) as 奖励,
sum(case ChangeMoneyInfo.ChangeTypeID when 4 then changeMoney else 0 end) as 奖励2
from dbo.ChangeMoneyInfo
where 1=1
and PSTypeID=isnull(@PSTypeID,PSTypeID)
and (ChangeMoneyInfo.userId in ((select UserID from Userinfo where AllAgentId like '%|'+cast(@AgentId as varchar(10))+'|%' or UserID=@AgentId)))--团队会员)
and ChangeTime>=Isnull(@DTime,ChangeTime)
and ChangeTime<=Isnull(@DTimeEnd,ChangeTime)
delete from #userIdTable where userid=@AgentId
end
drop table #userIdTable drop table #temp
END
#2
select into 只能用一次,你这种循环要换预先定义的表,然后insert into
#3
我知道报错的原因,因为循环的时候,已近构建了一个临时表,但是我想就是已经要知道要比如统计userid为1,2,3的用户,我该怎么写呢?分别统计着几个用户的数据。。。。
#4
select
@AgentId as 用户ID,
sum(case ChangeMoneyInfo.ChangeTypeID when 1 then changeMoney else 0 end) as 下单,
sum(case ChangeMoneyInfo.ChangeTypeID when 3 then changeMoney else 0 end) as 撤单,
sum(case ChangeMoneyInfo.ChangeTypeID when 2 then changeMoney else 0 end) as 奖励,
sum(case ChangeMoneyInfo.ChangeTypeID when 4 then changeMoney else 0 end) as 奖励2
into #temp --存入临时表
from dbo.ChangeMoneyInfo
where 1=1
and PSTypeID=isnull(@PSTypeID,PSTypeID)
and (ChangeMoneyInfo.userId in ((select UserID from Userinfo where AllAgentId like '%|'+cast(@AgentId as varchar(10))+'|%' or UserID=@AgentId)))--团队会员)
and ChangeTime>=Isnull(@DTime,ChangeTime)
and ChangeTime<=Isnull(@DTimeEnd,ChangeTime)
这段里面的where中加个in (userid)?
@AgentId as 用户ID,
sum(case ChangeMoneyInfo.ChangeTypeID when 1 then changeMoney else 0 end) as 下单,
sum(case ChangeMoneyInfo.ChangeTypeID when 3 then changeMoney else 0 end) as 撤单,
sum(case ChangeMoneyInfo.ChangeTypeID when 2 then changeMoney else 0 end) as 奖励,
sum(case ChangeMoneyInfo.ChangeTypeID when 4 then changeMoney else 0 end) as 奖励2
into #temp --存入临时表
from dbo.ChangeMoneyInfo
where 1=1
and PSTypeID=isnull(@PSTypeID,PSTypeID)
and (ChangeMoneyInfo.userId in ((select UserID from Userinfo where AllAgentId like '%|'+cast(@AgentId as varchar(10))+'|%' or UserID=@AgentId)))--团队会员)
and ChangeTime>=Isnull(@DTime,ChangeTime)
and ChangeTime<=Isnull(@DTimeEnd,ChangeTime)
这段里面的where中加个in (userid)?
#5
我现在写的貌似要很长时间。。。。有更好的方式吗?
#6
4楼写法中,数据是否你想要的?那个into #temp就不用了。如果是,上执行计划来看看
#7
不过你这写法是不是少了个GROUP BY?
#8
。。。。
确实是。。。
#9
是的,要选择下数据。。。
#10
话说你这代码也太复杂了吧?特别是
and (ChangeMoneyInfo.userId in ((select UserID from Userinfo where AllAgentId like '%|'+cast(@AgentId as varchar(10))+'|%' or UserID=@AgentId)))--团队会员)
and (ChangeMoneyInfo.userId in ((select UserID from Userinfo where AllAgentId like '%|'+cast(@AgentId as varchar(10))+'|%' or UserID=@AgentId)))--团队会员)
#11
这块我打算修改下。。。呵呵。。。
#12
单纯统计的话,你需要修改的就是where条件,复杂的条件不一定就性能差,不过大部分情况下都不好
#13
上面的代码while那可以优化么?这里估计有问题。。。我相当于把一个一个统一然后放到表里面的。。。。
#14
使用while的初衷是什么?
#15
我的思路就是这样的。。。。就是把一个一个吧用户统计然后一个一个添加。。但是我记得有一种写法速度快很多,忘了。。。。
#16
insert into #t
select *
from tb
where id in (你需要统计的ID)
好像不用循环吧。。。
select *
from tb
where id in (你需要统计的ID)
好像不用循环吧。。。
#17
Alter PROCEDURE [dbo].[statisticsMoneyInfo]
@DTime datetime, --时间
@DTimeEnd datetime, --时间
@PSTypeID int,
@iPageSize int --页面大小
,@iIndexPage int --多少页
,@xiadan money output --下单
,@cedan money output --撤单
,@cunkuan money output --存款
,@zhongjiang money output --中奖
,@fandian money output --返点
AS
BEGIN
Select UserID
,row_number() over (order by userId asc) as rowNum --行号,ID排序
into #temp100
from UserInfo where AgentId=0
select UserID as userid into #userIdTable from #temp100 where rowNum between @iPageSize*(@iIndexPage-1)+1 and (@iPageSize*@iIndexPage)--查询要统计的*代理UserId
DECLARE @AgentId int; --
DECLARE @UserCount int; --
SELECT @UserCount = count(*) FROM #userIdTable
CREATE TABLE #temp (用户ID INT,下单 INT,撤单 INT ,奖励 INT,奖励2 INT )
while (@UserCount>0)
begin
set @AgentId=0
set @AgentId = (select top 1 userid from #userIdTable)
insert into #temp
select
@AgentId as 用户ID,
sum(case ChangeMoneyInfo.ChangeTypeID when 1 then changeMoney else 0 end) as 下单,
sum(case ChangeMoneyInfo.ChangeTypeID when 3 then changeMoney else 0 end) as 撤单,
sum(case ChangeMoneyInfo.ChangeTypeID when 2 then changeMoney else 0 end) as 奖励,
sum(case ChangeMoneyInfo.ChangeTypeID when 4 then changeMoney else 0 end) as 奖励2
from dbo.ChangeMoneyInfo
where 1=1
and PSTypeID=isnull(@PSTypeID,PSTypeID)
and (ChangeMoneyInfo.userId in (select UserID from Userinfo where AllAgentId like '%|'+cast(@AgentId as varchar(10))+'|%') or UserID=@AgentId)--团队会员)
and ChangeTime>=Isnull(@DTime,ChangeTime)
and ChangeTime<=Isnull(@DTimeEnd,ChangeTime)
group by Userid
delete from #userIdTable where userid=@AgentId
set @UserCount = @UserCount-1;
end
--添加时间-升
select * from #temp
drop table #userIdTable
drop table #temp
drop table #temp100
END
显示结果是这样的。。。我想一个Id只用一行
#18
select 用户id,sum(xx)....
from 你这个结果集
group by 用户ID
from 你这个结果集
group by 用户ID
#19
非常感谢,问题解决了。。。
#20
有个问题是要是没有数据的时候,就没添加。。。
#21
insert into #temp
select
@AgentId as 用户ID,
sum(case ChangeMoneyInfo.ChangeTypeID when 1 then changeMoney else 0 end) as 下单,
sum(case ChangeMoneyInfo.ChangeTypeID when 3 then changeMoney else 0 end) as 撤单,
sum(case ChangeMoneyInfo.ChangeTypeID when 2 then changeMoney else 0 end) as 奖励,
sum(case ChangeMoneyInfo.ChangeTypeID when 4 then changeMoney else 0 end) as 奖励2
from dbo.ChangeMoneyInfo
where 1=1
and PSTypeID=isnull(@PSTypeID,PSTypeID)
and (ChangeMoneyInfo.userId in (select UserID from Userinfo where AllAgentId like '%|'+cast(@AgentId as varchar(10))+'|%') or UserID=@AgentId)--团队会员)
and ChangeTime>=Isnull(@DTime,ChangeTime)
and ChangeTime<=Isnull(@DTimeEnd,ChangeTime)
group by Userid
有个问题是要是没有数据的时候,就没添加。。。
#22
没数据的时候当然不添加啊,不然你添加什么?
#23
谢谢。已经搞定了
#1
Alter PROCEDURE [dbo].[statisticsMoneyInfo]
@DTime datetime, --时间
@DTimeEnd datetime, --时间
@PSTypeID int,
@iPageSize int --页面大小
,@iIndexPage int --多少页
,@xiadan money output --下单
,@cedan money output --撤单
,@jiangli1 money output --奖励
,@jiangli2 money output --奖励2
AS
BEGIN
CREATE TABLE #temp100(USERID INT,rowNum int)
CREATE TABLE #temp (用户ID INT,下单 INT,撤单 INT ,奖励 INT,奖励2 INT )--存入临时表
INSERT INTO into #temp100
Select UserID
,row_number() over (order by userId asc) as rowNum --行号,ID排序
--存入临时表
from UserInfo where AId=0
select UserID as userid into #userIdTable from #temp100 where rowNum between @iPageSize*(@iIndexPage-1)+1 and (@iPageSize*@iIndexPage)--查询要统计的*代理UserId
--#userIdTable所有的要统计的UserId 都在这个里面。下面是对每个userid统计,不知道不用循环怎么统计,
DECLARE @AgentId int; --
set @AgentId=0
SELECT @AgentId = count(*) FROM #userIdTable
while @AgentId>0
begin
INSERT into #temp --存入临时表
select
@AgentId as 用户ID,
sum(case ChangeMoneyInfo.ChangeTypeID when 1 then changeMoney else 0 end) as 下单,
sum(case ChangeMoneyInfo.ChangeTypeID when 3 then changeMoney else 0 end) as 撤单,
sum(case ChangeMoneyInfo.ChangeTypeID when 2 then changeMoney else 0 end) as 奖励,
sum(case ChangeMoneyInfo.ChangeTypeID when 4 then changeMoney else 0 end) as 奖励2
from dbo.ChangeMoneyInfo
where 1=1
and PSTypeID=isnull(@PSTypeID,PSTypeID)
and (ChangeMoneyInfo.userId in ((select UserID from Userinfo where AllAgentId like '%|'+cast(@AgentId as varchar(10))+'|%' or UserID=@AgentId)))--团队会员)
and ChangeTime>=Isnull(@DTime,ChangeTime)
and ChangeTime<=Isnull(@DTimeEnd,ChangeTime)
delete from #userIdTable where userid=@AgentId
end
drop table #userIdTable drop table #temp
END
#2
select into 只能用一次,你这种循环要换预先定义的表,然后insert into
#3
我知道报错的原因,因为循环的时候,已近构建了一个临时表,但是我想就是已经要知道要比如统计userid为1,2,3的用户,我该怎么写呢?分别统计着几个用户的数据。。。。
#4
select
@AgentId as 用户ID,
sum(case ChangeMoneyInfo.ChangeTypeID when 1 then changeMoney else 0 end) as 下单,
sum(case ChangeMoneyInfo.ChangeTypeID when 3 then changeMoney else 0 end) as 撤单,
sum(case ChangeMoneyInfo.ChangeTypeID when 2 then changeMoney else 0 end) as 奖励,
sum(case ChangeMoneyInfo.ChangeTypeID when 4 then changeMoney else 0 end) as 奖励2
into #temp --存入临时表
from dbo.ChangeMoneyInfo
where 1=1
and PSTypeID=isnull(@PSTypeID,PSTypeID)
and (ChangeMoneyInfo.userId in ((select UserID from Userinfo where AllAgentId like '%|'+cast(@AgentId as varchar(10))+'|%' or UserID=@AgentId)))--团队会员)
and ChangeTime>=Isnull(@DTime,ChangeTime)
and ChangeTime<=Isnull(@DTimeEnd,ChangeTime)
这段里面的where中加个in (userid)?
@AgentId as 用户ID,
sum(case ChangeMoneyInfo.ChangeTypeID when 1 then changeMoney else 0 end) as 下单,
sum(case ChangeMoneyInfo.ChangeTypeID when 3 then changeMoney else 0 end) as 撤单,
sum(case ChangeMoneyInfo.ChangeTypeID when 2 then changeMoney else 0 end) as 奖励,
sum(case ChangeMoneyInfo.ChangeTypeID when 4 then changeMoney else 0 end) as 奖励2
into #temp --存入临时表
from dbo.ChangeMoneyInfo
where 1=1
and PSTypeID=isnull(@PSTypeID,PSTypeID)
and (ChangeMoneyInfo.userId in ((select UserID from Userinfo where AllAgentId like '%|'+cast(@AgentId as varchar(10))+'|%' or UserID=@AgentId)))--团队会员)
and ChangeTime>=Isnull(@DTime,ChangeTime)
and ChangeTime<=Isnull(@DTimeEnd,ChangeTime)
这段里面的where中加个in (userid)?
#5
我现在写的貌似要很长时间。。。。有更好的方式吗?
#6
4楼写法中,数据是否你想要的?那个into #temp就不用了。如果是,上执行计划来看看
#7
不过你这写法是不是少了个GROUP BY?
#8
。。。。
确实是。。。
#9
是的,要选择下数据。。。
#10
话说你这代码也太复杂了吧?特别是
and (ChangeMoneyInfo.userId in ((select UserID from Userinfo where AllAgentId like '%|'+cast(@AgentId as varchar(10))+'|%' or UserID=@AgentId)))--团队会员)
and (ChangeMoneyInfo.userId in ((select UserID from Userinfo where AllAgentId like '%|'+cast(@AgentId as varchar(10))+'|%' or UserID=@AgentId)))--团队会员)
#11
这块我打算修改下。。。呵呵。。。
#12
单纯统计的话,你需要修改的就是where条件,复杂的条件不一定就性能差,不过大部分情况下都不好
#13
上面的代码while那可以优化么?这里估计有问题。。。我相当于把一个一个统一然后放到表里面的。。。。
#14
使用while的初衷是什么?
#15
我的思路就是这样的。。。。就是把一个一个吧用户统计然后一个一个添加。。但是我记得有一种写法速度快很多,忘了。。。。
#16
insert into #t
select *
from tb
where id in (你需要统计的ID)
好像不用循环吧。。。
select *
from tb
where id in (你需要统计的ID)
好像不用循环吧。。。
#17
Alter PROCEDURE [dbo].[statisticsMoneyInfo]
@DTime datetime, --时间
@DTimeEnd datetime, --时间
@PSTypeID int,
@iPageSize int --页面大小
,@iIndexPage int --多少页
,@xiadan money output --下单
,@cedan money output --撤单
,@cunkuan money output --存款
,@zhongjiang money output --中奖
,@fandian money output --返点
AS
BEGIN
Select UserID
,row_number() over (order by userId asc) as rowNum --行号,ID排序
into #temp100
from UserInfo where AgentId=0
select UserID as userid into #userIdTable from #temp100 where rowNum between @iPageSize*(@iIndexPage-1)+1 and (@iPageSize*@iIndexPage)--查询要统计的*代理UserId
DECLARE @AgentId int; --
DECLARE @UserCount int; --
SELECT @UserCount = count(*) FROM #userIdTable
CREATE TABLE #temp (用户ID INT,下单 INT,撤单 INT ,奖励 INT,奖励2 INT )
while (@UserCount>0)
begin
set @AgentId=0
set @AgentId = (select top 1 userid from #userIdTable)
insert into #temp
select
@AgentId as 用户ID,
sum(case ChangeMoneyInfo.ChangeTypeID when 1 then changeMoney else 0 end) as 下单,
sum(case ChangeMoneyInfo.ChangeTypeID when 3 then changeMoney else 0 end) as 撤单,
sum(case ChangeMoneyInfo.ChangeTypeID when 2 then changeMoney else 0 end) as 奖励,
sum(case ChangeMoneyInfo.ChangeTypeID when 4 then changeMoney else 0 end) as 奖励2
from dbo.ChangeMoneyInfo
where 1=1
and PSTypeID=isnull(@PSTypeID,PSTypeID)
and (ChangeMoneyInfo.userId in (select UserID from Userinfo where AllAgentId like '%|'+cast(@AgentId as varchar(10))+'|%') or UserID=@AgentId)--团队会员)
and ChangeTime>=Isnull(@DTime,ChangeTime)
and ChangeTime<=Isnull(@DTimeEnd,ChangeTime)
group by Userid
delete from #userIdTable where userid=@AgentId
set @UserCount = @UserCount-1;
end
--添加时间-升
select * from #temp
drop table #userIdTable
drop table #temp
drop table #temp100
END
显示结果是这样的。。。我想一个Id只用一行
#18
select 用户id,sum(xx)....
from 你这个结果集
group by 用户ID
from 你这个结果集
group by 用户ID
#19
非常感谢,问题解决了。。。
#20
有个问题是要是没有数据的时候,就没添加。。。
#21
insert into #temp
select
@AgentId as 用户ID,
sum(case ChangeMoneyInfo.ChangeTypeID when 1 then changeMoney else 0 end) as 下单,
sum(case ChangeMoneyInfo.ChangeTypeID when 3 then changeMoney else 0 end) as 撤单,
sum(case ChangeMoneyInfo.ChangeTypeID when 2 then changeMoney else 0 end) as 奖励,
sum(case ChangeMoneyInfo.ChangeTypeID when 4 then changeMoney else 0 end) as 奖励2
from dbo.ChangeMoneyInfo
where 1=1
and PSTypeID=isnull(@PSTypeID,PSTypeID)
and (ChangeMoneyInfo.userId in (select UserID from Userinfo where AllAgentId like '%|'+cast(@AgentId as varchar(10))+'|%') or UserID=@AgentId)--团队会员)
and ChangeTime>=Isnull(@DTime,ChangeTime)
and ChangeTime<=Isnull(@DTimeEnd,ChangeTime)
group by Userid
有个问题是要是没有数据的时候,就没添加。。。
#22
没数据的时候当然不添加啊,不然你添加什么?
#23
谢谢。已经搞定了