我正在尝试计算我的列'routeorder'中有两个数字之间的记录数。该列包含以下信息:
RouteOrder
ACC-001-01
ACC-001-01
ACC-001-01
ACC-001-01
ACC-015-18
ACC-015-18
ACC-015-18
ACC-015-18
ACC-015-19
ACC-015-19
ACC-015-19
ACC-015-19
ACC-015-19
ACC-016-01
ACC-016-01
ACC-016-01
ACC-017-19
ACC-017-19
ACC-017-19
ACC-017-19
ACC-017-19
ACC-017-19
ACC-017-19
我需要获取'ACC-001'和'ACC-016'之间的记录数 有办法吗? 这就是我到目前为止所做的:
SELECT COUNT(*) as areatotals FROM Customers WHERE SUBSTRING(routeorder,LENGTH(routeorder),-3) like 'ACC'
感谢您的帮助。
答案 0 :(得分:2)
根据official documentation上给出的示例,您应该可以:
SELECT COUNT(*) as areatotals FROM Customers WHERE routeorder BETWEEN 'ACC-001' AND 'ACC-016';
这应该按照here所述触发字符串转换。
答案 1 :(得分:2)
试试这个
SELECT count(*) counts,SUBSTRING(SUBSTRING(RouteOrder , 5),1,3 ) as str
FROM Customers
GROUP BY str
HAVING str > 001 and str < 016
输出:
COUNTS STR
9 015
修改强>
如果第一部分正在改变,这里更简单,更符合您的需求。
SELECT count(*) counts,SUBSTRING(RouteOrder ,-6 ,3) as str
FROM Customers
GROUP BY str
HAVING str > 001 and str < 016
答案 2 :(得分:0)
你甚至不需要子串或强制转换任何东西。 BETWEEN
运算符也适用于字符串:SELECT COUNT(*) FROM test WHERE RouteOrder BETWEEN "ACC-002" AND "ACC-007"
http://sqlfiddle.com/#!2/9614c/1
如果您还要包含上限匹配,请使用SELECT COUNT(*) FROM test WHERE RouteOrder BETWEEN "ACC-002" AND "ACC-007" OR RouteOrder LIKE "ACC-007%"
(您需要的like
,因为在比较字符串时ACC-007-1
在技术上不小于或等于ACC-007
,这意味着,两者之间的运算符将排除它)