续:如何实现按月份统计数据库信息(100分)

套牢

Unregistered / Unconfirmed
GUEST, unregistred user!
请问:
一个企业每个月都有数笔交易
如果我要统计1998年5月到2000年7月各月的销售额,
要求结果如下:
月 金额
1998.5 2000
1998.6 1000
... ...
sql语句该怎么写?
已经有一个可不太对
select year(日期),month(日期), sum(金额) from 表 where 日期>1998.5 and 日期<2000.7 group by year(日期), month(日期)

 
这部分不对:year(日期),month(日期), sum(金额)
 
只用month(日期), sum(金额)就可以了吗?
 
select year(日期), month(日期), sum(金额) from 表
where 日期 between '1998-5-1' and '2000-8-1'
group by year(日期), month(日期)
试试看
 
我的那句话没有问题,用grid一显示才知道实际上结果是正确的。
但是现在要把这个结果送到dbchart中,却怎么也不对。
x轴为月份,
显示时总是不同年份的同一个月覆盖在一起,怎样才能让它们一字排开呢?
 
另外对query进行排序也有问题
如adouqery1.sort:=(column1 ASC);//一执行就出错,怎么解决?
column1为年份,上面sql的结果。
 
实际上排序在sql中加上order by就可以解决,不用在外面单独做,
可上面的排序毕竟还是一个问题(现在暂时可以不考虑);
所以剩下就只有如何让dbchart识别的问题。
不知道sql中有没有同时截取年、月的函数,就象year()那样
 
不知道啊
 
请参照如下两种写法(database---sql server)
1. select convert(char(5),rptdate,2),sum(stock) from dailyqty
group by convert(char(5),rptdate,2) order by convert(char(5),rptdate,2)
2. select convert(char(4),year(rptdate))+'-'+convert(char(2),month(rptdate)),sum(stock) from dailyqty
group by convert(char(4),year(rptdate))+'-'+convert(char(2),month(rptdate))
convert(char(4),year(rptdate))+'-'+convert(char(2),month(rptdate))
 
select year(日期) AS 年, month(日期) AS 月, sum(金额) AS 金额 from 表
where 日期 between '1998-5-1' and '2000-8-1'
group by 年, 月
 
aspire的第一种方法可行
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
598
import
I
I
回复
0
查看
813
import
I
顶部