2011-02-01 56 views
6

我知道,我可以使用@distinctUnionOfObjects找到类似SQL中的以下内容:GROUP BY等效核心数据

SELECT a_value 
FROM my_table 
GROUP BY a_value; 

我正在寻找的是阵列中的全部返回的数据的,而不仅仅是通过表达式匹配组的值的数组。从本质上讲,我找下面的SQL查询的等效核心数据:

SELECT * 
FROM my_table 
GROUP BY a_value; 
+0

请让我们知道,如果一个答案帮助你。 – 2011-05-16 10:44:45

回答

15

这是模拟

SELECT 'Status', COUNT(*) FROM 'Records' GROUP BY 'Status'

NSFetchRequest* fetch = [NSFetchRequest fetchRequestWithEntityName:@"Record"]; 
NSEntityDescription* entity = [NSEntityDescription entityForName:@"Record" 
              inManagedObjectContext:myManagedObjectContext]; 
NSAttributeDescription* statusDesc = [entity.attributesByName objectForKey:@"status"]; 
NSExpression *keyPathExpression = [NSExpression expressionForKeyPath: @"url"]; // Does not really matter 
NSExpression *countExpression = [NSExpression expressionForFunction: @"count:" 
                  arguments: [NSArray arrayWithObject:keyPathExpression]]; 
NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init]; 
[expressionDescription setName: @"count"]; 
[expressionDescription setExpression: countExpression]; 
[expressionDescription setExpressionResultType: NSInteger32AttributeType]; 
[fetch setPropertiesToFetch:[NSArray arrayWithObjects:statusDesc, expressionDescription, nil]]; 
[fetch setPropertiesToGroupBy:[NSArray arrayWithObject:statusDesc]]; 
[fetch setResultType:NSDictionaryResultType]; 
NSError* error = nil; 
NSArray *results = [myManagedObjectContext executeFetchRequest:fetch 
                 error:&error]; 

Found here

-1

您可以使用Predicate Programming

编辑:对不起,你不能使用谓词分组至少不直截了当。我刚刚阅读了参考文献。

限制:您无法将“任意”SQL查询转换为谓词或提取请求。没有办法,例如,转换一个SQL语句,如

SELECT t1.name,V1,V2

FROM table1 t1 JOIN (SELECT t2.name AS V1, count(*) AS V2 

    FROM table2 t2 GROUP BY t2.name as V) on t1.name = V.V1 
+0

你能更具体吗?我知道存在谓词,但我不知道他们将如何解决我的问题。他们将如何提供类似于SQL或distinct()在SQL中? – smtlaissezfaire 2011-02-01 18:02:00

+0

是的,我也注意到了这个说法。不幸的是,这并不清楚*这些限制是什么。在文档中给出的查询要复杂得多(它涉及一个连接) - 我正在做一个直读表。 – smtlaissezfaire 2011-02-01 22:31:21

3

你可以尝试使用NSFetchedResultsController目的在于提供的方式分组初始值设定项中的sectionNameKeyPath构造。请注意,FRC主要用于与表格视图耦合,但并非真正必要。这样你可以用你的sectionNameKeyPath对你的结果进行分组,这可能是你模型中的一个瞬态属性。

作为一个评论,我不会推荐从数据库角度考虑核心数据,事实并非如此。核心数据的构建使您可以更轻松地坚持和管理对象关系。仅仅因为在iOS上它运行在SQLite之上并不能使它成为数据库的替代品。

参考:NSFRC Reference

0

我发现这个方法(大致类似于接受的答案)是一个小更清洁和更容易了解。这是SQL等效于:

SELECT COUNT(*), a_value FROM my_table GROUP BY a_value;

NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:[MyTable className]]; 

// create expression for grouped column 
NSExpressionDescription *aValueDescription = [[NSExpressionDescription alloc] init]; 
aValueDescription.name = @"aValue"; // name of key in result dictionary 
aValueDescription.expression = [NSExpression expressionWithFormat:@"aValue"]; 
aValueDescription.expressionResultType = NSObjectIDAttributeType; 

// create expression for count 
NSExpressionDescription *countDescription = [[NSExpressionDescription alloc] init]; 
countDescription.name = @"count"; // name of dictionary key in result dictionary 
countDescription.expression = [NSExpression expressionWithFormat:@"[email protected]"]; 
countDescription.expressionResultType = NSInteger32AttributeType; 

// fill out fetch request 
fetchRequest.propertiesToGroupBy = @[@"aValue"]; 
fetchRequest.propertiesToFetch = @[aValueDescription, countDescription]; 
//fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"count" ascending:NO]]; // not sure why this crashes 
fetchRequest.resultType = NSDictionaryResultType; // required for "group by" requests 

NSError *error = nil; 
NSArray *results = [managedObjectContext executeFetchRequest:fetchRequest error:&error]; 

返回results是的NSDictionary的阵列。请注意,描述name属性可以是任何你想要的 - 它们只是返回字典中键的名称。可以将一个谓词添加到获取请求中以过滤表中的行;此代码返回所有行。

奖励积分的人谁可以告诉我怎么做那种工作的描述符...