I am using DBAccess framework v1.6.12 with Xcode 7.0.1.
I would like to select a SUM and COUNT of two properties, much like the query below, except the ORM does not have a raw SQL interface:
SELECT
SUM(bed_in_datetime - bed_out_datetime) AS sum_bed_in
,COUNT(bed_in_datetime) AS cnt_bed_in
FROM
sleep_tbl;
Is there a way I can achieve it using DBAccess
.
There is no concept of SELECT as such with the ORM, because you're dealing with entire class objects and not SQL, however the DBQuery object does have methods on it for SUM
, COUNT
, GROUP BY
& IDs
that return subsets of data from a query.
This would have to be done in two stages, but shouldn't have a huge affect on the time take to perform the query.
But the first element will be performed like this:
double sum = [[sleep_tbl query] sumOf:@"(bed_in_datetime - bed_out_datetime)"];
And the count like this:
int count = [[sleep_tbl query] count];
Thanks