我是 MYSQL 的新手,我有一个在 SQL 中创建的脚本,需要将该脚本转换为 MySQL 脚本:
;WITH cte (id, ApplicationName, ActivityDate, username) AS
(
SELECT
id
, ApplicationName
, ActivityDate
, ROW_NUMBER() OVER (PARTITION BY ApplicationName ORDER BY ActivityDate) username
FROM #temp
WHERE ApplicationName IN ('Login', 'Logout')
)
SELECT *
FROM #temp O
INNER JOIN (
SELECT
A1.id [IID]
, A2.id [OID]
, A2.ActivityDate
FROM cte A1
INNER JOIN cte A2 ON A1.username = A2.username
AND A1.ApplicationName = 'Login'
AND A2.ApplicationName = 'logout'
) Logout ON Logout.[IID] < O.id AND Logout.[OID] > o.id
WHERE NOT O.ApplicationName IN ('Login', 'Logout')
ORDER BY O.id
#temp 表的代码:
CREATE TABLE #temp (
id INT IDENTITY (1, 1)
, ActivityName VARCHAR(MAX)
, ApplicationName VARCHAR(100)
, ActivityDate DATETIME
, UserName VARCHAR(20)
)
INSERT INTO #temp
VALUES ('Successfully Login into Dynamic Portal Application', 'Login', '2013-05-20 13:22:21', 'kk'),
('Login Success into Liferay SAMl Application', 'Liferay SAMl Application', '2013-05-21 15:28:08', 'kk'),
('Login Success into Zoho SAMl Application', 'Zoho SAMl Application', '2013-05-21 15:28:08', 'kk'),
('Login Success into Intranet Non SAMl Application', 'Intranet Non SAMl Application', '2013-05-23 13:04:22', 'kk'),
('Logout Successfully from application', 'Logout', '2013-05-21 18:20:07', 'kk'),
('Successfully Login into Dynamic Portal Application', 'Login', '2013-05-20 14:22:21', 'kk'),
('Login Success into Liferay SAMl Application', 'Liferay SAMl Application', '2013-05-21 16:28:08', 'kk'),
('Login Success into Zoho SAMl Application', 'Zoho SAMl Application', '2013-05-21 16:29:08', 'kk'),
('Login Success into Intranet Non SAMl Application', 'Intranet Non SAMl Application', '2013-05-21 15:04:22', 'kk'),
('Logout Successfully from application', 'Logout', '2013-05-21 19:20:07', 'kk')
期望的输出:
ActivityName LoginTime LogOutTime
Liferay SAMl Application 2013-05-21 15:28:08 2013-05-21 18:20:07
Zoho SAMl Application 2013-05-21 15:28:08 2013-05-21 18:20:07
Intranet Non SAMl Application 2013-05-23 13:04:22 2013-05-21 18:20:07
Liferay SAMl Application 2013-05-21 16:28:08 2013-05-21 19:20:07
Zoho SAMl Application 2013-05-21 16:29:08 2013-05-21 19:20:07
Intranet Non SAMl Application 2013-05-21 15:04:22 2013-05-21 19:20:07
活动的注销时间将是应用程序的注销时间......