SQL中CASE结合COUNT统计pole_type结果异常的解决求助
Issue with Pole Type Count Accuracy in SQL Aggregation
I'm using the following SQL query to generate statistical data for specific customers:
SELECT COUNT(DISTINCT sur.`customer_id`) AS 'Survey Done' ,COUNT(CASE WHEN sn.operator_name LIKE '%Zong%' AND sn.`signal_strength` = 'No Signal' THEN 1 ELSE NULL END) AS 'Zong No Signal' ,COUNT(CASE WHEN sn.operator_name LIKE '%Mobilink%' AND sn.`signal_strength` = 'No Signal' THEN 1 ELSE NULL END) AS 'Mobilink No Signal' ,COUNT(CASE WHEN sn.operator_name LIKE '%Ufone%' AND sn.`signal_strength` = 'No Signal' THEN 1 ELSE NULL END) AS 'Ufone No Signal' ,COUNT(CASE WHEN sn.operator_name LIKE '%Telenor%' AND sn.`signal_strength` = 'No Signal' THEN 1 ELSE NULL END) AS 'Telenor No Signal' ,COUNT(CASE WHEN sur.`pole_type` LIKE '%Wall%' THEN 1 ELSE NULL END) AS 'Wall' ,COUNT(CASE WHEN sur.`pole_type` LIKE '%PC Pole%' THEN 1 ELSE NULL END) AS 'PC Pole' ,COUNT(CASE WHEN sur.`pole_type` LIKE '%Structure Pole%' THEN 1 ELSE NULL END) AS 'Structure pole' ,COUNT(CASE WHEN sur.`pole_type` LIKE '%Spon pole%' THEN 1 ELSE NULL END) AS 'Spon pole' ,sd.`sub_div_code` AS 'SD Code', sd.`name` AS 'SD Name', sd.`circle_name` AS 'Circle Name', sd.`division_name` AS 'Division Name' FROM `survey` sur INNER JOIN `survey_hesco_subdivision` sd ON sur.`sub_division` = sd.`sub_div_code` INNER JOIN `survey_networks` sn ON sur.`id` = sn.`survey_id` WHERE sur.`customer_id` IN ('37010185878', '37010718785', '37010718759', '37010357911', '37010673539', '37010673796', '37010672166', '37010672162') GROUP BY sd.`name`
All the count metrics are correct except for the ones related to pole_type:
,COUNT(CASE WHEN sur.`pole_type` LIKE '%Wall%' THEN 1 ELSE NULL END) AS 'Wall' ,COUNT(CASE WHEN sur.`pole_type` LIKE '%PC Pole%' THEN 1 ELSE NULL END) AS 'PC Pole' ,COUNT(CASE WHEN sur.`pole_type` LIKE '%Structure Pole%' THEN 1 ELSE NULL END) AS 'Structure pole' ,COUNT(CASE WHEN sur.`pole_type` LIKE '%Spon pole%' THEN 1 ELSE NULL END) AS 'Spon pole'
Currently, the query returns 10, 4, 24, 0 for these four fields respectively, but the actual correct counts should be 4, 1, 7, 0. I've tried replacing LIKE with = for exact matches, but the results are still incorrect. I've looked up relevant solutions but haven't found a fix yet. Could someone help me get the accurate statistical method for these pole types?
内容的提问来源于stack exchange,提问作者Moeez
相关产品推荐
相关产品推荐

