如何使用不同的条件在派生表中创建变量

时间:2015-10-03 21:19:25

标签: mysql-workbench

我想生成一个表,我需要的所有变量都来自派生的临时表。现在,我需要计算不同条件下的时差,我需要处理2个问题: 1.如何在派生表中创建变量。 2.如何使用不同的条件将变量分成2个变量。

请注意,我在下面提供的sql语句是简化声明,请注意有助于您理解问题的注释。

提前感谢任何提示。

这是SQL语句:

Select id, name, offline_time, Process_time from 

## in the derived table, use the difference of createon in table a and b to calculte the offline_time and Process_time ##
## when {a.id = st.id AND a.activityid = 5008 and a.sessiontype = 7} a.creaton = a.creaton1 ##
## when {b.activityid = 5011} b.creaton = b.creaton1 ##

(select 
(UNIX_TIMESTAMP(MAX(a.createdon)) - UNIX_TIMESTAMP(MAX(b.createdon))) as 'Offline_Time',
(UNIX_TIMESTAMP(MAX(a.createdon1)) - UNIX_TIMESTAMP(MAX(b.createdon1))) as 'Process_Time',
q.id, 
q.name,
a.sessiontype
FROM tv_sessiontimer st
    LEFT JOIN sessionactivity_log a ON (a.id = st.id AND a.activityid in (5004,5008) and a.sessiontype IN (3,4,5,6,7))
    LEFT JOIN sessionactivity_log b ON (a.ssessionId = b.sessionId AND a.id = b.id AND b.activityid in (5003,5011))
    INNER JOIN tv_offline_request q ON q.id = a.sessionid
    LEFT JOIN tv_subject sub ON (sub.id = q.subjectid AND sub.SortKey > 10800 AND sub.sortkey < 30200)
    )

0 个答案:

没有答案