Qt, C++, Sqlite: AVG and SUM Sqlite column of time as a string (HH:mm:ss)

52 Views Asked by At

I have a column table name FlightTime that has the time as a string with the format HH:mm:ss. I want to get the Avg and SUM value of that column and append the result to a QTextBrowser. I want the result to apprear as HH:mm:ss, or HH hrs,mm min, ss sec . whichever is the easiest For example I have:

  • row 1 : 00:20:05
  • row 2 : 00:35:15
  • row 3 : 00:15:25

The Total line should show : 01 hrs, 10 min, 45 sec or 01:10:45 (hrs could be > 24) The Total line should show : 00 hrs, 23 min, 35 sec or 00:23:35 (hrs could be > 24) I have tried all kind of method even ChatGPT without much success. Thanks in advance for the help

void LogRep::on_pushBtn_LR_Eval_clicked()
{
    QString flightno = ui->comBox_LR_FlightNo->currentText();
    QSqlQuery query;
    query.prepare("SELECT AVG(Preflight), AVG(Taxi), AVG(Takeoff), AVG(PlaneCtrl), AVG(Nav), AVG(RadioCom), AVG(Instrument),"
                  "AVG(Circuit), AVG(Emergency), AVG(Landing), AVG(PostFlight), AVG(Overall), AVG(strftime('%s', FlightTime)), SUM(strftime('%s', FlightTime)) FROM FlightRun WHERE FlightNo = :flightno");
    query.bindValue(":flightno", flightno);
    query.exec();
    if (query.next()) {
        double preflight_average = query.value(0).toDouble();
        double taxi_average = query.value(1).toDouble();
        double takeoff_average = query.value(2).toDouble();
        double planectrl_average = query.value(3).toDouble();
        double nav_average = query.value(4).toDouble();
        double radiocom_average = query.value(5).toDouble();
        double instrument_average = query.value(6).toDouble();
        double circuit_average = query.value(7).toDouble();
        double emergency_average = query.value(8).toDouble();
        double landing_average = query.value(9).toDouble();
        double postflight_average = query.value(10).toDouble();
        double overall_average = query.value(11).toDouble();
        double flighttime_average = query.value(12).toDouble();
        double flighttime_sum = query.value(13).toDouble();

        ui->textBr_LR_Eval->append(QString("The average scores for FlightNo %1 are:").arg(flightno));
        ui->textBr_LR_Eval->append(QString("Preflight: %1").arg(preflight_average));
        ui->textBr_LR_Eval->append(QString("Taxi: %1").arg(taxi_average));
        ui->textBr_LR_Eval->append(QString("Takeoff: %1").arg(takeoff_average));
        ui->textBr_LR_Eval->append(QString("PlaneCtrl: %1").arg(planectrl_average));
        ui->textBr_LR_Eval->append(QString("Nav: %1").arg(nav_average));
        ui->textBr_LR_Eval->append(QString("RadioCom: %1").arg(radiocom_average));
        ui->textBr_LR_Eval->append(QString("Instrument: %1").arg(instrument_average));
        ui->textBr_LR_Eval->append(QString("Circuit: %1").arg(circuit_average));
        ui->textBr_LR_Eval->append(QString("Emergency: %1").arg(emergency_average));
        ui->textBr_LR_Eval->append(QString("Landing: %1").arg(landing_average));
        ui->textBr_LR_Eval->append(QString("PostFlight: %1").arg(postflight_average));
        ui->textBr_LR_Eval->append(QString("Overall: %1").arg(overall_average));
        ui->textBr_LR_Eval->append(QString("Average FlightTime: %1    minutes").arg(flighttime_average));
        ui->textBr_LR_Eval->append(QString("Total FlightTime: %1 minutes").arg(flighttime_sum));

    } else {
        QMessageBox::critical(this, tr("Error"), query.lastError().text());
    }
}

Result:

The average scores for FlightNo FLT001 are:
Preflight: 26.6667
Taxi: 66.6667
Takeoff: 56.6667
PlaneCtrl: 68.3333
Nav: 53.3333
RadioCom: 33.3333
Instrument: 33.3333
Circuit: 43.3333
Emergency: 33.3333
Landing: 80
PostFlight: 33.3333
Overall: 40
Average FlightTime: 9.46686e+08 minutes
Total FlightTime: 2.84006e+09 minutes.

There seems to be a conversion problem from double and from the column string format to sec

0

There are 0 best solutions below