一个sql问题,问大家献策(100分)

  • 主题发起人 主题发起人 qiubole
  • 开始时间 开始时间
Q

qiubole

Unregistered / Unconfirmed
GUEST, unregistred user!
我有一数据表test
内容如下
client workdate p_sum
---------- ------------------------------------------------------ -----------
1111 2001-01-05 00:00:00.000 14
1111 2001-02-05 00:00:00.000 94
1111 2001-01-07 00:00:00.000 24
1112 2001-01-05 00:00:00.000 14
1113 2001-03-05 00:00:00.000 24
1112 2001-02-05 00:00:00.000 17


我想得到每一个client的月平均销售量
即我想得到
1111 66
1112 15.5
1113 24

直接用select client,avg(p_sum) from test group by client好像不行
请大家献献计策
 
你是什么数据库?
怎么看怎么行
 
主要是你的月平均销量要求是(总销量/销售月份)销售月份要写个子查询求出。
 
select client, sum(p_sum)/count(distinct month(workdate))
from test
group by client
 
接受答案了.
 
可以啊,我在oracle下面试了一下,没问题。
就是还用加上一个where workdate between begindate and enddate
 
select client,avg(P_sum) from (select client,YYMM,sum(P_sum) as P_sum from
(select client, Rtrim(cast(year(workdate) as char(4)))+Rtrim(cast(month(workdate)as char(2))) as YYMM,p_sum
from test) as A
group by client,YYMM) as B
group by client

没问题吧,给分吧
 
SELECT DISTINCT test.client ,
((SELECT sum(p_sum) FROM test where test.client = test1.client)/(SELECT count (DISTINCT rtrim(cast(year(test.workdate) as char(4)) + rtrim(cast(month(test.workdate) as char(2))))) FROM test where test.client = test1.client)) as [sum]
FROM test LEFT OUTER JOIN
test test1 ON test.ID = test1.ID where test.client = test1.client


sql server 7.0 ok

give me
 
后退
顶部