|M| 郁闷又把那条Group by 语句忘了大家帮看看 SQL查询语句
表
编号 用户 销售鸡蛋数 生成苹果数 销售日期开始 销售日期结未 是否有效
id user saleage saleapple begindate enddate istrue
1 1 10 8 2007-1-1 2007-1-31 1
2 1 12 7 2007-1-1 2007-1-31 1
3 2 8 9 2007-1-1 2007-1-31 1
4 2 8 10 2007-2-1 2007-02-28 1
5 2 12 11 2007-2-1 2007-02-28 1
6 1 11 15 2007-3-1 2007-03-31 1
7 3 19 12 2007-3-1 2007-03-31 0
要求:查询出每个时段里里面每个用的合计销售额
如结果:
user saleage saleapple begindate enddate
1 22 15 2007-1-1 2007-1-31
2 8 9 2007-1-1 2007-1-31
2 20 21 2007-2-1 2007-02-28
1 11 15 2007-3-1 2007-03-31
谢谢
------解决方案--------------------
select user ,sum(saleage ) saleage , sum( saleapple) saleapple,
begindate ,enddate
from 表
where istrue =1
group by user ,begindate ,enddate
[解决办法]
select sum(saleage),sum(saleapple) from tablename
where 提取月份的函数(begindate) in (select distinct 提取月份的函数(begindate) from tablename)
userid显示出来是不合理的,因为你一个月中有很多的userid.除非同时group
[解决办法]
装 Sqlserver要装联机丛书的吧,里面有说明和例子
[解决办法]
1> select Teller, saleage, saleapple, begindate, enddate from test group by begindate, enddate, Teller having istrue= '1 '
2> go
Teller saleage saleapple begindate enddate
------ ----------- ----------- ---------- ----------
1 10 8 2007-1-1 2007-1-31
1 12 7 2007-1-1 2007-1-31
2 8 9 2007-1-1 2007-1-31
2 8 10 2007-2-1 2007-02-28
2 12 11 2007-2-1 2007-02-28
1 11 15 2007-3-1 2007-03-31
[解决办法]
select user ,sum(saleage ) as saleage , sum( saleapple) as saleapple,
begindate ,enddate
from 表
where istrue =1
group by user ,begindate ,enddate
[解决办法]
sorry, 刚才看错了。
1> select Teller, sum(saleage) , sum(saleapple), begindate, enddate from test where istrue= '1 ' group by begindate, enddate,Teller
2> go
Teller begindate enddate
------ ----------- ----------- ---------- ----------
1 22 15 2007-1-1 2007-1-31
2 8 9 2007-1-1 2007-1-31
2 20 21 2007-2-1 2007-02-28
1 11 15 2007-3-1 2007-03-31
是正解
[解决办法]
学习
[解决办法]
强烈建议增加自学能力
[解决办法]
select user, sum(saleage) ageSum, sum(saleapple) appleSum, begindate enddate,
where istrue = '1 '
group by user,begindate,enddate;
[解决办法]
帮顶 LZ给点分
[解决办法]
呵呵.......