vfp 有订购日期和金额两个字段名,现在要按季度查出每个季度订购的金额,写出sql语句,怎么写?

来源:百度知道 编辑:UC知道 时间:2024/06/17 16:52:43
vfp 我有一个表dgd,里面有订购日期和总金额两个字段名,现在要按季度查出每个季度订购的总金额,写出sql语句,怎么写?(即按季度分组并求出每个季度金额总和)

假设日期是字符类型,表达形式如200902
select 金额,
sum(case when right(日期,2)>='1' and right(日期,2)<='3' then 金额 else 0 end ) as 第一季度,
sum(case when right(日期,2)>='4' and right(日期,2)<='6' then 金额 else 0 end ) as 第二季度,
sum(case when right(日期,2)>='7' and right(日期,2)<='9' then 金额 else 0 end ) as 第三季度,
sum(case when right(日期,2)>='10' and right(日期,2)<='12' then 金额 else 0 end ) as 第一季度
from dgd
group by 日期,金额

“订购日期”为日期型
“金额”为数值型
“季度”为数值型
1、在dgd表中,添加(季度,N,6,0)字段
use dgd
repl all 季度 with year(订购日期)*10+int(month(订购日期))/3.1) &&将日期转换成某年季度:如2009年2月10日,“季度”字段的值为20091
index on 季度 to 季度 &&按季度建索引,这步不能省
TOTAL TO 季度金额 ON 季度 金额
use 季度金额 &&生成的新表
brow &&打开查看

2、用sele语句
SELECT INT(MONTH(表1.rq)/3.1)+1, SUM(表1.sl);
FROM 表1;
GROUP BY 1;
ORDER BY 1;
into table 季度金额