taosdata / TDengine

High-performance, scalable time-series database designed for Industrial IoT (IIoT) scenarios
https://tdengine.com
GNU Affero General Public License v3.0
23.38k stars 4.86k forks source link

data statistics, how to use count() #13526

Closed muyu1285 closed 6 months ago

muyu1285 commented 2 years ago

如何进行数据的count统计操作? 超级表: CREATE STABLE IF NOT EXISTS tasklog (visit_time TIMESTAMP, ciid NCHAR(19), imei NCHAR(16), imsi NCHAR(15), mvno NCHAR(15), scnum NCHAR(20), city NCHAR(20), transplantid NCHAR(15), province NCHAR(10), taskname NCHAR(30), taskid INT, tasktype NCHAR(3), shortcode NCHAR(32), resultdata NCHAR(500)) TAGS (tag_name NCHAR(15));

子表: CREATE TABLE task_get_log USING tasklog TAGS ('任务领取日志');

数据示例: INSERT INTO task_get_log VALUES (NOW, '1495171396162310144', '*867185051524909', '460005733560227', '中国移动 GSM', '868126423010811', '宿州市', 'CCT8', '广西', '测试任务', 99, 'ivr', '123852', 'type=0;opt_sim=1;polling=1;');

需要统计的功能是province 和 city,也就是统计省市数据,每个省province 下面分别统计市的数据,在mysql中,是可以这样统计的: select province, city, count(visit_time) from task_get_log where visit_time > '2022-6-6 00:00:00' AND visit_time < '2022-6-6 23:59:59';

这个语句在TD中,会出现: invalid operation: aggregation function should not be mixed up with projection

需要的结果: province city count(visit_time) 广东 广州 100 广东 深圳 150 广西 南宁 50

是否可以帮我解决一下,我要如何实现这个功能??

ljluestc commented 1 year ago
SELECT province, city, COUNT(visit_time) AS visit_count
FROM task_get_log
WHERE visit_time > '2022-06-06 00:00:00' AND visit_time < '2022-06-06 23:59:59'
GROUP BY province, city;