我有一个表有重复的日期和时间,有些日期有重复的时间,但是真的想用SQL查询把它算作1,distinct不工作,也不知道怎么做这个任何帮助?目的是计算每天每个寄存器号的下载量。例如,2018-01-08具有重复的开始时间,应计为1.
我的查询不是这样做的。
Select StartDate, Starttime, count(*) AS TOTALDOWNLOAD, RegisterNumber
FROM `SAMPLE.csv`
WHERE (MaintenanceFlightTime > 0)
group by StartDate,Starttime, RegisterNumber
RegisterNumber StartDate StartTime
A 2017-11-27 19:22:17
A 2017-11-27 19:45:39
B 2017-11-29 14:09:30
A 2017-11-28 21:51:38
A 2017-11-30 17:09:34
B 2017-12-01 15:19:35
A 2017-12-01 19:31:42
B 2017-12-02 14:59:28
B 2017-12-03 17:38:39
B 2017-12-03 19:09:19
B 2017-12-04 10:16:24
B 2017-12-04 13:43:37
B 2017-12-05 06:37:22
A 2017-12-05 10:24:37
A 2017-12-05 14:40:19
B 2017-12-05 19:44:06
A 2017-12-06 08:26:55
A 2017-12-06 06:30:38
A 2017-12-06 11:35:41
A 2018-01-08 06:40:52
A 2018-01-08 06:40:52
A 2018-01-08 06:40:52
A 2018-01-08 06:40:52
A 2018-01-08 06:40:52
A 2018-01-08 06:40:52
A 2018-01-08 06:40:52
A 2018-01-08 06:40:52
答案 0 :(得分:2)
试试这个:
at
org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:137) ~[spring-boot-2.0.0.M3.jar!/:2.0.0.M3]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-5.0.0.RC3.jar!/:5.0.0.RC3]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:122) ~[spring-boot-2.0.0.M3.jar!/:2.0.0.M3]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) [spring-boot-2.0.0.M3.jar!/:2.0.0.M3]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:386) [spring-boot-2.0.0.M3.jar!/:2.0.0.M3]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.0.M3.jar!/:2.0.0.M3]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1245) [spring-boot-2.0.0.M3.jar!/:2.0.0.M3]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1233) [spring-boot-2.0.0.M3.jar!/:2.0.0.M3]
at com.zophop.gps.ZophopFrontEndServer.main(ZophopFrontEndServer.java:16) [classes!/:0.0.3]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_171]
答案 1 :(得分:0)
您应该只在结果集中包含group by
所需的行。因此,如果您想计算每RegisterNumber
天的数量,您可以使用:
SELECT RegisterNumber, COUNT(DISTINCT StartDate)
FROM `SAMPLE.csv`
WHERE MaintenanceFlightTime > 0
GROUP BY RegisterNumber ;
如果要计算日期和时间的不同组合,则某些数据库允许您将COUNT(DISTINCT)
与多个参数一起使用:
SELECT RegisterNumber, COUNT(DISTINCT StartDate, StartTime)
FROM `SAMPLE.csv`
WHERE MaintenanceFlightTime > 0
GROUP BY RegisterNumber ;
在其他情况下,您需要使用字符串连接或日期/时间函数将值组合到单个列中。在极端情况下,您还可以使用子查询:
SELECT RegisterNumber, COUNT(*)
FROM (SELECT DISTINCT RegisterNumber, StartDate, StartTime
FROM `SAMPLE.csv`
WHERE MaintenanceFlightTime > 0
) s
GROUP BY RegisterNumber ;