0
我想有SQL函數的核心數據等效的:重複計數
SELECT species, sex, COUNT(*) FROM Bird GROUP BY species, sex;
請求指通常會以下輸入返回此
+---------+------+----------+
| species | sex | COUNT(*) |
+---------+------+----------+
| Bus | m | 2 |
| Car | f | 1 |
+---------+------+----------+
:
INSERT INTO Bird VALUES ('BlueBird','Car','f');
INSERT INTO Bird VALUES ('RedBird','Bus','m');
INSERT INTO Bird VALUES ('RedBird','Bus','m');
我已經完成了不同的請求,但我在計算(*)時遇到了問題。 以下是我有:
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Bird" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
[request setReturnsDistinctResults:YES];
[request setResultType:NSDictionaryResultType];
NSDictionary *entityProperties = [entity propertiesByName];
NSMutableArray *properties = [NSMutableArray arrayWithObject:[entityProperties objectForKey:@"species"]];
[properties addObject:[entityProperties objectForKey:@"sex"]];
[request setPropertiesToFetch: properties];
什麼我要補充?
非常感謝
謝謝你的回答本篤。其實我已經考慮過這樣的算法,但最終它在大型數據集上並不是很快。也許我應該試着微調它,但我認爲在一個請求中獲得所有內容會更有效率。 – lorenzo 2011-02-04 13:19:45