Derby Db行合并

时间:2013-05-12 18:55:52

标签: sql derby

我有以下derby sql语句:

    SELECT DISTINCT MEETINGS.MEETING_ID,
        MEETINGS.TITLE,
        RECORDING_FRAME.STARTTIME,
        RECORDING_FRAME.ENDTIME,
        {
                fn timestampdiff(SQL_TSI_FRAC_SECOND,
                RECORDING_FRAME.STARTTIME,
                RECORDING_FRAME.ENDTIME)
        } AS RUNTIMEFIELD_SESSIONDURATION
FROM APP.MEETINGS
JOIN APP.RECORDING ON
        MEETINGS.MEETING_ID=RECORDING.MEETINGID
JOIN APP.RECORDING_TRACK ON
        RECORDING.RECORDING_ID=RECORDING_TRACK.RECORDING_ID
JOIN APP.RECORDING_FRAME ON
        RECORDING.RECORDING_ID=RECORDING_FRAME.RECORDING_ID
WHERE RECORDING_FRAME.MODULE LIKE 'Recorder' AND
        MEETINGS.STATUS LIKE 'ENDED'

如果我执行此查询,我会得到以下结果:

MEETING_ID  TITLE                 STARTTIME            ENDTIME              RUNTIMEFIELD_SESSIONDURATION  
----------  --------------------  -------------------  -------------------  ----------------------------  
65536       abc                   2013-03-06 21:28:49  (null)               (null)                        
65536       abc                   (null)               2013-03-06 21:30:04  (null)                        
65537       fasfasf               2013-03-06 21:13:13  (null)               (null)                        
65537       fasfasf               (null)               2013-03-06 21:13:40  (null)                        
360448      fasfffa               2013-03-06 21:31:07  (null)               (null)                        
360448      fasfffa               (null)               2013-03-06 21:38:13  (null)                        
360448      fasfffa               (null)               (null)               (null)                        
360450      test                  2013-03-06 22:07:31  (null)               (null)                        
360450      test                  (null)               2013-03-06 22:14:13  (null)                        
1605632     test benno            2013-03-19 18:21:03  (null)               (null)                        
1605632     test benno            (null)               2013-03-19 18:28:02  (null)                        
1605632     test benno            (null)               (null)               (null)                        
3211281     Conference Unni       2013-03-28 11:03:34  (null)               (null)                        
3211281     Conference Unni       (null)               2013-03-28 11:12:49  (null)                        
3211281     Conference Unni       (null)               (null)               (null)                        
3211283     test                  2013-03-28 10:51:27  (null)               (null)                        
3211283     test                  (null)               2013-03-28 10:59:13  (null)                        
3211283     test                  (null)               (null)               (null)                        
3211284     test (Copy)           2013-03-28 11:02:57  (null)               (null)                        
3211284     test (Copy)           (null)               2013-03-28 11:03:08  (null)                        
3211285     test (Copy) (Kopie)   2013-03-28 18:24:35  (null)               (null)                        
3211285     test (Copy) (Kopie)   (null)               2013-03-28 18:24:58  (null)                        
3211288     ss test               2013-03-28 17:02:20  (null)               (null)                        
3211288     ss test               (null)               2013-03-28 17:34:33  (null)                        
3211288     ss test               (null)               (null)               (null)

现在,很明显此列表中的同一事件(例如,事件“abc”)有两个记录,starttime和endtime。如何修改这个derby sql语句,因此它会在单行中返回事件“abc”starttime和endtime?

2 个答案:

答案 0 :(得分:3)

而不是DISTINCT使用GROUP BYMAX(start)MAX(end)

您的查询将如下:

SELECT id, title, MAX(start), MAX(end)
FROM ....
JOIN...
WHERE ...
GROUP BY id, title

由于汇总功能会跳过NULL,而您每个事件只有1个开始和结束MAX将产生正确的结果。

编辑:持续时间计算

SELECT minmax.*, 
        {
          fn timestampdiff(SQL_TSI_FRAC_SECOND,
                minmax.StartTime,
                minmax.EndTime)
        } AS RUNTIMEFIELD_SESSIONDURATION
FROM 
    (SELECT id, title, MAX(start) As StartTime, MAX(end) As EndTime
    FROM ....
    JOIN...
    WHERE ...
    GROUP BY id, title) minmax

Derby docs中的表子查询部分:http://db.apache.org/derby/docs/10.2/ref/

答案 1 :(得分:1)

我看到它的方式,似乎表RECORDING_FRAME有2或3行与RECORDING_ID上的RECORDING_TRACK连接。一行的starttime不为null,但是endtime为null,一行的starttime为null,endtime为null,而一些记录的第三行的starttime和endtime为null。您需要做的是将union(多行)转换为连接(单行)。这是你如何做到的:

首先创建一个RECORDING_FRAME视图,其中只包含starttime不为null的行。

Create view APP.VW_RF_START as
 (Select RECORDING_ID, STARTTIME from RECORDING_FRAME
  where STARTTIME is not null) 

现在创建一个视图,其中只包含RECORDING_FRAME的记录,其中endtime不为null。

Create view APP.VW_RF_END as
 (Select RECORDING_ID, STARTTIME from RECORDING_FRAME
  where ENDTIME is not null) 

现在,如果您在recording_id上加入两个视图,那么您将在同一行中拥有starttime和endtime。 您的查询是:

SELECT DISTINCT 
 MEETINGS.MEETING_ID, MEETINGS.TITLE,
 VW_RF_START.STARTTIME, VW_RF_END.STARTTIME,
 {
            fn timestampdiff(SQL_TSI_FRAC_SECOND,
            RECORDING_FRAME.STARTTIME,
            RECORDING_FRAME.ENDTIME)
 } AS RUNTIMEFIELD_SESSIONDURATION
FROM APP.MEETINGS
JOIN APP.RECORDING ON
    MEETINGS.MEETING_ID=RECORDING.MEETINGID
JOIN APP.RECORDING_TRACK ON
    RECORDING.RECORDING_ID=RECORDING_TRACK.RECORDING_ID
JOIN APP.VW_RF_START ON
    RECORDING.RECORDING_ID=VW_RF_START.RECORDING_ID
JOIN APP.VW_RF_END ON
    RECORDING.RECORDING_ID=VW_RF_START.RECORDING_ID

在哪里RECORDING_FRAME.MODULE喜欢'记录器'和         MEETINGS.STATUS喜欢'结束'

如果您无法创建视图,则可以在查询中定义它们:

SELECT DISTINCT 
 MEETINGS.MEETING_ID, MEETINGS.TITLE,
 VW_RF_START.STARTTIME, VW_RF_END.STARTTIME,
 {
            fn timestampdiff(SQL_TSI_FRAC_SECOND,
            RECORDING_FRAME.STARTTIME,
            RECORDING_FRAME.ENDTIME)
 } AS RUNTIMEFIELD_SESSIONDURATION
FROM APP.MEETINGS
APP.RECORDING ON
    MEETINGS.MEETING_ID=RECORDING.MEETINGID
JOIN APP.RECORDING_TRACK ON
    RECORDING.RECORDING_ID=RECORDING_TRACK.RECORDING_ID
JOIN 
  (Select RECORDING_ID, STARTTIME from RECORDING_FRAME
  where ENDTIME is not null) as VW_RF_START
ON
    RECORDING.RECORDING_ID=VW_RF_START.RECORDING_ID
JOIN 
  (Select RECORDING_ID, STARTTIME from RECORDING_FRAME
  where ENDTIME is not null) as APP.VW_RF_END 
ON RECORDING.RECORDING_ID=VW_RF_START.RECORDING_ID
WHERE RECORDING_FRAME.MODULE LIKE 'Recorder' AND
    MEETINGS.STATUS LIKE 'ENDED'