I'm trying to setup my NSFetchRequest to core data to retrieve the rows "unique name which have greater rate"
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"rate = max(rate)"];
[request setPropertiesToFetch:#[#"name"]];
request.predicate = predicate;
[request setReturnsDistinctResults:YES];
But the above return only one row which have maximum rate.
required sample
name | rate | factor |
_______|______|________|
John | 3.2 | 7 |
Betty | 5.5 | 7 |
Betty | 7.1 | 2 |
Betty | 3.1 | 2 |
Edward | 5.5 | 1 |
Edward | 4.5 | 2 |
John | 4.3 | 4 |
How would i set up the request to return an array like
John, 4.3, 4
Betty, 7.1, 2
Edward,5.5, 1
And can we sort it (sort by rate desc) with the fetch query itself ? So the result array will be
Betty, 7.1, 2
Edward,5.5, 1
John, 4.3, 4
Set the propertiesToGroupBy property of your NSFetchRequest to include "name".
Note: you may have to aggregate the factor column in some way as well, for the group by to work.
Related
I would like to find the median of a range of cells that correspond to a condition in another column.
For the example pictured below that would be finding the median of the numbers in Column2 that have the same numbers in Column1. This is shown in Column3. The number of rows is dynamic, hence needing to associate this to Column 1.
Here is an example table:
|---------------------|------------------|------------------|
| Column1 | Column2 | Column3 |
|---------------------|------------------|------------------|
| 1 | 0.1 | 0.25 |
|---------------------|------------------|------------------|
| 1 | 0.2 | 0.25 |
|---------------------|------------------|------------------|
| 1 | 0.3 | 0.25 |
|---------------------|------------------|------------------|
| 1 | 0.4 | 0.25 |
|---------------------|------------------|------------------|
| 2 | 1 | 1.5 |
|---------------------|------------------|------------------|
| 2 | 2 | 1.5 |
|---------------------|------------------|------------------|
| 3 | 1.1 | 1.1 |
|---------------------|------------------|------------------|
I've tried using INDEX, MATCH like this
=INDEX(MEDIAN(B:B), MATCH(A1,A:A,0))
but it's (obviously) incorrect. Any help appreciated!
google-spreadsheet
Use Filter to return the correct range to the Median function.
=median(filter($B$2:$B,$A$2:$A=A2))
To summarize each month's balance, I try to call month API in query statement. but sounds like the month API always return (month - 1) but not month.
Example data:
| Type | TransDate | Amount |
|---------+------------+---------|
| Sale | 08/13/2017 | -40.97 |
| Return | 08/10/2017 | 127.48 |
| Payment | 08/07/2017 | 2194.45 |
| Sale | 08/07/2017 | -100.97 |
| Sale | 08/06/2017 | -143.44 |
| Sale | 07/02/2017 | -143.44 |
| Sale | 07/03/2017 | -7.55 |
| Sale | 08/04/2017 | -8.03 |
I use below formula:
=query(A:C,"select MONTH(B),sum(C) where A = 'Sale' or A = 'Return' group by MONTH(B) label MONTH(B) 'Month',sum(C) 'Total'")
The result table:
| Month | Total |
|-------+---------|
| 6 | -150.99 |
| 7 | -165.93 |
The month should be 7 and 8, but actually it return 6 and 7.
As the documentation says:
Returns the zero-based month value from a date or datetime value. For example: month(date "2009-02-05") returns 1. Note: the months are 0-based, so the function returns 0 for January, 1 for February, etc.
As #puts said, the index is start from 0, fix that with a offset:
=query(A:E,"select MONTH(B)+1,sum(E) where A = 'Sale' or A = 'Return' group by MONTH(B) label MONTH(B)+1 'Month',sum(E) 'Total'")
I have a single table
DB_SMS
----------------------------------------
contactId | messageText | to | createdAt
I want fetch the most recently created record for each contactId. For example:
contactId | messageText | to | createdAt
1 | msg01 | a | 2015-04-20
1 | msg02 | b | 2015-04-21
2 | msg03 | c | 2015-04-20
3 | msg04 | d | 2015-04-20
3 | msg05 | w | 2015-04-22
Required result
contactId | messageText | to | createdAt
1 | msg02 | b | 2015-04-21
2 | msg03 | c | 2015-04-20
3 | msg05 | w | 2015-04-22
I am using following code
NSFetchRequest * theRequest = [NSFetchRequest fetchRequestWithEntityName:#"DB_SMS"];
[theRequest setResultType:NSDictionaryResultType];
[theRequest setPropertiesToFetch:#[#"contactId",#"messageText",#"to",#"createdAt"]];
[theRequest setPropertiesToGroupBy:#[#"contactId"]];
[theRequest setSortDescriptors:#[[NSSortDescriptor sortDescriptorWithKey:#"createdAt" ascending:NO]]];
NSArray * theArray = [theDbHandler.managedObjectContext executeFetchRequest:theRequest error:nil];
i'm getting following exception
SELECT clauses in queries with GROUP BY components can only contain properties named in the GROUP BY or aggregate functions
plz suggest some solution .
I have entity employee_detail in coredata
name | rate | factor |
_______|______|________|
John | 3.2 | 4 |
Betty | 5.5 | 7 |
Betty | 2.1 | 2 |
Betty | 3.1 | 2 |
Edward | 4.5 | 5 |
John | 2.3 | 4 |
i want unique object base on attribute name
O/P should be
name | rate | factor |
_______|______|________|
John | 3.2 | 4 |
Betty | 5.5 | 7 |
Edward | 4.5 | 5 |
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:#"employee_detail"];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"employee_detail" inManagedObjectContext:self.managedObjectContext];
fetchRequest.resultType = NSDictionaryResultType;
fetchRequest.propertiesToFetch = [NSArray arrayWithObject:[[entity propertiesByName] objectForKey:#"name"]];
fetchRequest.returnsDistinctResults = YES;
NSArray *dictionaries = [self.managedObjectContext executeFetchRequest:fetchRequest error:nil];
NSLog (#"names: %#",dictionaries);
I am working on a rather large query, but am now stuck on the last bit. Given this example table:
Key1 | Key2 | SomeCol |
0 | 0 | ABC |
0 | 1 | 123 |
------------------------------
1 | 5 | ABC |
1 | 6 | DEF |
1 | 7 | ABC |
------------------------------
2 | 4 | ABC |
2 | 5 | 456 |
2 | 6 | 456 |
------------------------------
3 | 4 | ABC |
3 | 5 | 456 |
3 | 6 | ABC |
------------------------------
4 | 4 | ABC |
4 | 5 | ABC |
4 | 6 | ABC |
At this point in my query, I have extracted sequential(Key1, Key2) portions of a table and grouped by Key1. I wish to determine if all the values of SomeCol are identical, except for the first row.
Expected results:
Key1 | Key2 | SomeCol |
0 | 0 | ABC |
2 | 4 | ABC |
4 | 4 | ABC |
I know I can use something like.Any(g => g.SomeCol.Distinct().Count() == 1) in a case where I need all entries to be the same, but I can't seem to figure out how to get the syntax right to Skip(1). Also, I feel that my method of checking equality is sort of a hack. I know I can do this processing easily in C#, but I want to get as much of the processing to happen on the database side. Since my query is currently written in extension methods, I would appreciate in answer in the same syntax. Thanks!
What I have so far:
resultFromRestOfQuery
.GroupBy(g => g.Key1)
????
.SelectMany(g => g.Take(1).Select(h => h)
UPDATE
Alright, tested this on your values and it works.
var result = collection
.OrderBy(p => p.Key1)
.ThenBy(p => p.Key2)
.GroupBy(p => p.Key1)
.Where(p => p.Skip(1)
.Select(j => j.SomeCol)
.Distinct().Count() == 1)
.Select(p => p.First())
.ToList();
UPDATE #2
Perhaps this will help you with performance. Here is another version of this query without using Distinct(). Note the p.Count() > 1 - this is to avoid selecting the 1st row in a group when there's only 1 element in a group. If it's okay to select the first row when there's only one row, simply remove this part of the condition.
var result = collection
.OrderBy(p => p.Key1)
.ThenBy(p => p.Key2)
.GroupBy(p => p.Key1)
.Where(p => p.Count() > 1 && p.Skip(1)
.Select(j => j.SomeCol)
.All(j => j == p.Last().SomeCol))
.Select(p => p.First())
.ToList();