[已用]创建门店耗卡业绩统计视图.sql
5.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
-- =============================================
-- 门店耗卡业绩统计视图创建脚本
-- 功能:统计所有门店的目标业绩、耗卡业绩、完成率等关键指标
-- 作者:系统自动生成
-- 创建时间:2025年
-- =============================================
-- 先删除视图(如果存在)
DROP VIEW IF EXISTS v_store_consume_performance_simple;
-- =============================================
-- 创建门店耗卡业绩统计视图(当月数据)
-- =============================================
CREATE VIEW v_store_consume_performance_simple AS
SELECT
`md`.`F_Id` AS `store_id`,
`md`.`djmd` AS `store_code`,
`md`.`dm` AS `store_name`,
COALESCE(`md`.`xsyj`, 0) AS `target_performance`,
COALESCE(`consume`.`consume_performance`, 0) AS `actual_performance`,
(
CASE
WHEN (COALESCE(`md`.`xsyj`, 0) > 0) THEN
round(
(
(
COALESCE(`consume`.`consume_performance`, 0) / `md`.`xsyj`
) * 100
),
2
) ELSE 0
END
) AS `completion_rate`,
COALESCE(`consume`.`consume_count`, 0) AS `order_count`
FROM
(
`lqerp`.`lq_mdxx` `md`
LEFT JOIN (
SELECT
`hk`.`md` AS `md`,
sum(COALESCE(`px`.`F_TotalPrice`, 0)) AS `consume_performance`,
count(DISTINCT `hk`.`F_Id`) AS `consume_count`
FROM
(
`lqerp`.`lq_xh_hyhk` `hk`
LEFT JOIN `lqerp`.`lq_xh_pxmx` `px` ON ((`hk`.`F_Id` = `px`.`glkdbh`))
)
WHERE
(
(
YEAR(`hk`.`hksj`) = YEAR(curdate())
)
AND (
MONTH(`hk`.`hksj`) = MONTH(curdate())
)
AND (
isnull(`px`.`F_CreateTIme`)
OR (
(
YEAR(`px`.`F_CreateTIme`) = YEAR(curdate())
)
AND (
MONTH(`px`.`F_CreateTIme`) = MONTH(curdate())
)
)
)
)
GROUP BY
`hk`.`md`
) `consume` ON ((`md`.`F_Id` = `consume`.`md`))
)
ORDER BY
COALESCE(`consume`.`consume_performance`, 0) DESC;
-- =============================================
-- 添加视图注释
-- =============================================
ALTER VIEW v_store_consume_performance_simple COMMENT = '门店耗卡业绩统计视图(当月数据)- 统计所有门店的目标业绩、耗卡业绩、完成率等关键指标';
-- =============================================
-- 视图字段说明
-- =============================================
/*
字段说明:
- store_id: 门店ID(来自lq_mdxx.F_Id)
- store_code: 门店编码(来自lq_mdxx.djmd)
- store_name: 店名(来自lq_mdxx.dm)
- target_performance: 目标业绩/生命线(来自lq_mdxx.xsyj)
- actual_performance: 耗卡业绩(来自lq_xh_pxmx.F_TotalPrice的汇总)
- completion_rate: 完成率百分比(actual_performance / target_performance * 100)
- order_count: 耗卡记录数(有效耗卡记录数)
关联说明:
- 主表:lq_mdxx(门店信息表)
- 关联表:lq_xh_hyhk(耗卡记录表)+ lq_xh_pxmx(耗卡品项表)
- 关联条件:lq_mdxx.F_Id = lq_xh_hyhk.md, lq_xh_hyhk.F_Id = lq_xh_pxmx.glkdbh
- 关联方式:LEFT JOIN(确保所有门店都显示,包括没有耗卡记录的门店)
数据来源:
- 目标业绩:lq_mdxx.xsyj(目标-门店生命线)
- 耗卡业绩:lq_xh_pxmx.F_TotalPrice(金额合计)
- 耗卡记录数:lq_xh_hyhk表记录数统计
时间过滤:
- 耗卡日期:lq_xh_hyhk.hksj(当月)
- 品项创建时间:lq_xh_pxmx.F_CreateTIme(当月)
注意事项:
1. 使用金额合计(F_TotalPrice)而不是品项价格(pxjg)
2. 只统计当月的数据(耗卡日期和品项创建时间都是当月)
3. 即使目标业绩、耗卡业绩为0的门店也会显示
4. 完成率计算时,目标业绩为0的门店显示0%完成率
5. 按耗卡业绩降序排列,便于查看业绩排名
6. 确保耗卡品项也是当月创建的,避免历史数据干扰
*/
-- =============================================
-- 使用示例
-- =============================================
/*
-- 查看所有门店当月耗卡业绩
SELECT * FROM v_store_consume_performance_simple;
-- 查看华润店当月耗卡业绩
SELECT * FROM v_store_consume_performance_simple WHERE store_name LIKE '%华润%';
-- 查看已完成目标的门店(当月耗卡)
SELECT * FROM v_store_consume_performance_simple WHERE completion_rate >= 100;
-- 查看零耗卡业绩的门店(当月)
SELECT * FROM v_store_consume_performance_simple WHERE actual_performance = 0;
-- 按完成率排名(当月耗卡)
SELECT * FROM v_store_consume_performance_simple WHERE target_performance > 0 ORDER BY completion_rate DESC;
-- 查看当月耗卡业绩前10名门店
SELECT * FROM v_store_consume_performance_simple ORDER BY actual_performance DESC LIMIT 10;
-- 查看当月完成率低于50%的门店
SELECT * FROM v_store_consume_performance_simple WHERE completion_rate < 50 AND target_performance > 0;
*/