求写一条sql语句。。。统计用的。。。

ruolins 2014-04-15 01:39:47
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
...全文
238 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
ruolins 2014-04-17
  • 打赏
  • 举报
回复
引用 22 楼 DBA_Huangzj 的回复:
没数据的时候当然不添加啊,不然你添加什么?
谢谢。已经搞定了
發糞塗牆 2014-04-16
  • 打赏
  • 举报
回复
没数据的时候当然不添加啊,不然你添加什么?
ruolins 2014-04-15
  • 打赏
  • 举报
回复
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 
有个问题是要是没有数据的时候,就没添加。。。
ruolins 2014-04-15
  • 打赏
  • 举报
回复
引用 18 楼 DBA_Huangzj 的回复:
select 用户id,sum(xx).... from 你这个结果集 group by 用户ID
有个问题是要是没有数据的时候,就没添加。。。
ruolins 2014-04-15
  • 打赏
  • 举报
回复
非常感谢,问题解决了。。。
發糞塗牆 2014-04-15
  • 打赏
  • 举报
回复
select 用户id,sum(xx).... from 你这个结果集 group by 用户ID
ruolins 2014-04-15
  • 打赏
  • 举报
回复
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只用一行
發糞塗牆 2014-04-15
  • 打赏
  • 举报
回复
insert into #t select * from tb where id in (你需要统计的ID) 好像不用循环吧。。。
ruolins 2014-04-15
  • 打赏
  • 举报
回复
引用 14 楼 DBA_Huangzj 的回复:
使用while的初衷是什么?
我的思路就是这样的。。。。就是把一个一个吧用户统计然后一个一个添加。。但是我记得有一种写法速度快很多,忘了。。。。
發糞塗牆 2014-04-15
  • 打赏
  • 举报
回复
使用while的初衷是什么?
ruolins 2014-04-15
  • 打赏
  • 举报
回复
引用 12 楼 DBA_Huangzj 的回复:
单纯统计的话,你需要修改的就是where条件,复杂的条件不一定就性能差,不过大部分情况下都不好
上面的代码while那可以优化么?这里估计有问题。。。我相当于把一个一个统一然后放到表里面的。。。。
發糞塗牆 2014-04-15
  • 打赏
  • 举报
回复
单纯统计的话,你需要修改的就是where条件,复杂的条件不一定就性能差,不过大部分情况下都不好
ruolins 2014-04-15
  • 打赏
  • 举报
回复
引用 10 楼 DBA_Huangzj 的回复:
话说你这代码也太复杂了吧?特别是 and (ChangeMoneyInfo.userId in ((select UserID from Userinfo where AllAgentId like '%|'+cast(@AgentId as varchar(10))+'|%' or UserID=@AgentId)))--团队会员)
这块我打算修改下。。。呵呵。。。
發糞塗牆 2014-04-15
  • 打赏
  • 举报
回复
话说你这代码也太复杂了吧?特别是 and (ChangeMoneyInfo.userId in ((select UserID from Userinfo where AllAgentId like '%|'+cast(@AgentId as varchar(10))+'|%' or UserID=@AgentId)))--团队会员)
ruolins 2014-04-15
  • 打赏
  • 举报
回复
引用 4 楼 DBA_Huangzj 的回复:
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)?
是的,要选择下数据。。。
ruolins 2014-04-15
  • 打赏
  • 举报
回复
引用 7 楼 DBA_Huangzj 的回复:
不过你这写法是不是少了个GROUP BY?
。。。。确实是。。。
發糞塗牆 2014-04-15
  • 打赏
  • 举报
回复
不过你这写法是不是少了个GROUP BY?
發糞塗牆 2014-04-15
  • 打赏
  • 举报
回复
4楼写法中,数据是否你想要的?那个into #temp就不用了。如果是,上执行计划来看看
ruolins 2014-04-15
  • 打赏
  • 举报
回复
我现在写的貌似要很长时间。。。。有更好的方式吗?
發糞塗牆 2014-04-15
  • 打赏
  • 举报
回复
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)?
加载更多回复(3)

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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