㈠ 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 日期 按要求這樣就成吧