㈠ sql 语句计算当月工资总和
substring截取res_time,只取年月,再GROUP BY查询,返回日期应只有年月
㈡ 如何用sql的日期函数,分别查出1月~12月每个月的销售金额
一般情况下数据是按日期存到数据库中的,但考虑到某些月份是无数据的,可用如下语句:
1、创建测试表及插入测试数据:
create table sale
(id int,
name varchar(10),
sdate datetime,
money int)
insert into sale values (1,'西瓜','2015-01-01',10)
insert into sale values (2,'香蕉','2015-01-05',20)
insert into sale values (3,'苹果','2015-02-01',60)
insert into sale values (4,'葡萄','2015-02-23',345)
insert into sale values (5,'柚子','2015-04-23',10)
insert into sale values (6,'牛奶','2015-05-12',67)
insert into sale values (7,'地瓜','2015-06-01',10)
insert into sale values (8,'薯仔','2015-07-01',10)
2、执行语句:
with t as
( select '2015-'+right('0'+cast(number+1 as varchar),2) number from master..spt_values where type='p' and number<=11 )
select t.number month,SUM(isnull(b.money,0)) money
from t left join sale b on
t.number=CONVERT(varchar(7),b.sdate,120)
group by t.number
结果截图:
㈢ SQL 计算月累计的两个方法
Part1:求累加和
-- 方法一:sum() over函数
select month_id,sum(num) over(order by month_id)
from public.test_sum_over;
-- 方法二:列子查询
select month_id
,(select sum(num) from public.test_sum_over t2 where t2.month_id<=t1.month_id)
from public.test_sum_over t1
㈣ 求SQL语句,我的表里记录了每天的销售产品及金额,我想统计一个月中每天的销售额,生成列表,语句怎么写
select 日期,count(金额)as 销售额 from 表名 group by 日期 按要求这样就成吧