我试图基于三个不同的表生成一个报告,该表存储了系统进程的某种痕迹。 Source_Base表存储她的进程尝试以及上次调用的机械手的成功或失败结果。 Source_Robot表存储每个被调用的机器人的踪迹。最后,如果某个机器人发生了异常,该异常将存储在Robot_Exception表中。
在这里,我将尝试显示以下表格:
Source_Base (Showing only a portion of the real data)
----------------------------------------------------------------------
AccesID | OwnerAccessID | StartTime | EndTime | Success
----------------------------------------------------------------------
1248683283 | 0 | 09:10:23.237 | 09:10:26.727 | 0
1248683284 | 10013553 | 09:10:23.233 | 09:12:05.703 | 1
1248683285 | 0 | 14:24:32.037 | 14:24:39.313 | 0
1248683286 | 10013555 | 14:24:32.030 | 14:25:42.387 | 1
1248683287 | 0 | 14:44:47.747 | 14:44:50.973 | 1
1248683288 | 1248683287 | 14:44:47.747 | 14:44:51.697 | 1
Source_Robot
-----------------------------------------------------------------------------------
AccesID | OwnerAccessID | RobotID | StartTime | EndTime | Success
-----------------------------------------------------------------------------------
10013551 | 1248683283 | 11 | 09:10:27.587 | 09:11:17.605 | 0
10013552 | 10013551 | 12 | 09:11:18.186 | 09:11:18.219 | 0
10013553 | 10013552 | 13 | 09:11:18.323 | 09:12:05.003 | 1
10013554 | 1248683285 | 15 | 14:24:41.763 | 14:25:36.313 | 0
10013555 | 10013554 | 14 | 14:25:37.393 | 14:25:39.967 | 1
Robot_Exception
-----------------------------------------------------------------------
ExceptionID | OwnerAccessID | RobotID | StartTime | EndTime
-----------------------------------------------------------------------
999993 | 10013554 | 15 | 14:24:41.763 | 14:25:36.313
999992 | 10013552 | 12 | 09:11:18.189 | 09:11:18.217
999991 | 10013551 | 11 | 09:10:27.593 | 09:11:17.603
现在我希望有这样的报告:
--------------------------------------------------------------------------------------
AccesID | OwnerAccessID | RobotID | Success | Exception | Duration
--------------------------------------------------------------------------------------
1248683283 | 0 | NULL | 0 | NULL | (datediff)
10013551 | 1248683283 | 11 | 0 | 999991 | (datediff)
10013552 | 10013551 | 12 | 0 | 999992 | (datediff)
10013553 | 10013552 | 13 | 1 | NULL | (datediff)
1248683284 | 10013553 | NULL | 1 | NULL | (datediff)
1248683285 | 0 | NULL | 0 | NULL | (datediff)
10013554 | 1248683285 | 15 | 0 | 999993 | (datediff)
10013555 | 10013554 | 14 | 1 | NULL | (datediff)
1248683286 | 10013555 | NULL | 1 | NULL | (datediff)
1248683288 | 1248683287 | NULL | 1 | NULL | (datediff)
1248683287 | 0 | NULL | 1 | NULL | (datediff)
我尝试解决CTE问题,但是报告中未显示某些值,我确定我的上一个查询是错误的。否则使用CTE是不正确的方法。
WITH Source_Base_CTE (AccesID, OwnerAccessID, RobotID, StartTime, EndTime, Duration, Success)
AS
(SELECT
AccesID,
OwnerAccessID,
0 as RobotID,
StartTime,
EndTime,
datediff (second, StartTime, EndTime) as Duration,
Success
FROM
Source_Base
),
Source_Robot_CTE (AccesID, OwnerAccessID, RobotID, StartTime, EndTime, Duration, Success)
AS
(SELECT
AccesID,
OwnerAccessID,
RobotID,
StartTime,
EndTime,
datediff (second, StartTime, EndTime) as Duration,
Success
FROM
Source_Robot
)
SELECT * FROM Source_Base_CTE WHERE StartTime > '00:00:00.00'
UNION ALL
SELECT * FROM Source_Robot_CTE WHERE StartTime > '00:00:00.00' AND OwnerAccessID in (SELECT AccesID FROM Source_Base_CTE)
ORDER BY StartTime ASC
结果:
AccesID OwnerAccessID RobotID StartTime EndTime Duration Success
1248683284 10013551 0 09:10:23.233 09:12:05.703 102 1
1248683283 0 0 09:10:23.237 09:10:26.727 3 0
10013551 1248683283 12 09:10:27.587 09:11:17.603 50 0
1248683286 10013554 0 14:24:32.030 14:25:42.387 70 1
1248683285 0 0 14:24:32.037 14:24:39.313 7 0
10013553 1248683285 15 14:24:41.763 14:25:36.313 55 0
1248683288 1248683287 0 14:44:47.747 14:44:51.697 4 1
1248683287 0 0 14:44:47.747 14:44:50.973 3 1
1248683289 0 0 16:23:35.930 16:23:38.857 3 0
1248683290 10013557 0 16:23:35.930 16:24:34.197 59 1
10013555 1248683289 12 16:23:39.727 16:24:17.860 38 0
如果有人可以给我一种方法,我将不胜感激。
答案 0 :(得分:1)
运行:https://www.db-fiddle.com/f/iDtVPvCnXV6E92CzyWNjWK/0
在此处按Run(运行)以查看11行的结果。
SQL:
with source as (
select AccesID, OwnerAccessID, null RobotID, StartTime, EndTime, Success from Source_Base union
select AccesID, OwnerAccessID, RobotID, StartTime, EndTime, Success from Source_Robot
)
select AccesID,OwnerAccessID, RobotID, Success,
(select ExceptionID from Robot_Exception e where e.OwnerAccessID=source.AccesID) Exception,
round(extract(epoch from EndTime)-extract(epoch from StartTime)) duration,
starttime
from source
order by StartTime;
使用示例表:
create table source_base ( accesid numeric, owneraccessid numeric, starttime timestamp, endtime timestamp, success numeric );
insert into source_base values
(1248683283, 0, '2019-04-10 09:10:23.237', '2019-04-10 09:10:26.727', 0),
(1248683284, 10013553, '2019-04-10 09:10:23.233', '2019-04-10 09:12:05.703', 1),
(1248683285, 0, '2019-04-10 14:24:32.037', '2019-04-10 14:24:39.313', 0),
(1248683286, 10013555, '2019-04-10 14:24:32.030', '2019-04-10 14:25:42.387', 1),
(1248683287, 0, '2019-04-10 14:44:47.747', '2019-04-10 14:44:50.973', 1),
(1248683288, 1248683287, '2019-04-10 14:44:47.747', '2019-04-10 14:44:51.697', 1);
create table source_robot ( accesid numeric, owneraccessid numeric, robotid numeric, starttime timestamp, endtime timestamp, success numeric );
insert into source_robot values
(10013551, 1248683283, 11, '2019-04-10 09:10:27.587', '2019-04-10 09:11:17.605', 0),
(10013552, 10013551, 12, '2019-04-10 09:11:18.186', '2019-04-10 09:11:18.219', 0),
(10013553, 10013552, 13, '2019-04-10 09:11:18.323', '2019-04-10 09:12:05.003', 1),
(10013554, 1248683285, 15, '2019-04-10 14:24:41.763', '2019-04-10 14:25:36.313', 0),
(10013555, 10013554, 14, '2019-04-10 14:25:37.393', '2019-04-10 14:25:39.967', 1);
create table robot_exception (exceptionid numeric, owneraccessid numeric, robotid numeric, starttime timestamp, endtime timestamp);
insert into robot_exception values
(999993, 10013554, 15, '2019-04-10 14:24:41.763', '2019-04-10 14:25:36.313'),
(999992, 10013552, 12, '2019-04-10 09:11:18.189', '2019-04-10 09:11:18.217'),
(999991, 10013551, 11, '2019-04-10 09:10:27.593', '2019-04-10 09:11:17.603');