统计当天、当月、12个月信息的SQL

该博客介绍了如何使用SQL查询统计过去12个月以及当天和当月的数据。通过LEFT JOIN和GROUP BY操作,分别计算了12个月的总数量,以及在特定条件下的工作订单数量。同时,提供了查询当天和当月收入总额的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

-- 12个月的数据 --
select ifnull(b.count, 0) count ,ifnull(c.count, 0) count2, a.month month from (
select 1 month union all
select 2 month union all
select 3 month union all
select 4 month union all
select 5 month union all
select 6 month union all
select 7 month union all
select 8 month union all
select 9 month union all
select 10 month union all
select 11 month union all
select 12 month
) a left join
(
select month(create_time) month, count(*) count from property_work_order
where
year(create_time)=year(now()) and del_flag = 0 and status != 4 and status != 8
group by date_format(create_time, '%Y-%m'), month
) b
on a.month=b.month left join
(
select month(create_time) month, count(*) count from property_work_order
where
year(create_time)=year(now()) and del_flag = 0 and status in (5,6)
group by date_format(create_time, '%Y-%m'), month
) c
on a.month=c.month order by month
-- 当天的数据 --
select ifnull(sum(a.fee_amount), 0) as shoukuantoday, 1 as id
from bill_list_detail a left join bill_list as b on a.pid = b.id
where a.del_flag = 0 and b.del_flag = 0 and b.pay_flag = 1 and b.confirmed = 1 and date_format(b.pay_date,'%Y-%m-%d')=date_format(curdate(),'%Y-%m-%d')
-- 当月的数据 --
select ifnull(sum(a.fee_amount), 0) as shoukuancurmonth, 1 as id
from bill_list_detail a left join bill_list as b on a.pid = b.id
where a.del_flag = 0 and b.del_flag = 0 and b.pay_flag = 1 and b.confirmed = 1 and date_format(b.pay_date,'%Y-%m')=date_format(curdate(),'%Y-%m')

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值