Navicat下生产报表及自动化处理

下列是我采用Navicat 12.0版本 做每天印度餐厅的报表自动生成报表

1. 打开Navicat编写好程序代码,并保证能正常运行不报错

GROUP_CONCAT函数用于将多个字符串连接成一个字符串,在拼接成字符串时就会存在拼接长度的问题,mysql 默认的拼接最大长度为1024 个字节,由于1024个字节会出现不够用的情况,所以有时需要去根据情况进行修改,方式如下。
MySQL group_concat函数参考链接
由于自己没有在MySQL配置文件中修改group_concat的拼接长度,所以要在函数开头补充修改参数:

#设置最大编排函数长度
SET GLOBAL group_concat_max_len=102400;
SET SESSION group_concat_max_len = 102400;

印度餐厅报表整理的数据整理代码如下:

-- 第一步:建立中间表1,将点法汇总到一列且用文字表示。同时用时间函数筛选最近1年的交易,计算每家店每种点法的具体交易数。
DROP TABLE IF EXISTS middle_list1;
CREATE TABLE IF NOT EXISTS middle_list1 AS
SELECT CONCAT(rice_tag,' ',curry_tag,' ',naan_tag) AS choices,brand_tag,
COUNT(DISTINCT `Order Number`) AS count_order_number
FROM(
SELECT a.*, 
case WHEN a.quantity>0 THEN 1 else 0 END AS rice_flag,
case WHEN a.quantity>0 THEN 1 else 0 END AS curry_flag,
case WHEN a.quantity>0 THEN 1 else 0 END AS naan_flag,
case WHEN a.rice_quantity>0 THEN 'rice' else '' END AS rice_tag,
case WHEN a.curry_quantity>0 THEN 'curry' else ' ' END AS curry_tag,
case WHEN a.naan_quantity>0 THEN 'naan' else ' ' END AS naan_tag
FROM(
SELECT brand_tag,`Order Number`,date,
SUM(Quantity) AS quantity,
SUM(Quantity*`Product Price`) AS total_price,
SUM(CASE WHEN `Item Name`LIKE '%rice%' THEN Quantity*`Product Price` ELSE 0 END) AS rice_sales,
SUM(CASE WHEN `Item Name`LIKE '%rice%' THEN Quantity ELSE 0 END) AS rice_quantity,
SUM(CASE WHEN `Item Name`LIKE '%curry%' THEN Quantity*`Product Price` ELSE 0 END) AS curry_sales,
SUM(CASE WHEN `Item Name`LIKE '%curry%' THEN Quantity ELSE 0 END) AS curry_quantity,
SUM(CASE WHEN `Item Name`LIKE '%naan%' THEN Quantity*`Product Price` ELSE 0 END) AS naan_sales,
SUM(CASE WHEN `Item Name`LIKE '%naan%' THEN Quantity ELSE 0 END) AS naan_quantity
FROM ds_indian_restaurants_orders_price_date
GROUP BY brand_tag,`Order Number`,date
)a
)b
WHERE b.date BETWEEN SUBDATE(CURDATE(),365) AND CURDATE()
GROUP BY CONCAT(rice_tag,curry_tag,naan_tag,''),brand_tag
ORDER BY CONCAT(rice_tag,curry_tag,naan_tag,''),brand_tag;


#第二步:计算每家店在此次运行中的数据起始日期和总交易数,计入中间表2,为之后关联统计做准备。
DROP TABLE IF EXISTS middle_list2;
CREATE TABLE IF NOT EXISTS middle_list2 AS
SELECT brand_tag,
MIN(date) AS start_time,
MAX(date) AS end_time,
COUNT(DISTINCT `Order Number`) AS count_order_number,
now() AS lastest_update_time
FROM ds_indian_restaurants_orders_price_date
WHERE date BETWEEN SUBDATE(CURDATE(),365) AND CURDATE()
GROUP BY brand_tag
ORDER BY brand_tag;

表1:
Navicat下生产报表及自动化处理_第1张图片
表2:
在这里插入图片描述
其中生成表2时报错:详细请看

--查找提示的错误:
select @@global.sql_mode
='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
--修正错误格式
set @@global.sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'

--修改完之后,表2能正常生成,是为了更改底层SQL里面的参数,不需要增加到报表代码中

联立2个表,形成表3:

第三步:将中间表1和中间表2关联后汇总计算表1中每种点法的占比,附上每家店此次运行的起始时间并加上本次任务执行完成的时间。
DROP TABLE IF EXISTS list3;

CREATE TABLE list3 AS
SELECT a.choices,b.start_time,
#计算出不同分店的所有点法的订单数
SUM(CASE when a.brand_tag='res_1' THEN a.count_order_number ELSE 0 END) AS res1_orders,
SUM(CASE when a.brand_tag='res_2' THEN a.count_order_number ELSE 0 END) AS res2_orders,
#计算出不同分店的所有点法的订单占该店所有订单数的比例
CONCAT(ROUND(SUM(CASE when a.brand_tag='res_1' THEN a.count_order_number ELSE 0 END)/MAX(CASE when a.brand_tag='res_1' THEN b.count_order_number ELSE 0 END),2)*100,'%') AS res1_orders_rate,
CONCAT(ROUND(SUM(CASE when a.brand_tag='res_2' THEN a.count_order_number ELSE 0 END)/MAX(CASE when a.brand_tag='res_2' THEN b.count_order_number ELSE 0 END),2)*100,'%') AS res2_orders_rate,
b.end_time,b.lastest_update_time
FROM middle_list1 a
JOIN middle_list2 b ON
a.brand_tag=b.brand_tag
GROUP BY a.choices,b.start_time,b.end_time
ORDER BY a.choices;

Navicat下生产报表及自动化处理_第2张图片

2、新建报表

在这里插入图片描述在这里插入图片描述
在这里插入图片描述Navicat下生产报表及自动化处理_第3张图片
Navicat下生产报表及自动化处理_第4张图片
Navicat下生产报表及自动化处理_第5张图片
粘贴上述三段代码:

注意:该段代码需要直接查询出来的对象用来作为报表的引用,不能带有creat table的字样,不然不能生产报表,所以上面的代码做了部分修改:

DROP TABLE IF EXISTS middle_list1;
CREATE TABLE IF NOT EXISTS middle_list1 AS
SELECT CONCAT(rice_tag,' ',curry_tag,' ',naan_tag) AS choices,brand_tag,
COUNT(DISTINCT `Order Number`) AS count_order_number
FROM(
SELECT a.*, 
case WHEN a.quantity>0 THEN 1 else 0 END AS rice_flag,
case WHEN a.quantity>0 THEN 1 else 0 END AS curry_flag,
case WHEN a.quantity>0 THEN 1 else 0 END AS naan_flag,
case WHEN a.rice_quantity>0 THEN 'rice' else '' END AS rice_tag,
case WHEN a.curry_quantity>0 THEN 'curry' else ' ' END AS curry_tag,
case WHEN a.naan_quantity>0 THEN 'naan' else ' ' END AS naan_tag
FROM(
SELECT brand_tag,`Order Number`,date,
SUM(Quantity) AS quantity,
SUM(Quantity*`Product Price`) AS total_price,
SUM(CASE WHEN `Item Name`LIKE '%rice%' THEN Quantity*`Product Price` ELSE 0 END) AS rice_sales,
SUM(CASE WHEN `Item Name`LIKE '%rice%' THEN Quantity ELSE 0 END) AS rice_quantity,
SUM(CASE WHEN `Item Name`LIKE '%curry%' THEN Quantity*`Product Price` ELSE 0 END) AS curry_sales,
SUM(CASE WHEN `Item Name`LIKE '%curry%' THEN Quantity ELSE 0 END) AS curry_quantity,
SUM(CASE WHEN `Item Name`LIKE '%naan%' THEN Quantity*`Product Price` ELSE 0 END) AS naan_sales,
SUM(CASE WHEN `Item Name`LIKE '%naan%' THEN Quantity ELSE 0 END) AS naan_quantity
FROM ds_indian_restaurants_orders_price_date
GROUP BY brand_tag,`Order Number`,date
)a
)b
WHERE b.date BETWEEN SUBDATE(CURDATE(),365) AND CURDATE()
GROUP BY CONCAT(rice_tag,curry_tag,naan_tag,''),brand_tag
ORDER BY CONCAT(rice_tag,curry_tag,naan_tag,''),brand_tag;
#第二步:计算每家店在此次运行中的数据起始日期和总交易数,计入中间表2,为之后关联统计做准备。
DROP TABLE IF EXISTS middle_list2;
CREATE TABLE IF NOT EXISTS middle_list2 AS
SELECT brand_tag,
MIN(date) AS start_time,
MAX(date) AS end_time,
COUNT(DISTINCT `Order Number`) AS count_order_number,
now() AS lastest_update_time
FROM ds_indian_restaurants_orders_price_date
WHERE date BETWEEN SUBDATE(CURDATE(),365) AND CURDATE()
GROUP BY brand_tag
ORDER BY brand_tag;
-- SQL优化流程:
-- 第三步:将中间表1和中间表2关联后汇总计算表1中每种点法的占比,附上每家店此次运行的起始时间并加上本次任务执行完成的时间。
SELECT a.choices,b.start_time,
#计算出不同分店的所有点法的订单数
SUM(CASE when a.brand_tag='res_1' THEN a.count_order_number ELSE 0 END) AS res1_orders,
SUM(CASE when a.brand_tag='res_2' THEN a.count_order_number ELSE 0 END) AS res2_orders,
#计算出不同分店的所有点法的订单占该店所有订单数的比例
CONCAT(ROUND(SUM(CASE when a.brand_tag='res_1' THEN a.count_order_number ELSE 0 END)/MAX(CASE when a.brand_tag='res_1' THEN b.count_order_number ELSE 0 END),2)*100,'%') AS res1_orders_rate,
CONCAT(ROUND(SUM(CASE when a.brand_tag='res_2' THEN a.count_order_number ELSE 0 END)/MAX(CASE when a.brand_tag='res_2' THEN b.count_order_number ELSE 0 END),2)*100,'%') AS res2_orders_rate,
b.end_time,b.lastest_update_time
FROM middle_list1 a
JOIN middle_list2 b ON
a.brand_tag=b.brand_tag
GROUP BY a.choices,b.start_time,b.end_time
ORDER BY a.choices;

将需要的字段拖进相应的位置并作调整
Navicat下生产报表及自动化处理_第6张图片
修改表头及内容格式,生成相应的报表,每次修改都需要调整表格的大小,以便刷新Navicat下生产报表及自动化处理_第7张图片Navicat下生产报表及自动化处理_第8张图片
点选预览,查看最终效果
Navicat下生产报表及自动化处理_第9张图片
最后保存报表,最终在报表栏会出现刚新做的报表
Navicat下生产报表及自动化处理_第10张图片

3、自动化报表

Navicat下生产报表及自动化处理_第11张图片
Navicat下生产报表及自动化处理_第12张图片

注意:若需要发送附件记得点选添加附件,点了是没反应的(这是自然现象)

Navicat下生产报表及自动化处理_第13张图片
Navicat下生产报表及自动化处理_第14张图片
邮箱设置如下,我用自己的QQ邮箱发送到163邮箱做测试:根据下列要求Navicat下生产报表及自动化处理_第15张图片

设置任务触发器:

Navicat下生产报表及自动化处理_第16张图片
测试发送
Navicat下生产报表及自动化处理_第17张图片

你可能感兴趣的:(数据分析,SQL)