从GROUP BY查询中检索表项的总和

时间:2013-05-20 11:06:22

标签: android mysql sql sqlite

我有三张桌子

private static final String DTReasonTable = "create table tb_DTReasons(DTReasonId integer primary key autoincrement,DTReason text)";

private static final String ConfigurationTable = "create table tb_Config(ConfigId integer primary key autoincrement," +
        "RawMaterialName text,OutPut_pdt text,TotalWorkHrs real,Quantity real,ProcessStartTime real,OutputQuantity real,procesEndTime real)";

private static final String DownTimeTable = "create table tb_DownTimeTable(DT_Id integer primary key autoincrement,ConfigId integer," +
        "StartTime real,EndTime real,DTReasonId integer)";

我想创建一个类似下面的报告

enter image description here

有人可以帮我编写相应报告生成的程序吗?

  public Cursor getDtAnalysisReport()
{
    return this.data.rawQuery("select EndTime-StartTime as duration,tb_DTReasons.DTReason,count(tb_DownTimeTable.DTReasonId) from tb_DownTimeTable" +
            " inner join tb_DTReasons on tb_DownTimeTable.DTReasonId = tb_DTReasons.DTReasonId group by tb_DownTimeTable.DTReasonId ", null);

}

这是我尝试的但是我收到了一个错误。请帮我找一个解决方案。

1 个答案:

答案 0 :(得分:0)

SELECT DTReason AS 'Down Time Reason',
       SUM(EndTime - StartTime) AS 'Duration',
       SUM(EndTime - StartTime) /
       (SELECT SUM(EndTime - StartTime)
        FROM tb_DownTimeTable
       ) * 100 AS '% Total Down Time',
       COUNT(*) AS 'No of Occurrences'
FROM tb_DTReasons
JOIN tb_DownTimeTable ON tb_DTReasons.DTReasonId = tb_DownTimeTable.DTReasonId
GROUP BY tb_DTReasons.DTReasonId