将SQL脚本转换为MySQl脚本

时间:2013-05-28 05:45:18

标签: mysql sql sql-server

我是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

活动的LogOut时间将是申请的退出时间....

1 个答案:

答案 0 :(得分:4)

试试这个 -

<强>查询:

CREATE TABLE temp (
  ID int AUTO_INCREMENT PRIMARY KEY,
  ActivityName varchar(500),
  ApplicationName varchar(100),
  ActivityDate datetime,
  UserName varchar(20)
);

INSERT INTO temp (ActivityName, ApplicationName, ActivityDate, UserName)
  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', 'kk10'),
  ('Login Success into Liferay SAMl Application', 'Liferay SAMl Application', '2013-05-21 16:28:08', 'kk10'),
  ('Login Success into Zoho SAMl Application', 'Zoho SAMl Application', '2013-05-21 16:29:08', 'kk10'),
  ('Login Success into Intranet Non SAMl Application', 'Intranet Non SAMl Application', '2013-05-21 15:04:22', 'kk10'),
  ('Logout Successfully from application', 'Logout', '2013-05-21 19:20:07', 'kk10');

SELECT t_.ApplicationName, t_.ActivityDate, t2_.ActivityDate
FROM temp t_
JOIN (
    SELECT 
          MAX(CASE WHEN t.ApplicationName = 'Login' THEN t.ID END) AS IID
        , MAX(CASE WHEN t.ApplicationName = 'Logout' THEN t.ID END) AS OID
        , MAX(t.ActivityDate) AS ActivityDate
    FROM temp t
    WHERE t.ApplicationName IN ('Login', 'Logout')
    GROUP BY t.UserName
) t2_ ON t2_.IID < t_.ID AND t2_.OID > t_.ID
WHERE t_.ApplicationName NOT IN ('Login', 'Logout')
ORDER BY t_.ID 

<强>输出:

ApplicationName                 ActivityDate            ActivityDate
------------------------------- ----------------------- -----------------------
Liferay SAMl Application        2013-05-21 15:28:08.000 2013-05-21 18:20:07.000
Zoho SAMl Application           2013-05-21 15:28:08.000 2013-05-21 18:20:07.000
Intranet Non SAMl Application   2013-05-23 13:04:22.000 2013-05-21 18:20:07.000
Liferay SAMl Application        2013-05-21 16:28:08.000 2013-05-21 19:20:07.000
Zoho SAMl Application           2013-05-21 16:29:08.000 2013-05-21 19:20:07.000
Intranet Non SAMl Application   2013-05-21 15:04:22.000 2013-05-21 19:20:07.000

<强>更新

SELECT t_.ApplicationName, t_.ActivityDate, t2_.ActivityDate
FROM temp t_
JOIN (
    SELECT 
          d1.ID AS IID
        , d2.ID AS OID
        , d2.ActivityDate
        , d2.UserName 
    FROM ( 
        SELECT  
              id
            , l.UserName
            , @curRow := @curRow + 1 AS row_number
        FROM temp l
        JOIN (SELECT @curRow := 0) r
        WHERE ApplicationName = 'Login'
    ) d1
    JOIN (
        SELECT  
              id
            , ActivityDate
            , l.UserName
            , @curRow2 := @curRow2 + 1 AS row_number
        FROM temp l
        JOIN (SELECT @curRow2 := 0) r
        WHERE ApplicationName = 'Logout'
    ) d2 ON d1.row_number = d2.row_number AND d1.UserName = d2.UserName
) t2_ ON t2_.IID < t_.ID AND t2_.OID > t_.ID AND t_.UserName = t2_.UserName
WHERE t_.ApplicationName NOT IN ('Login', 'Logout')
ORDER BY t_.ID