0

请看我的表:

在此处输入图像描述

我需要计算总'code1'和总'code2'以及组合code1和code2的总和以及每天的总elapsedTime(timefiledate)。请看我得到的这个输出:

在此处输入图像描述

这是我的查询:

 SELECT 
        `Date`,
        `Code1`,
        `Code2`,
        `Total`
    FROM   
    (
        SELECT 
            timefiledate AS `Date`, 
            (SELECT COUNT(*) FROM tableName WHERE transactionCode= 'code1' AND timefiledate = `date`)AS code1,
            (SELECT COUNT(*) FROM tableName WHERE transactionCode= 'code2' AND timefiledate = `date`)AS code2, 
            (SELECT COUNT(*) FROM tableName WHERE transactionCode IN ('code1', 'code2') AND timefiledate = `date`)AS Total
            #Total elapsedTime goes here
        FROM bartran

    ) AS A

    WHERE `date` BETWEEN '2013-05-01' AND '2013-05-31'
    GROUP BY `date`;

请注意,elapsedTime 类型是“TIME”。我无法获得整个月的总经过时间。请有人给我一个建议。非常感谢!

4

2 回答 2

1

要以秒为单位返回总数,请使用 time_to_sec 函数 - 如下所示:

SELECT sum(time_to_sec(elapsedTime))
FROM tableName 
WHERE transactionCode IN ('code1', 'code2') AND timefiledate = `date`
于 2013-05-15T08:57:19.120 回答
0

可以将 TIME 转换为秒:

SELECT 
        `Date`,
        `Code1`,
        `Code2`,
        `Total`,
        `TotalElapsedTime`
    FROM   
    (
        SELECT 
            timefiledate AS `Date`, 
            (SELECT COUNT(*) FROM tableName WHERE transactionCode= 'code1' AND timefiledate = `date`)AS code1,
            (SELECT COUNT(*) FROM tableName WHERE transactionCode= 'code2' AND timefiledate = `date`)AS code2, 
            (SELECT COUNT(*) FROM tableName WHERE transactionCode IN ('code1', 'code2') AND timefiledate = `date`)AS Total
            (SELECT SUM(TIME_TO_SEC(elapsedTime)) FROM tableName WHERE transactionCode IN ('code1', 'code2') AND timefiledate = `date`) AS TotalElapsedTime
        FROM bartran

    ) AS A

    WHERE `date` BETWEEN '2013-05-01' AND '2013-05-31'
    GROUP BY `date`;
于 2013-05-15T08:38:19.867 回答