是否可以基于文本函数在sql中进行计数?我想计算邮政编码的实例(类型为DECIMAL
),但是,有些行有5位数字的拉链,而其他行有9位数的拉链。
我当前的查询:
select patzip, count(patzip) as zip_count
from patdemo_t
where patstate='KY'
group by patzip
order by zip_count desc
答案 0 :(得分:0)
<强>已更新强>
SELECT SUBSTRING(CONVERT(VARCHAR(9),patzip), 1, 5) zip, COUNT(*) zip_count
FROM patdemo_t
WHERE patstate = 'KY'
GROUP BY SUBSTRING(CONVERT(VARCHAR(9), patzip), 1, 5)
ORDER BY zip DESC
示例输出:
| ZIP | ZIP_COUNT | --------------------- | 41017 | 2 | | 41016 | 1 |
这是 SQLFiddle 演示