Cursor c = sqlExec("SELECT time DISTINCT(time) FROM mainTable ");while(c.hasNext()){sqlExec("SELECT * FROM mainTable WHERE device = c.device" GROUP BY time)}
query = "SELECT timeTbl.Time AS [TIME], ";for (int i = 0; i < devices.size(); ++i) //использовать лучше foreach{ query += QString("device%1.Value AS [%2], ").arg(i).arg(devices.at(i))}
query += "FROM mainTable as timeTbl "
for (int i = 0; i < devices.size(); ++i){ QString subQuery ("SELECT Time, Value From mainTable WHERE Device = %1").arg(devices.at(i)) query += QString("LEFT JOIN (%1) AS device%2 ON (timeTbl.Time= device%2.Time) ").arg(subQuery).arg(i);}
query += GROUP BY timeTbl.Time
SELECT DISTINCT mainTable.Time, barometer.Value as BAROMETER, thermometer.Value as THERMOMETERFROM (mainTable LEFT JOIN (SELECT mainTable.Time, mainTable.Value FROM mainTable WHERE mainTable.Device="barometer") AS barometer ON barometer.Time = mainTable.Time)LEFT JOIN (SELECT mainTable.Time, mainTable.Value FROM mainTable WHERE mainTable.Device="thermometer") AS thermometer ON thermometer.Time = mainTable.Time