sql - How to filter integer with specific numbers in sql
问题描述
So in one of my assignment, they wanted me to sort out numbers that ends with "00" for the queries. This means no "00" on middle, just at the end.
For example: 100, 200, 300, etc. This is how I want it to come out.
The problem is, I have no idea how to do it. I have try to convert it to string with CASE and CONVERT, but it gives me error instead. Do I convert it first on Select then sort it later on, or should it all happen at the same time?
Or do you have any better suggestion?
This is some of my attempts at doing it.
SELECT name, SUM(count) AS freq
FROM [baby.baby_names]
WHERE gender = 'M' AND year < 2010 AND freq like '%[00]'
GROUP BY name
ORDER BY freq DESC;
Thank you for your time.
解决方案
Check if a number ends with two zeros is equivalent to checking if it is a multiple of 100. So you could just use the modulo operator:
where freq % 100 = 0
Some databases dot not support %
and want mod()
instead:
where mod(freq, 100) = 0
推荐阅读
- java - 如何使用 JAXB 转换 Java 对象中的 xml 数据?
- python - 在 Pandas 的列中模拟“”(单引号)?
- android - CardView 在 RecyclerView 中未正确显示
- swift - 嵌套的高阶函数
- javascript - 如何在纯 JavaScript 中暂时禁用该事件?
- dart - Flutter desktop / go-flutter-desktop 在Windows平台打开Excel文件
- java - 线程本地仅初始化为零
- python-3.x - 无法删除开始和结束双引号,我将列值从 .xlsx 文件复制到剪贴板
- javascript - 使用 ajax 绘制 highcharts
- python - 如何设置支持开和关的简单网络服务器