修复业绩类型字段-根据品相表fl3更新.sql
11.3 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
-- ============================================
-- 修复业绩类型字段:根据品相表fl3字段更新所有相关表的F_PerformanceType
-- ============================================
-- 说明:
-- 1. 此SQL用于修复因品相表(lq_xmzl)fl3字段设置错误导致的业绩类型问题
-- 2. 会更新所有开单、耗卡、退卡相关表的F_PerformanceType字段
-- 3. 更新逻辑:根据品相表的fl3字段,重新设置所有相关表的业绩类型
-- 4. 注意:此SQL会覆盖原有的F_PerformanceType值,请确保品相表的fl3字段已正确修复
--
-- 执行前请确认:
-- 1. 品相表(lq_xmzl)的fl3字段已经修复正确
-- 2. 已备份相关数据表
-- 3. 建议在测试环境先执行验证
-- ============================================
-- ============================================
-- 1. 更新开单品项表(lq_kd_pxmx)
-- ============================================
-- 通过品项ID(px字段)关联品相表的fl3字段
UPDATE lq_kd_pxmx pxmx
INNER JOIN lq_xmzl xmzl ON pxmx.px = xmzl.F_Id
SET pxmx.F_PerformanceType = COALESCE(xmzl.fl3, '')
WHERE pxmx.F_IsEffective = 1;
-- ============================================
-- 2. 更新开单健康师业绩表(lq_kd_jksyj)
-- ============================================
-- 优先使用F_ItemId,如果没有则通过F_kdpxid关联开单品项表获取品项ID
UPDATE lq_kd_jksyj jksyj
LEFT JOIN lq_kd_pxmx pxmx ON jksyj.F_kdpxid = pxmx.F_Id
LEFT JOIN lq_xmzl xmzl ON COALESCE(jksyj.F_ItemId, pxmx.px) = xmzl.F_Id
SET jksyj.F_PerformanceType = COALESCE(xmzl.fl3, '')
WHERE jksyj.F_IsEffective = 1
AND xmzl.F_Id IS NOT NULL;
-- ============================================
-- 3. 更新开单科技部老师业绩表(lq_kd_kjbsyj)
-- ============================================
-- 优先使用F_ItemId,如果没有则通过F_kdpxid关联开单品项表获取品项ID
UPDATE lq_kd_kjbsyj kjbsyj
LEFT JOIN lq_kd_pxmx pxmx ON kjbsyj.F_kdpxid = pxmx.F_Id
LEFT JOIN lq_xmzl xmzl ON COALESCE(kjbsyj.F_ItemId, pxmx.px) = xmzl.F_Id
SET kjbsyj.F_PerformanceType = COALESCE(xmzl.fl3, '')
WHERE kjbsyj.F_IsEffective = 1
AND xmzl.F_Id IS NOT NULL;
-- ============================================
-- 4. 更新消耗品项表(lq_xh_pxmx)
-- ============================================
-- 通过品项ID(px字段)关联品相表的fl3字段
UPDATE lq_xh_pxmx pxmx
INNER JOIN lq_xmzl xmzl ON pxmx.px = xmzl.F_Id
SET pxmx.F_PerformanceType = COALESCE(xmzl.fl3, '')
WHERE (pxmx.F_IsEffective = 1 OR pxmx.F_IsEffective IS NULL);
-- ============================================
-- 5. 更新消耗健康师业绩表(lq_xh_jksyj)
-- ============================================
-- 优先使用F_ItemId,如果没有则通过F_kdpxid关联消耗品项表获取品项ID
UPDATE lq_xh_jksyj jksyj
LEFT JOIN lq_xh_pxmx pxmx ON jksyj.F_kdpxid = pxmx.F_Id
LEFT JOIN lq_xmzl xmzl ON COALESCE(jksyj.F_ItemId, pxmx.px) = xmzl.F_Id
SET jksyj.F_PerformanceType = COALESCE(xmzl.fl3, '')
WHERE (jksyj.F_IsEffective = 1 OR jksyj.F_IsEffective IS NULL)
AND xmzl.F_Id IS NOT NULL;
-- ============================================
-- 6. 更新消耗科技部老师业绩表(lq_xh_kjbsyj)
-- ============================================
-- 优先使用F_ItemId,如果没有则通过F_hkpxid关联消耗品项表获取品项ID
UPDATE lq_xh_kjbsyj kjbsyj
LEFT JOIN lq_xh_pxmx pxmx ON kjbsyj.F_hkpxid = pxmx.F_Id
LEFT JOIN lq_xmzl xmzl ON COALESCE(kjbsyj.F_ItemId, pxmx.px) = xmzl.F_Id
SET kjbsyj.F_PerformanceType = COALESCE(xmzl.fl3, '')
WHERE (kjbsyj.F_IsEffective = 1 OR kjbsyj.F_IsEffective IS NULL)
AND xmzl.F_Id IS NOT NULL;
-- ============================================
-- 7. 更新退卡明细表(lq_hytk_mx)
-- ============================================
-- 通过品项ID(px字段)关联品相表的fl3字段
UPDATE lq_hytk_mx mx
INNER JOIN lq_xmzl xmzl ON mx.px = xmzl.F_Id
SET mx.F_PerformanceType = COALESCE(xmzl.fl3, '')
WHERE mx.F_IsEffective = 1;
-- ============================================
-- 8. 更新退卡健康师业绩表(lq_hytk_jksyj)
-- ============================================
-- 优先使用F_ItemId,如果没有则通过F_CardReturn关联退卡明细表获取品项ID
UPDATE lq_hytk_jksyj jksyj
LEFT JOIN lq_hytk_mx mx ON jksyj.F_CardReturn = mx.F_Id
LEFT JOIN lq_xmzl xmzl ON COALESCE(jksyj.F_ItemId, mx.px) = xmzl.F_Id
SET jksyj.F_PerformanceType = COALESCE(xmzl.fl3, '')
WHERE jksyj.F_IsEffective = 1
AND xmzl.F_Id IS NOT NULL;
-- ============================================
-- 9. 更新退卡科技部老师业绩表(lq_hytk_kjbsyj)
-- ============================================
-- 优先使用F_ItemId,如果没有则通过F_CardReturn关联退卡明细表获取品项ID
UPDATE lq_hytk_kjbsyj kjbsyj
LEFT JOIN lq_hytk_mx mx ON kjbsyj.F_CardReturn = mx.F_Id
LEFT JOIN lq_xmzl xmzl ON COALESCE(kjbsyj.F_ItemId, mx.px) = xmzl.F_Id
SET kjbsyj.F_PerformanceType = COALESCE(xmzl.fl3, '')
WHERE kjbsyj.F_IsEffective = 1
AND xmzl.F_Id IS NOT NULL;
-- ============================================
-- 10. 验证更新结果(统计各表的业绩类型分布)
-- ============================================
SELECT
'lq_kd_pxmx' as table_name,
COUNT(*) as total_count,
SUM(CASE WHEN F_PerformanceType IS NOT NULL AND F_PerformanceType != '' THEN 1 ELSE 0 END) as '已设置',
SUM(CASE WHEN F_PerformanceType IS NULL OR F_PerformanceType = '' THEN 1 ELSE 0 END) as '未设置'
FROM lq_kd_pxmx
WHERE F_IsEffective = 1
UNION ALL
SELECT
'lq_kd_jksyj' as table_name,
COUNT(*) as total_count,
SUM(CASE WHEN F_PerformanceType IS NOT NULL AND F_PerformanceType != '' THEN 1 ELSE 0 END) as '已设置',
SUM(CASE WHEN F_PerformanceType IS NULL OR F_PerformanceType = '' THEN 1 ELSE 0 END) as '未设置'
FROM lq_kd_jksyj
WHERE F_IsEffective = 1
UNION ALL
SELECT
'lq_kd_kjbsyj' as table_name,
COUNT(*) as total_count,
SUM(CASE WHEN F_PerformanceType IS NOT NULL AND F_PerformanceType != '' THEN 1 ELSE 0 END) as '已设置',
SUM(CASE WHEN F_PerformanceType IS NULL OR F_PerformanceType = '' THEN 1 ELSE 0 END) as '未设置'
FROM lq_kd_kjbsyj
WHERE F_IsEffective = 1
UNION ALL
SELECT
'lq_xh_pxmx' as table_name,
COUNT(*) as total_count,
SUM(CASE WHEN F_PerformanceType IS NOT NULL AND F_PerformanceType != '' THEN 1 ELSE 0 END) as '已设置',
SUM(CASE WHEN F_PerformanceType IS NULL OR F_PerformanceType = '' THEN 1 ELSE 0 END) as '未设置'
FROM lq_xh_pxmx
WHERE F_IsEffective = 1 OR F_IsEffective IS NULL
UNION ALL
SELECT
'lq_xh_jksyj' as table_name,
COUNT(*) as total_count,
SUM(CASE WHEN F_PerformanceType IS NOT NULL AND F_PerformanceType != '' THEN 1 ELSE 0 END) as '已设置',
SUM(CASE WHEN F_PerformanceType IS NULL OR F_PerformanceType = '' THEN 1 ELSE 0 END) as '未设置'
FROM lq_xh_jksyj
WHERE F_IsEffective = 1 OR F_IsEffective IS NULL
UNION ALL
SELECT
'lq_xh_kjbsyj' as table_name,
COUNT(*) as total_count,
SUM(CASE WHEN F_PerformanceType IS NOT NULL AND F_PerformanceType != '' THEN 1 ELSE 0 END) as '已设置',
SUM(CASE WHEN F_PerformanceType IS NULL OR F_PerformanceType = '' THEN 1 ELSE 0 END) as '未设置'
FROM lq_xh_kjbsyj
WHERE F_IsEffective = 1 OR F_IsEffective IS NULL
UNION ALL
SELECT
'lq_hytk_mx' as table_name,
COUNT(*) as total_count,
SUM(CASE WHEN F_PerformanceType IS NOT NULL AND F_PerformanceType != '' THEN 1 ELSE 0 END) as '已设置',
SUM(CASE WHEN F_PerformanceType IS NULL OR F_PerformanceType = '' THEN 1 ELSE 0 END) as '未设置'
FROM lq_hytk_mx
WHERE F_IsEffective = 1
UNION ALL
SELECT
'lq_hytk_jksyj' as table_name,
COUNT(*) as total_count,
SUM(CASE WHEN F_PerformanceType IS NOT NULL AND F_PerformanceType != '' THEN 1 ELSE 0 END) as '已设置',
SUM(CASE WHEN F_PerformanceType IS NULL OR F_PerformanceType = '' THEN 1 ELSE 0 END) as '未设置'
FROM lq_hytk_jksyj
WHERE F_IsEffective = 1
UNION ALL
SELECT
'lq_hytk_kjbsyj' as table_name,
COUNT(*) as total_count,
SUM(CASE WHEN F_PerformanceType IS NOT NULL AND F_PerformanceType != '' THEN 1 ELSE 0 END) as '已设置',
SUM(CASE WHEN F_PerformanceType IS NULL OR F_PerformanceType = '' THEN 1 ELSE 0 END) as '未设置'
FROM lq_hytk_kjbsyj
WHERE F_IsEffective = 1;
-- ============================================
-- 11. 查看品相表fl3字段的值分布
-- ============================================
SELECT
'lq_xmzl.fl3 值分布' as description,
fl3 as performance_type,
COUNT(*) as count
FROM lq_xmzl
WHERE fl3 IS NOT NULL AND fl3 != ''
GROUP BY fl3
ORDER BY count DESC;
-- ============================================
-- 12. 查看各表业绩类型的值分布(用于对比验证)
-- ============================================
SELECT
'lq_kd_pxmx' as table_name,
F_PerformanceType as performance_type,
COUNT(*) as count
FROM lq_kd_pxmx
WHERE F_IsEffective = 1
AND F_PerformanceType IS NOT NULL AND F_PerformanceType != ''
GROUP BY F_PerformanceType
UNION ALL
SELECT
'lq_kd_jksyj' as table_name,
F_PerformanceType as performance_type,
COUNT(*) as count
FROM lq_kd_jksyj
WHERE F_IsEffective = 1
AND F_PerformanceType IS NOT NULL AND F_PerformanceType != ''
GROUP BY F_PerformanceType
UNION ALL
SELECT
'lq_kd_kjbsyj' as table_name,
F_PerformanceType as performance_type,
COUNT(*) as count
FROM lq_kd_kjbsyj
WHERE F_IsEffective = 1
AND F_PerformanceType IS NOT NULL AND F_PerformanceType != ''
GROUP BY F_PerformanceType
UNION ALL
SELECT
'lq_xh_pxmx' as table_name,
F_PerformanceType as performance_type,
COUNT(*) as count
FROM lq_xh_pxmx
WHERE (F_IsEffective = 1 OR F_IsEffective IS NULL)
AND F_PerformanceType IS NOT NULL AND F_PerformanceType != ''
GROUP BY F_PerformanceType
UNION ALL
SELECT
'lq_xh_jksyj' as table_name,
F_PerformanceType as performance_type,
COUNT(*) as count
FROM lq_xh_jksyj
WHERE (F_IsEffective = 1 OR F_IsEffective IS NULL)
AND F_PerformanceType IS NOT NULL AND F_PerformanceType != ''
GROUP BY F_PerformanceType
UNION ALL
SELECT
'lq_xh_kjbsyj' as table_name,
F_PerformanceType as performance_type,
COUNT(*) as count
FROM lq_xh_kjbsyj
WHERE (F_IsEffective = 1 OR F_IsEffective IS NULL)
AND F_PerformanceType IS NOT NULL AND F_PerformanceType != ''
GROUP BY F_PerformanceType
UNION ALL
SELECT
'lq_hytk_mx' as table_name,
F_PerformanceType as performance_type,
COUNT(*) as count
FROM lq_hytk_mx
WHERE F_IsEffective = 1
AND F_PerformanceType IS NOT NULL AND F_PerformanceType != ''
GROUP BY F_PerformanceType
UNION ALL
SELECT
'lq_hytk_jksyj' as table_name,
F_PerformanceType as performance_type,
COUNT(*) as count
FROM lq_hytk_jksyj
WHERE F_IsEffective = 1
AND F_PerformanceType IS NOT NULL AND F_PerformanceType != ''
GROUP BY F_PerformanceType
UNION ALL
SELECT
'lq_hytk_kjbsyj' as table_name,
F_PerformanceType as performance_type,
COUNT(*) as count
FROM lq_hytk_kjbsyj
WHERE F_IsEffective = 1
AND F_PerformanceType IS NOT NULL AND F_PerformanceType != ''
GROUP BY F_PerformanceType
ORDER BY table_name, count DESC;
-- ============================================
-- SQL脚本执行完成
-- ============================================
-- 说明:
-- 1. 此SQL会根据品相表(lq_xmzl)的fl3字段,更新所有相关表的F_PerformanceType字段
-- 2. 更新覆盖所有有效记录,不管原来的值是什么
-- 3. 执行后请查看验证结果,确认更新是否正确
-- 4. 如果发现数据异常,请及时回滚或联系开发人员