Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Core Data and Grouping / Sum

Core Data and Grouping / Sum
Thread Tools
Fresh-Faced Recruit
Join Date: Aug 2001
Location: Germany
Status: Offline
Reply With Quote
Jul 7, 2005, 02:41 AM
 
Hi,

I've started with Core Data and binding and it's quite easy to use.

I started with filtering my entities and everything works find.

But now I would like to get @sums and group some data. How could I do that:

e.g.:

my Entity has 3 Attributes: name, periode and Amount.

I would like to have a NSTableview which shows name, @sum(Amount).

Do I need to create a custon class or is there another way?

Thanks Mark
     
Fresh-Faced Recruit
Join Date: Aug 2001
Location: Germany
Status: Offline
Reply With Quote
Jul 7, 2005, 08:41 AM
 
would like to precise my problem:
Thinking in SQL-Statements I would like to get the following SQL query:

SELECT name, SUM(amount) FROM Entity GROUP BY name

and display the result in a NSTableview.

How do I get this result?

Thanks Mark
     
Senior User
Join Date: Mar 2000
Location: Ithaca, NY
Status: Offline
Reply With Quote
Jul 9, 2005, 11:48 AM
 
I don't believe there's a simple (i.e. codeless) way to do what you're describing. Key-value coding does provide an @sum operator, which lets you do things like:

sumOfAmounts = [myArray valueForKeyPath:@"@sum.amount"];

This will get you the sum of all the amounts, but there's no way for KVC to do the grouping you want.

The way I'd approach it would probably be to make another entity that represents a group of your original entities, let's call it EntityGroup. This entity would have an attribute for "name", and a to-many relationship to your original entity, let's call it "subentities". The creation of these objects would have to be done in code, although searching for entities with a given name could be helped along by using an NSFetchRequest. Your table would then display these EntityGroup objects, with the columns bound to "name" and "subentities.@sum.amount".

Another hint: if you want to get a list of the unique names in a collection of objects, try [myArrayOrSetOfObjects valueForKeyPath:@"@distinctUnionOfObjects.name"]
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 09:20 AM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2