select count(1) yjs, case when substr(t.YJB011, 0, 4) = '6301' then '西宁市' when substr(t.YJB011, 0, 4) = '6302' then '海东市' when substr(t.YJB011, 0, 4) = '6322' then '海北藏族自治州' when substr(t.YJB011, 0, 4) = '6323' then '黄南藏族自治州' when substr(t.YJB011, 0, 4) = '6325' then '海南藏族自治州' when substr(t.YJB011, 0, 4) = '6326' then '果洛藏族自治州' when substr(t.YJB011, 0, 4) = '6327' then '玉树藏族自治州' when substr(t.YJB011, 0, 4) = '6328' then '海西蒙古族藏族自治州' else '其他' end qhName from GZZF_YJSJ t group by t.YJB011; 优化这条语句 只显示某个区划下的数量
时间: 2024-01-14 08:03:31 浏览: 264
要只显示某个区划下的数量,你可以使用HAVING子句来过滤结果。以下是优化后的SQL语句:
```sql
SELECT COUNT(1) AS yjs,
CASE
WHEN SUBSTR(t.YJB011, 0, 4) = '6301' THEN '西宁市'
WHEN SUBSTR(t.YJB011, 0, 4) = '6302' THEN '海东市'
WHEN SUBSTR(t.YJB011, 0, 4) = '6322' THEN '海北藏族自治州'
WHEN SUBSTR(t.YJB011, 0, 4) = '6323' THEN '黄南藏族自治州'
WHEN SUBSTR(t.YJB011, 0, 4) = '6325' THEN '海南藏族自治州'
WHEN SUBSTR(t.YJB011, 0, 4) = '6326' THEN '果洛藏族自治州'
WHEN SUBSTR(t.YJB011, 0, 4) = '6327' THEN '玉树藏族自治州'
WHEN SUBSTR(t.YJB011, 0, 4) = '6328' THEN '海西蒙古族藏族自治州'
ELSE '其他' END AS qhName
FROM GZZF_YJSJ t
GROUP BY t.YJB011
HAVING qhName = '西宁市'; -- 替换 '西宁市' 为要显示的区划名称
```
请注意,根据你的需求,你需要将 `'西宁市'` 替换为你想要显示数量的具体区划名称。
阅读全文