这个是几个月前被问的问题(看笔记有这个功能),对方技术说很难,我当时倒是挺纳闷的,不是很容易吗。
直接上代码:
DELIMITER //
drop procedure If exIsts get_month_tb;
create procedure get_month_tb(In p_month varchar(10))
begIn
select td.*,ld.*,(td.cnt-ld.cnt)/ld.cnt tb from
(select c_hotel_name,sum(c_custom) cnt from test where c_date=p_month group by c_hotel_name) td
left joIn
(select c_hotel_name,sum(c_custom) cnt from test where c_date=date_add(p_month ,Interval -1 year) group by c_hotel_name) ld
on td.c_hotel_name=ld.c_hotel_name
end //
这里直接传入要查询的月份,就可以直接获得同比了:
call get_date_tb("2021-03")