Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6148185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:14:12+00:00 2026-05-23T19:14:12+00:00

I have to create a NSFetchRequest for my iPhone App that returns the same

  • 0

I have to create a NSFetchRequest for my iPhone App that returns the same results as the following SQL statement:

SELECT week
      , year
      , SUM(duration) AS totalDuration
FROM myTable
GROUP BY year
      , week

I’ve tried to solve that with the following code:

NSExpression * durationExpression = [NSExpression expressionForFunction:@"sum:" arguments:[NSArray arrayWithObject:[NSExpression expressionForKeyPath:@"duration"]]];
NSExpressionDescription * durationExpressionDescription = [[[NSExpressionDescription alloc] init] autorelease];
[durationExpressionDescription setExpression:durationExpression];
[durationExpressionDescription setExpressionResultType:NSDoubleAttributeType];
[durationExpressionDescription setName:@"totalDuration"];

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"MyEntity" inManagedObjectContext:managedObjectContext];
NSArray *propertiesToFetch = [[NSArray alloc] initWithObjects:@"year", @"week", durationExpressionDescription, nil];

[fetchRequest setEntity:entity];
[fetchRequest setReturnsDistinctResults:YES];
[fetchRequest setResultType:NSDictionaryResultType];
[fetchRequest setPropertiesToFetch:propertiesToFetch];

But that will group all results to one single row. Any hints how to solve it?

Edit: The source table has the following data:

year | month | duration
2011 |     7 | 10
2011 |     7 | 15
2011 |     6 | 15
2011 |     5 | 10

The SQL statement returns the correct result I’d like to achieve:

year | month | duration
2011 |     7 | 25
2011 |     6 | 15
2011 |     5 | 10

The NSFetchRequest returns:

year | month | duration
2011 |     7 | 50
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T19:14:13+00:00Added an answer on May 23, 2026 at 7:14 pm

    Pretty sure your single return is triggered by the NSExpression which can only return a single value. Otherwise, you would get an array of dictionaries like this:

    year | month | duration
    2011 |     7 | 50
    2011 |     6 | 50
    2011 |     5 | 50
    

    … which wouldn’t actually reflect the state of the data.

    The expression is configured to return the sum of all the duration values in every instance of MyEntity given to it. Since you have no fetch predicate, it will be given every existing instance of MyEntity available to the context. Nevertheless, it can only return a single value and that means a single dictionary when fetching by property.

    The best solution for this is to create a separate fetch for just the expression since it really has nothing to do with the other values being fetched i.e. the summed value isn’t associated with any particular year or month value. In any case, simple fetches run faster so separating the expression out will give you a overall speed boost even through you have two fetches instead of one.

    I would caution you to avoid trying to use Core Data as you would SQL. It is a cardinal error to think of Core Data as a SQL wrapper. Core Data is not SQL. Entities are not tables. Objects are not rows. Attributes are not columns. Relationships are not joins. Core Data is an object graph management system that may or may not persist the object graph and may or may not use SQL far behind the scenes to do so. There is surprisingly little that translates directly from SQL to Core Data.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have create my own NSOpenGLView class, right now the data that i want
I have to create a site definition for a client that must contain pre-defined
I have setup Core Data for an iPhone app without an instance of NSFetchedResultsController.
I have create testing application that has 3 classes Car Radio SportCar : Car
I have an app and there is some basic functionality that needs to be
I have create an application that makes use of the ImagemMagick library. I have
In my app I have a function that presents a modal view controller when
I have some inefficiency in my app that I'd like to understand and fix.
I have create my code in order to write images in a remote sql
I have a fairly simple iPhone app which utilizes CoreData for object persistence. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.