问题:我需要根据 unitlog_table 上的 UnitNo 获取用户名。
这是我的查询:
select distinct(avh.UnitNo),
isnull((select count(UnitNo) from unitlog_table
where UnitNo = avh.UnitNo
and timestamp between '2021-09-01 00:00:00.00' and '2021-09-01 08:00:00.00'
and speed >= 41),0) as overord,
isnull((select count(UnitNo) from unitlog_table
where UnitNo = avh.UnitNo
and timestamp between '2021-09-01 00:00:00.00' and '2021-09-01 08:00:00.00'
and speed >= 71),0) as overex ,
isnull((select count(UnitNo) from unitlog_table
where UnitNo = avh.UnitNo
and timestamp between '2021-09-01 00:00:00.00' and '2021-09-01 08:00:00.00'
and speed >= 91),0) as overc,
isnull((select count(UnitNo) from unitlog_table
where UnitNo = avh.UnitNo
and timestamp between '2021-09-01 00:00:00.00' and '2021-09-01 08:00:00.00'
and speed >= 41 and speed >= 71 and speed >= 91), 0) as ttover,
(select distinct(username)
from unit_table av
inner join users_table ut on ut.UnitNo = av.UnitNo
where UnitNo = avh.UnitNo) as username
from
unitlog_table avh
where
avh.UnitNo in (select UnitNo from unit_table where userid = '1122')
and timestamp between '2021-09-01 00:00:00.00' and '2021-09-01 08:00:00.00'
这是我的表 1 ( unitlog_table):
UnitNo timestamp speed
----------------------------------------
unit1 2021-09-01 07:36:21.00 7.72
unit1 2021-09-01 07:36:30.00 9.58
unit1 2021-09-01 07:37:16.00 7.2
unit1 2021-09-01 07:37:37.00 18.32
unit1 2021-09-01 07:38:18.00 1.85
unit1 2021-09-01 07:38:27.00 13.32
unit1 2021-09-01 07:38:45.00 12.94
unit1 2021-09-01 07:47:39.00 8.34
unit1 2021-09-01 07:48:07.00 30.04
unit1 2021-09-01 07:48:24.00 31.825
unit1 2021-09-01 07:49:06.00 30.26
unit1 2021-09-01 07:49:24.00 33.875
unit1 2021-09-01 07:49:33.00 33.54
unit1 2021-09-01 07:50:21.00 37.235
unit1 2021-09-01 07:50:43.00 15.22
unit1 2021-09-01 07:50:51.00 13.435
unit1 2021-09-01 07:51:24.00 17.03
unit1 2021-09-01 07:52:17.00 16.915
unit1 2021-09-01 07:52:33.00 18.435
unit1 2021-09-01 07:52:54.00 16.805
unit1 2021-09-01 07:53:15.00 76.225
unit1 2021-09-01 07:53:29.00 24.375
unit1 2021-09-01 07:54:21.00 29.925
unit1 2021-09-01 07:55:14.00 17.39
unit1 2021-09-01 07:55:29.00 22.975
unit1 2021-09-01 07:56:19.00 8.015
unit1 2021-09-01 07:56:28.00 42.68
unit1 2021-09-01 07:56:57.00 24.33
unit1 2021-09-01 07:57:16.00 33.575
unit1 2021-09-01 07:57:25.00 13.215
unit1 2021-09-01 07:57:39.00 31.245
unit1 2021-09-01 07:58:16.00 90.705
unit1 2021-09-01 07:58:31.00 58.665
unit1 2021-09-01 07:58:47.00 44.06
unit1 2021-09-01 07:59:16.00 40.32
unit2 2021-09-01 07:57:25.00 13.215
unit2 2021-09-01 07:57:39.00 31.245
unit2 2021-09-01 07:58:16.00 60.705
unit2 2021-09-01 07:58:31.00 58.665
unit2 2021-09-01 07:58:47.00 44.06
unit2 2021-09-01 07:59:16.00 40.32
unit3 2021-09-01 07:57:25.00 43.215
unit3 2021-09-01 07:57:39.00 71.245
unit3 2021-09-01 07:58:16.00 60.705
unit3 2021-09-01 07:58:31.00 98.665
unit3 2021-09-01 07:58:47.00 44.06
unit3 2021-09-01 07:59:16.00 40.32
这是我的表 2 ( unit_table):
UnitNo userid
--------------
unit1 1122
unit2 1122
unit3 1122
unit4 5577
unit5 5577
unit6 8888
这是我的表 3 ( users_table):
userid username
--------------------
1122 Rolly
1122 Efren
1122 Mark -- edited to match OP comment
5577 Bert
5577 Jay
8888 Eric
预期成绩: 用户名 userid UnitNo overord overex overc ttover 罗利 1122 单元 1 2 1 2 5 埃弗伦 1122 单元 2 1 1 1 3