简体   繁体   English

按列结果查询分组

[英]Query group by pair of column result

Currently my dataset consist of 4 columns, id , status , user_id , created_date目前我的数据集包含 4 列, idstatususer_idcreated_date

so after a while the data can be like this所以过一段时间数据可能是这样的

(1, 'LOGIN', '2019-07-16 07:06:55', 'Bob')
(2, 'LOGOUT', '2019-07-16 07:29:13', 'Bob')
(3, 'LOGIN', '2019-07-16 07:30:31', 'Bob')
(4, 'LOGOUT', '2019-07-16 07:49:50', 'Bob')
(5, 'LOGIN', '2019-07-16 08:05:55', 'Tom')
(6, 'LOGOUT', '2019-07-16 08:15:13', 'Tom')
(7, 'LOGIN', '2019-07-16 09:13:55', 'John')
(8, 'LOGOUT', '2019-07-16 09:20:13', 'John')

I am trying to make it like this我正在努力让它像这样

(1, '2019-07-16 07:06:55', '2019-07-16 07:29:13', 'Bob', 22.5800)
(2, '2019-07-16 07:30:31', '2019-07-16 07:49:50', 'Bob', 19.5800)
(3, '2019-07-16 08:05:55', '2019-07-16 08:15:13', 'Tom', 9.5800)
(4, '2019-07-16 09:13:55', '2019-07-16 09:20:13', 'John', 6.5800)

So this is the query that I came up with so far所以这是我到目前为止提出的查询

SELECT
  max(CASE WHEN action = 'LOGOUT'
    THEN A.action_date END)               AS login_date,
  CASE WHEN max(CASE WHEN action = 'LOGOUT'
    THEN A.action_date END) < max(CASE WHEN action = 'LOGOUT'
    THEN A.action_date END)
    THEN max(CASE WHEN action = 'LOGOUT'
      THEN A.action_date END)
  ELSE max(CASE WHEN action = 'LOGOUT'
    THEN A.action_date END) END           AS logout_date,
  A.full_name,
  CASE WHEN timestamp(max(CASE WHEN action = 'LOGOUT'
    THEN A.action_date END)) < timestamp(max(CASE WHEN action = 'LOGOUT'
    THEN A.action_date END)) OR max(CASE WHEN action = 'LOGOUT'
    THEN A.action_date END) IS NULL
    THEN 0
  ELSE
    (timestamp(max(CASE WHEN action = 'LOGOUT'
      THEN A.action_date END)) - timestamp(max(CASE WHEN action = 'LOGOUT'
      THEN A.action_date END))) / 100 END AS session_time
FROM (
       SELECT
         timestamp(created_date) AS action_date,
         name as full_name,
         status as action
       FROM `training_ground`.session
       WHERE status = 'LOGIN' OR status = 'LOGOUT'
       GROUP BY action, cast(action_date AS DATE), name
       ORDER BY action_date DESC) AS A
GROUP BY A.full_name
ORDER BY A.action_date DESC;

I have no idea how to differentiate first login-logout to the second login-logout session, with this query, I am only getting我不知道如何区分第一次登录注销和第二次登录注销会话,通过这个查询,我只得到

(1, '2019-07-16 07:06:55', '2019-07-16 07:29:13', 'Bob', 22.5800)
(2, '2019-07-16 08:05:55', '2019-07-16 08:15:13', 'Tom', 9.5800)
(3, '2019-07-16 09:13:55', '2019-07-16 09:20:13', 'John', 6.5800)

Is there a way to group login-logout as a set, so that I can differentiate every single pair of login-logout sequentially?有没有办法将 login-logout 分组为一组,以便我可以按顺序区分每一对 login-logout?

You can do it with a left join and then subtract the 2 dates:您可以使用左连接来完成,然后减去 2 个日期:

select 
  t.id, 
  t.action_date login_date, tt.action_date logout_date,
  t.user_id,
  (tt.action_date - t.action_date) / 100 session_time
from (
  select * from session where status = 'LOGIN'  
) t left join (
  select * from session where status = 'LOGOUT'    
) tt on tt.user_id = t.user_id and 
  tt.action_date = (
    select min(action_date) from session
    where status = 'LOGOUT' and user_id = t.user_id and action_date > t.action_date
  )

See the demo .请参阅演示
Results:结果:

| id  | login_date          | user_id | logout_date         | session_time |
| --- | ------------------- | ------- | ------------------- | ------------ |
| 1   | 2019-07-16 07:06:55 | Bob     | 2019-07-16 07:29:13 | 22.58        |
| 3   | 2019-07-16 07:30:31 | Bob     | 2019-07-16 07:49:50 | 19.19        |
| 5   | 2019-07-16 08:05:55 | Tom     | 2019-07-16 08:15:13 | 9.58         |
| 7   | 2019-07-16 09:13:55 | John    | 2019-07-16 09:20:13 | 6.58         |

Simple implementation over provided inputs.对提供的输入的简单实现。 i have data set like:我有如下数据集:

select id, stat[status],dater[Date], name[Name] from tablelogin

在此处输入图片说明 Data Extraction:数据提取:

Select t1.id, t1.dater[login], t2.dater[logout], t1.name[name], datediff(day,t1.dater, t2.dater)[days] from tablelogin t1, tablelogin t2 where t1.name=t2.name and t2.id>t1.id and t2.stat='logout'

在此处输入图片说明

you need to work on logic and control, when records grows the above query may produce different results.您需要处理逻辑和控制,当记录增长时,上述查询可能会产生不同的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM