分组统计求占比, 请大家指教 (续)

miller530 2004-12-07 06:15:29
前:
http://community.csdn.net/Expert/topic/3622/3622512.xml?temp=.5480158


有如下表:

代号 收入 时间
------------------ ------------------- -----------------------
1001 300.00 2004-11-6
1001 200.00 2004-11-7
1002 150.00 2004-11-6
1002 250.00 2004-11-7
1003 100.00 2004-11-8
1004 100.00 2004-11-10
1004 200.00 2004-11-12
1005 300.00 2004-11-16
1005 300.00 2004-11-17
1005 300.00 2004-11-20
1005 300.00 2004-11-21
1006 300.00 2004-11-22
1006 300.00 2004-11-23
1006 300.00 2004-11-25

===================================================================

现在要根据时间段分组来得到如下表:
比如:设定2004-11-6到2004-11-20, 按每3天为一组得到如下表的结果
其中按几天来分组是不确定的,有可能是3天,有可能是7或者一个月


(以下是按3天的一组)

代号 收入 总收入 占比
------------------ ------------------- --------------- --------------------
1001 500.00 ....... .....
1002 400.00 ....... .....
1003 100.00 ....... .....
.... ...... ....... .....


请教各位怎么解决?
...全文
237 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
miller530 2004-12-16
  • 打赏
  • 举报
回复
这是我要的结果, 非常感谢你帮助.
Yang_ 2004-12-16
  • 打赏
  • 举报
回复
运行以下测试,看结果是不是你要的:
declare @SDt datetime --开始日期
declare @n int --一组的天数

set @SDt='2004-11-6'
set @n=3

select convert(varchar(10),dateadd(day,3*(datediff(day,@SDt,时间) / 3),@SDt),120)+' -- '+ convert(varchar(10),dateadd(day,2+3*(datediff(day,@SDt,时间) / 3),@SDt),120) as 日期组,代号,sum(收入) as 收入
into #t
from (
select
1001 as 代号, 300.00 as 收入, cast('2004-11-6' as datetime) as 时间
union all select
1001, 200.00 , '2004-11-7'
union all select
1002, 150.00 , '2004-11-6'
union all select
1002, 250.00 , '2004-11-7'
union all select
1003, 100.00 , '2004-11-8'
union all select
1004, 100.00 , '2004-11-10'
union all select
1004, 200.00 , '2004-11-12'
union all select
1005, 300.00 , '2004-11-16'
union all select
1005, 300.00 , '2004-11-17'
union all select
1005, 300.00 , '2004-11-20'
union all select
1005, 300.00 , '2004-11-21'
union all select
1006, 300.00 , '2004-11-22'
union all select
1006, 300.00 , '2004-11-23'
union all select
1006, 300.00 , '2004-11-25'
) as t
group by convert(varchar(10),dateadd(day,3*(datediff(day,@SDt,时间) / 3),@SDt),120)+' -- '+ convert(varchar(10),dateadd(day,2+3*(datediff(day,@SDt,时间) / 3),@SDt),120),代号

select 日期组,sum(收入) as 总收入
into #t1
from #t
group by 日期组

select a.日期组,a.代号,a.收入,b.总收入,cast(a.收入 as numeric(10,2))/b.总收入 as 占比
from #t a,#t1 b
where a.日期组=b.日期组
order by a.日期组,a.代号

drop table #t,#t1


Yang_ 2004-12-16
  • 打赏
  • 举报
回复
不好意思,好多天没来,短消息才看到

用临时标好处理些:
declare @SDt datetime --开始日期
declare @n int --一组的天数

set @SDt='2004-11-6'
set @n=3

select convert(varchar(10),dateadd(day,3*(datediff(day,@SDt,时间) / 3),@SDt),120)+' -- '+ convert(varchar(10),dateadd(day,2+3*(datediff(day,@SDt,时间) / 3),@SDt),120) as 日期组,代号,sum(收入) as 收入
into #t
from 原表
group by convert(varchar(10),dateadd(day,3*(datediff(day,@SDt,时间) / 3),@SDt),120)+' -- '+ convert(varchar(10),dateadd(day,2+3*(datediff(day,@SDt,时间) / 3),@SDt),120),代号

select 日期组,sum(收入) as 总收入
into #t1
from #t
group by 日期组

select a.日期组,a.代号,a.收入,b.总收入,cast(a.收入 as numeric(10,2))/b.总收入 as 占比
from #t a,#t1 b
where a.日期组=b.日期组
order by a.日期组,a.代号

drop table #t,#t1
yyyjff 2004-12-11
  • 打赏
  • 举报
回复
mark 帮楼主up
lipkissnow 2004-12-09
  • 打赏
  • 举报
回复
建議:
1.把一組要查詢的數據先取出其日期進行三天一組進行分劃.
2.然後用GROUP BY Item, Color WITH ROLLUP 進行查詢.
prcgolf 2004-12-09
  • 打赏
  • 举报
回复
up
miller530 2004-12-08
  • 打赏
  • 举报
回复
各位不好意思,我的问题没有描述清楚,现在补充一下。

首先, 按日期段查询出符合条件的记录,比如查询出2004-11-6至2004-11-25的记录,
然后,按N天为一组(如比3天:2000-11-6 -- 2004-11-8为一组),计算出[收入]、[总收入]、[占比]。

----------------------------------------------------------
[收入] = 查询结果中[代号]相同且在同一组内的收入之和
[总收入] = 查询到的所有记录的[收入]之和(不分组)
[占比] = [收入]/[总收入]


(以下是按3天的一组)

日期组 代号 收入 总收入 占比
--------------------- ------------------ ------------------- --------------- --------------------
2004-11-6 -- 2004-11-8 1001 500.00 3400 500/3400=?
2004-11-6 -- 2004-11-8 1002 400.00 3400 400/3400=?
2004-11-6 -- 2004-11-8 1003 100.00 3400 100/3400=?
.... ... ...... ....... .....



我这样描述,不知道大家是否能够理解, 还请各位帮忙。
Frewin 2004-12-08
  • 打赏
  • 举报
回复
Create Proc Pro_
@Bdate Datetime,
@Edate Datetime
As
declare @sum numeric(10,2)
select @sum=sum(收入) from t Where 时间 Between @Bdate And @Edate
select 代号,收入=sum(收入),总收入=@sum,占比=cast(sum(收入)/@sum as numeric(5,2))
from t Where 时间 Between @Bdate And @Edate group by 代号
Yang_ 2004-12-08
  • 打赏
  • 举报
回复
这个问题早几天就看到,问题是楼主描述得不是很清楚,既然按时间段汇总,那结果每个代号应该有多条记录,或者结果的字段是多个,搂主在最紧要的结果集进行了省略,就搞不清楚你的需要了
yingqing 2004-12-08
  • 打赏
  • 举报
回复
用存儲過程實現,引人一個臨時表,用來保存@startday--@ednday按@step進行分組#div表
create table #div(stday datetime,endday datime)
在#div表建立遊標,打開遊標,得到@stday,@endday
select 代号,sum(收入)as 收入 into tableB from tableA where 时间 between @stday and @endday
select 代号,sum(收入)as 總收入 into tableC from tableA
select B.代号,B.收入,C.總收入 from tableB B left outer join tableC C on B.代號=C.代號

27,579

社区成员

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

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