在2个表格中汇总小时和分钟,并以HH:MM显示结果

时间:2018-08-19 15:49:39

标签: sqlite

我有2个表,第一个表是主表,其左侧列为HH:MM格式,从右侧的列中获取hh和mm格式的信息

ACType   A     B        C      Ahr    Amin     Bhr    Bmin     Chr      Cmin

A320   12:34 85:45    07:23     12      34      85     45      7         23
B777   20:00 30:00    10:00     20      0       30     0      10         0

第二张表的列为hh和mm格式

ACType       Bhr   Bmin      Chr      Cmin

A320          10       20     46       31

如何获得最终结果:

ACType    A    B    C     Ahr     Amin   Bhr     Bmin     Chr    Cmin

A320  12:34  96:05 53:54  12       34     95      65       53      54
B777  20:00  30:00 10:00  20       0      30      0        10      0

2 个答案:

答案 0 :(得分:0)

SELECT
   first.ACType,
   first.A,
   (
     x.Bhr + x.Bmin / 60
   )
   || ':' || printf(" % 02d", x.Bmin % 60) AS B,
   (
     x.Chr + x.Cmin / 60
   )
   || ':' || printf(" % 02d", x.Cmin % 60) AS C,
   first.Ahr,
   first.Amin,
   x.Bhr,
   x.Bmin,
   x.Chr,
   x.Cmin
FROM
   first
   LEFT JOIN
     (
        SELECT
           ACType,
           SUM(Bhr) AS Bhr,
           SUM(Bmin)AS Bmin,
           SUM(Chr) AS Chr,
           SUM(Cmin) AS Cmin
        FROM
           (
              SELECT
                 ACType,
                 Bhr,
                 Bmin,
                 Chr,
                 Cmin
              FROM
                 first
              UNION ALL
              SELECT
                 ACType,
                 Bhr,
                 Bmin,
                 Chr,
                 Cmin
              FROM
                 SECOND
           )
        GROUP BY
           ACType
     )
     AS x
WHERE
   first.ACType = x.ACType

答案 1 :(得分:-2)

选择null作为A,* 来自second_table