交易与交易实体之间具有多对多关系,交易实体具有金额和日期.我的目标是基于Person有一个NSFetchRequest,但我只想知道在某些日期之间有交易的人和交易金额的总和.
我的代码看起来像:
NSExpression *amountKeyPath = [NSExpression ExpressionForKeyPath:@"transactions.amount"];NSExpression *sumAmountExpression = [NSExpression ExpressionForFunction:@"sum:" arguments:@[amountKeyPath]];// Create the Expression description for that Expression.NSExpressionDescription *description = [[NSExpressionDescription alloc] init];[description set@R_404_6889@:@"sum"];[description setExpression:sumAmountExpression];[description setExpressionResultType:NSDecimalAttributeType];// Create the sum amount fetch request,self.sumAmountFetchRequest = [NSFetchRequest fetchRequestWithEntity@R_404_6889@:@"Person"];self.sumAmountFetchRequest.resultType = NSDictionaryResultType;self.sumAmountFetchRequest.predicate = [nspredicate predicateWithFormat:@"SUBquery(transactions,$t,$t.date >= %@ AND $t.date <= %@).@count > 0",self.startDate,self.endDate];self.sumAmountFetchRequest.propertIEsToFetch = @[@"@R_404_6889@",description];
一切似乎都很好但是……当我查看生成的SQL查询时,它看起来像:
SELECT t0.ZPERSONID,t0.Z@R_404_6889@,(SELECT TOTAL(t2.ZAMOUNT) FROM ZTRANSACTION t2 WHERE (t0.Z_PK = t2.ZPERSON) ) FROM ZPERSON t0 WHERE (SELECT COUNT(t1.Z_PK) FROM ZTRANSACTION t1 WHERE (t0.Z_PK = t1.ZPERSON AND (( t1.ZDATE >= ? AND t1.ZDATE <= ?))) ) > ?
因此,似乎NSExpression声明,不尊重创建的fetchRequest的nspredicate.
是否可以使表达式也遵循附加到fetchRequest的谓词?或者既然NSExpression独立存在,我应该附加另一个谓词吗?
解决方法 我没有任何东西支持我自己的答案,但经过几个小时的努力.我认为不是NSExpression不尊重fetchRequest的nspredicate,而是更多的是fetchRequest的谓词是子查询.SELECT t0.ZPERSONID,(SELECT TOTAL(t2.ZAMOUNT) FROM ZTRANSACTION t2 WHERE (t0.Z_PK = t2.ZPERSON) ) FROM ZPERSON t0 WHERE (SELECT COUNT(t1.Z_PK) FROM ZTRANSACTION t1 WHERE (t0.Z_PK = t1.ZPERSON AND (( t1.ZDATE >= ? AND t1.ZDATE <= ?))) ) > ?
因此,谓词中的子查询并没有真正转换为我想要的WHERE子句.我认为如果它正在创建一个JOIN,那就行了.
我的解决方案最终是以相反的方式工作.如果我从Transaction创建fetchRequest,那就完美了.
NSExpression *amountKeyPath = [NSExpression ExpressionForKeyPath:@"amount"];NSExpression *sumAmountExpression = [NSExpression ExpressionForFunction:@"sum:" arguments:@[amountKeyPath]];// Create the Expression description for that Expression.NSExpressionDescription *description = [[NSExpressionDescription alloc] init];[description set@R_404_6889@:@"sum"];[description setExpression:sumAmountExpression];[description setExpressionResultType:NSDecimalAttributeType];// Create the sum amount fetch request,self.sumAmountFetchRequest = [NSFetchRequest fetchRequestWithEntity@R_404_6889@:@"Transaction"];self.sumAmountFetchRequest.resultType = NSDictionaryResultType;self.sumAmountFetchRequest.predicate = [nspredicate predicateWithFormat:@"date >= %@ AND date <= %@",self.endDate];self.sumAmountFetchRequest.propertIEsToFetch = @[@"person.@R_404_6889@",description];self.sumAmountFetchRequest.propertIEsToGroupBy = @[@"person.@R_404_6889@"];
这非常有效.它必须按“person.@R_404_6889@”分组,以便它将使用总和:如所需.
生成的sql将是
SELECT t1.ZPERSONID,total( t0.ZAMOUNT) FROM ZTRANSACTION t0 left OUTER JOIN ZPERSON t1 ON t0.ZPERSON = t1.Z_PK WHERE ( t0.ZDATE >= ? AND t0.ZDATE <= ?) GROUP BY t1.ZPERSONID
干杯,
总结以上是内存溢出为你收集整理的ios – NSExpression尊重子查询NSPredicate全部内容,希望文章能够帮你解决ios – NSExpression尊重子查询NSPredicate所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)