我有一个看起来像这样的表:
# ITEM, operating_system, error_code
B01NCUBH43, mac, ['E141202', 'E20012']
B01N4MN9PL, mac, ['E141202', 'E20012']
B06XKH8NXM, win, ['E141202', 'E20012']
B01N3AZUH8, mac, ['E00101', 'E141202']
B06XRRQ8QV, win, ['E00101', 'E141202']
B01NAZ2I1H, mac, ['E00101', 'E141202']
B06XKHJ39Q, mac, ['E141202', 'E20012']
我需要找到每个操作系统的每个错误代码的计数。我写了这个sql,它给了我正确的答案。但是,它不是通用且可扩展的解决方案,因为error_codes是硬编码的,并且将来可能会出现新的错误代码。
SELECT Platform,
E141202,
E20012
from
(SELECT
(CASE WHEN operating_system = 'Win'
THEN 'Windows'
WHEN operating_system = 'Mac'
THEN 'Mac'
END) Platform,
COUNT(CASE WHEN tag like 'liveItem' THEN ITEM END) total_runs,
COUNT(CASE WHEN error_code like '%E141202%' THEN ITEM END) E141202,
COUNT(CASE WHEN error_code like '%E20012%' THEN ITEM END) E20012
FROM CrossPlatformYield where tag = 'liveItem'
GROUP BY Platform)a;
有没有办法获取唯一错误代码列表并使用它来执行上述计算以获得每个操作系统的每个错误代码的计数?理想情况下,我不想使用MySQL查询来完成它。在最坏的情况下,我可以写一个脚本。
答案 0 :(得分:0)
你有一个糟糕的数据格式。你应该有一个单独的表,每个项目和每个错误一行。
如果列表中有错误数量的错误(例如最多2个),那么我建议:
select substring_index(substring_index(replace(replace(cpy.error_code, '[', ''), ']', ''), ',', n.n), ',', -1) as error_code,
operating_system, count(*)
from CrossPlatformYield cpy join
(select 1 as n union all select 2) n
on n.n <= length(cpy.error_code) - length(replace(cpy.error_code, ',', '')) + 1
where cpy.tag = 'liveItem'
group by error_code, operating_system;