如何return只统计一列的结果?
How to return a count result of only one column?
我有一个 table 这样的:
并且想按时间段(日期字段)创建一个 selection,我可以在其中 return 寄存器,并且一列寄存器总数仅与一列匹配。
例如:
如果我使用这个查询:
SELECT date, product_type, operation, unit
FROM table
WHERE date BETWEEN '2019-08-26 00:00:00' AND '2019-08-26 23:59:59';
必须return:
但我希望我可以 return 多一列,总操作数不分周期,如下所示:
在这种情况下,6 是 "ajoy" 在 table 中出现的频率。
重要!如果我 select 有两个或多个操作被 returned,那么查询也必须能够 return 它们的频率。
我使用了您的部分数据,并将在 table 中包含我所拥有的数据,但我相信这就是您想要的
create table mytab (job int, operations char(8), prod char(8),
ts_date datetime year to minute, unit int) lock mode row;
insert into mytab values(22, "ajoy","arrow","2020-05-11 08:51", 20);
insert into mytab values(22, "ajoy","arrow","2020-05-11 08:51", 20);
insert into mytab values(22, "ajoy","arrow","2020-05-11 08:51", 20);
insert into mytab values(22, "ajoy","arrow","2020-04-11 14:15", 20);
insert into mytab values(22, "ajoy","arrow","2020-04-11 14:15", 20);
insert into mytab values(22, "ajoy","arrow","2020-04-11 14:15", 20);
insert into mytab values(23, "dinn","curve","2020-05-11 08:51",1);
insert into mytab values(23, "dinn","point","2020-05-11 08:51",1);
insert into mytab values(23, "dinn","arrow","2020-04-11 08:51",1);
查询:
select job, operations, prod, ts_date, unit, (select count(*) from mytab b
where b.operations = a.operations) total_operation from mytab a where
a.ts_date between "2020-05-11 08:50" and "2020-05-11 08:59"
上面的查询给了我以下结果,我认为这是你要的:
job operations prod ts_date unit total_operation
22 ajoy arrow 2020-05-11 08:51 20 6
22 ajoy arrow 2020-05-11 08:51 20 6
22 ajoy arrow 2020-05-11 08:51 20 6
23 dinn curve 2020-05-11 08:51 1 3
23 dinn point 2020-05-11 08:51 1 3
此示例很小,没有 include/account 索引,您可以将其放在 table 上以加快查询性能。
我有一个 table 这样的:
并且想按时间段(日期字段)创建一个 selection,我可以在其中 return 寄存器,并且一列寄存器总数仅与一列匹配。
例如: 如果我使用这个查询:
SELECT date, product_type, operation, unit
FROM table
WHERE date BETWEEN '2019-08-26 00:00:00' AND '2019-08-26 23:59:59';
必须return:
但我希望我可以 return 多一列,总操作数不分周期,如下所示:
在这种情况下,6 是 "ajoy" 在 table 中出现的频率。
重要!如果我 select 有两个或多个操作被 returned,那么查询也必须能够 return 它们的频率。
我使用了您的部分数据,并将在 table 中包含我所拥有的数据,但我相信这就是您想要的
create table mytab (job int, operations char(8), prod char(8),
ts_date datetime year to minute, unit int) lock mode row;
insert into mytab values(22, "ajoy","arrow","2020-05-11 08:51", 20);
insert into mytab values(22, "ajoy","arrow","2020-05-11 08:51", 20);
insert into mytab values(22, "ajoy","arrow","2020-05-11 08:51", 20);
insert into mytab values(22, "ajoy","arrow","2020-04-11 14:15", 20);
insert into mytab values(22, "ajoy","arrow","2020-04-11 14:15", 20);
insert into mytab values(22, "ajoy","arrow","2020-04-11 14:15", 20);
insert into mytab values(23, "dinn","curve","2020-05-11 08:51",1);
insert into mytab values(23, "dinn","point","2020-05-11 08:51",1);
insert into mytab values(23, "dinn","arrow","2020-04-11 08:51",1);
查询:
select job, operations, prod, ts_date, unit, (select count(*) from mytab b
where b.operations = a.operations) total_operation from mytab a where
a.ts_date between "2020-05-11 08:50" and "2020-05-11 08:59"
上面的查询给了我以下结果,我认为这是你要的:
job operations prod ts_date unit total_operation
22 ajoy arrow 2020-05-11 08:51 20 6
22 ajoy arrow 2020-05-11 08:51 20 6
22 ajoy arrow 2020-05-11 08:51 20 6
23 dinn curve 2020-05-11 08:51 1 3
23 dinn point 2020-05-11 08:51 1 3
此示例很小,没有 include/account 索引,您可以将其放在 table 上以加快查询性能。