首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

怎么统计每周,每月,每季,每年的数据?多谢大家

2012-01-23 
如何统计每周,每月,每季,每年的数据?谢谢大家表order(订单表)order_idclient_id(客户ID)order_time(订单时

如何统计每周,每月,每季,每年的数据?谢谢大家
表order(订单表)
order_id         client_id(客户ID)     order_time(订单时间)
      1                               1                           2007-1-5
      2                               1                           2007-1-7
      3                               1                           2007-6-5
      4                               3                           2007-2-5
      5                               3                           2007-2-18
---
表item(明细表)
item_id   order_id(明细表)   pro_id(产品ID)     pro_amount(数量)     pro_price(单价)
      1                   1                               1                             10                                   10.00
      2                   1                               3                               5                                   15.00
      3                   2                               1                               5                                   12.00
      4                   3                               2                               10                                 8.00
      5                   4                               3                                 2                                 15.00
      6                   5                               2                                 6                                   10.00


如何汇总得到如下效果:
1.按年得到总金额
client_id       1月       2月       3月         4月     5月     6月   ...       12月
        1           235.00     0.00     0.00     0.00   0.00     80.00   ...   0.00
        3             0.00       90.00   0.00     0.00   0.00     0.00...       0.00
2.按周得到总金额:
client_id   周一     周二     ....周日
      1
      3
3.按月得到总金额:
client_id     1号     2号     3号.....31号
      1
      3
4.按季:
client_id   第一季度     第二季度     第二季度       第四季度  
        1
        3

[解决办法]
帮顶
[解决办法]
create table [order]
(
order_id int,
client_id int,
order_time datetime
)
create table item
(
item_idint,
order_id int,
pro_id int,
pro_amount int,
pro_price int
)

insert into [order]
select 1,1, '2007-1-5 '
union all
select 2,1, '2007-1-7 '
union all
select 3,1, '2007-6-5 '
union all
select 4,3, '2007-2-5 '
union all
select 5,3, '2007-2-18 '

insert into item
select 1,1,1,10,10
union all
select 2,1,3,5,15
union all
select 3,2,1,5,12
union all
select 4,3,2,10,8
union all
select 5,4,3,2,15
union all
select 6,5,2,6,10


select client_id ,
sum(case when month(order_time) = 1 then pro_amount*pro_price else 0 end) '1月 ',
sum(case when month(order_time) = 2 then pro_amount*pro_price else 0 end) '2月 ',
sum(case when month(order_time) = 3 then pro_amount*pro_price else 0 end) '3月 ',
sum(case when month(order_time) = 4 then pro_amount*pro_price else 0 end) '4月 ',
sum(case when month(order_time) = 5 then pro_amount*pro_price else 0 end) '5月 ',
sum(case when month(order_time) = 6 then pro_amount*pro_price else 0 end) '6月 ',
sum(case when month(order_time) = 7 then pro_amount*pro_price else 0 end) '7月 ',
sum(case when month(order_time) = 8 then pro_amount*pro_price else 0 end) '8月 ',
sum(case when month(order_time) = 9 then pro_amount*pro_price else 0 end) '9月 ',
sum(case when month(order_time) = 10 then pro_amount*pro_price else 0 end) '10月 ',
sum(case when month(order_time) = 11 then pro_amount*pro_price else 0 end) '11月 ',
sum(case when month(order_time) = 12 then pro_amount*pro_price else 0 end) '12月 '
from [order],item where [order].order_id = item.order_id
group by client_id
[解决办法]
newppstream() ( ) 信誉:100
谢谢:dawugui(潇洒老乌龟),按周计算是统计从周一到周日的数据.

2.按周得到总金额:
client_id 周一 周二 ....周日
1
3

那得把月份带进去才行了.不好意思.

热点排行