关于数据统计的问题???请各位赐教!!!

liuyang0322 2001-11-27 08:22:07
我想在从表中取得这样的数据,怎样做????


客户编码,物料编码,销售数量,销售金额,销售日期 等一些字段。

条件:

给出一段时间(datetime1,datetime2), 时间类型(也就是 周,月,季度,年)

要求:

对这段时间内的销售情况进行按“时间类型”统计。

例如:给出的时间是 2001-10-01 , 2001-12-01

要得出这段时间内 每周或每月或每季度或每年 的销售情况

如果是每周就应该有8组数据,从10-01日起第一周,第二周,。。。。。
如果是每月就因该有2组数据,从10-01日起第一月,第二月,。。。。。

请各位高手多多指教????我这里急用!!!!
...全文
70 4 打赏 收藏 举报
写回复
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
SE1 2001-11-27
  • 打赏
  • 举报
回复
年:
select year(销售日期) as sj,sum(qty) as total_qty
from sales
where 销售日期>=datetime1
and 销售日期<=datetime2
group by year(销售日期)
月:
select str(year(销售日期))+str(month(销售日期)) as sj,sum(qty) as total_qty
from sales
where 销售日期>=datetime1
and 销售日期<=datetime2
group by str(year(销售日期))+str(month(销售日期))
周:
select str(year(销售日期))+str(datepart(wk,销售日期)) as sj,sum(qty) as total_qty
from sales
where 销售日期>=datetime1
and 销售日期<=datetime2
group by str(year(销售日期))+str(datepart(wk,销售日期))
季度:
好像没有现成的函数,得自己定义,然后按照上述方法进行查询。

P.S. 上面只是思路,具体应用时还需考虑一些其他因素,例如时间上有间断的处理(如按月统计,但有些月没有销售数据)
liuyang0322 2001-11-27
  • 打赏
  • 举报
回复
我用的数据库是SQL SERVER, 开发工具是C++ BUILDER。能否给出一个详细的解决方法???谢谢了!!!!!!!!!!!
segl 2001-11-27
  • 打赏
  • 举报
回复
其实解决此问题的突破口在于怎么样将datatime型的字段值转化为周数(不同的数据库提供不同的函数)。下面以周统计为例:
1。建立临时表与源表结构相同增加字段week,范围提取出来插入临时表中。同时将销售日期转换为周播入字段week中。

2.按group by周的方式从临时表中按指定要求分类汇总数据。
gexue 2001-11-27
  • 打赏
  • 举报
回复
呵呵,用笨办法啊,

取得日期类型的天数?

declare arr_date[2]
for i=1 to int((datetime2-datetime1)/datetype)
arr_date[1]=datetime1+i*datetype
arr_date[2]=datetime1+i*datetype
  select (数据) from tablename where arr_date[1]<=datetime<=arr_date[2]
......
发帖
MS-SQL Server

3.4w+

社区成员

MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
帖子事件
创建了帖子
2001-11-27 08:22
社区公告
暂无公告