Search code examples
iphonecore-datansfetchedresultscontrollernsfetchrequest

how to sqlite query to core data


how can i translate the following sqlite query to core data

select name, firstname, class, telephone, entryDateTime, counselor, count() 
  from myTable 
  group by strftime('yy-mm-dd', entryDateTime), name, class, counselor 
  order by entryDateTime desc;

what did i try?

i want to group by date(yy-mm-dd), class, counselor, and name. entryDateTime is in UTC but it should be converted to local time.

thanks in advance


Solution

  • The most flexible method for "group by"-like queries is the NSFetchedResultsController. Please see my answer of a couple of days ago, which should help you.

    To solve the problem of subgrouping based on more than one attribute, you just use the sectionNameKeyPath as a subgroup and do the other grouping from there after fetching.

    Alternatively, consider putting the attributes you need into a different entity and group by that entity.