core data Group By fetch all propetries issues - ios

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 .

Related

Cypher query aggregate sum of values

I have a Cypher query that shows the following output:
+----------------
| usid | count |
+----------------
| "000" | 1 |
| "000" | 0 |
| "000" | 0 |
| "001" | 1 |
| "001" | 1 |
| "001" | 0 |
| "002" | 2 |
| "002" | 2 |
| "002" | 0 |
| "003" | 4 |
| "003" | 2 |
| "003" | 2 |
| "004" | 4 |
| "004" | 4 |
| "004" | 4 |
+----------------
How can I get the below result with the condition SUM(count) <= 9.
+----------------
| usid | count |
+----------------
| "000" | 1 |
| "001" | 2 |
| "002" | 4 |
| "003" | 8 |
+----------------
Note: I have used the below query to get the 1st table data.
MATCH (us:USER)
WITH us
WHERE us.count <= 4
RETURN us.id as usid, us.count as count;
I don't know how you get your original data, so I will just use a WITH clause and assume the data is there:
// original data
WITH usid, count
// aggregate and filter
WITH usid, sum(count) as new_count
WHERE new_count <= 9
RETURN usid, new_count
Based on the updated question, the new query would look like:
MATCH (us:USER)
WHERE us.count <= 4
WITH us.id as usid, sum(us.count) as count
WHERE new_count <= 9
RETURN usid, count
˙˙˙

DISTINCT, MAX and list value from join table

How to combine distinct and max within join table below?
Table_details_usage
UID | VE_NO | START_MILEAGE | END_MILEAGE
------------------------------------------------
1 | ASD | 410000 | 410500
2 | JWQ | 212000 | 212350
3 | WYS | 521000 | 521150
4 | JWQ | 212360 | 212400
5 | ASD | 410520 | 410600
Table_service_schedule
SID | VE_NO | SV_ONMILEAGE | SV_NEXTMILEAGE
------------------------------------------------
1 | ASD | 400010 | 410010
2 | JWQ | 212120 | 222120
3 | WYS | 511950 | 521950
4 | JWQ | 212300 | 222300
5 | ASD | 410510 | 420510
How to get display as below (only max value)?
Get Max value from Table_service_schedule (SV_NEXTMILEAGE) and Get Max value from Table_details_usage (END_MILEAGE)
SID | VE_NO | SV_NEXTMILEAGE | END_MILEAGE
--------------------------------------------
5 | ASD | 420510 | 410600
4 | JWQ | 222300 | 212400
3 | WYS | 521950 | 521150
Something in the lines of:
SELECT
SID,
VE_NO,
SV_NEXTMILEAGE,
(select max(END_MILEAGE) from Table_details_usage d where d.VE_NO = s.VE_NO) END_MILEAGE
FROM Table_service_schedule s
WHERE SID = (SELECT max(SID) FROM Table_service_schedule s2 WHERE s2.VE_NO = s.VE_NO)
Probably could need to change the direct value ov SV_NEXTMILEAGE to max as well if the id:s aren't in order...

Joining two tables, A and B, where the column names of A should be joined with B based on the values of a column in B

The title is a little confusing, so here is an example of the problem I'm facing.
Table:
FORM_QUESTION
Fields:
student_id
q1_ans
q2_ans
q3_ans
q4_ans
q5_ans
x--------------x----------x----------x----------x----------x----------x
| student_id | q1_ans | q2_ans | q3_ans | q4_ans | q5_ans |
x--------------x----------x----------x----------x----------x----------x
| 1 | A | D | B | B | E |
| 2 | D | C | B | A | D |
| 3 | B | C | D | A | B |
x--------------x----------x----------x----------x----------x----------x
The FORM_QUESTION table stores a student's answers for each question.
Here is information on the second table:
Table:
FORM_VALID_ANS
Fields:
question_id
valid_answer
x---------------x----------------x
| question_id | valid_answer |
x---------------x----------------x
| q1_ans | A |
| q1_ans | B |
| q2_ans | A |
| q2_ans | B |
| q2_ans | C |
| q2_ans | D |
| q3_ans | A |
| q4_ans | A |
| q4_ans | B |
| q5_ans | A |
x---------------x----------------x
The second table, FORM_VALID_ANS, stores the valid, acceptable answers for a particular question. So, according to the above table, here is the acceptable list of values for each question:
q1_ans: A, B
q2_ans: A, B, C, D
q3_ans: A
q4_ans: A, B
q5_ans: A
As you can see, the values for the FORM_VALID_ANS.valid_answer include only the question field names for FORM_QUESTION ("q1_ans", "q2_ans", "q3_ans", "q4_ans", and "q5_ans"). I need to check each students' answer to make sure it is a valid value that they can enter, which is specified in the FORM_VALID_ANS.valid_answer field.
The desired output, where XXX represents an invalid value, and all other values would be answered value:
x--------------x----------x----------x----------x----------x----------x
| student_id | q1_ans | q2_ans | q3_ans | q4_ans | q5_ans |
x--------------x----------x----------x----------x----------x----------x
| 1 | A | D | XXX | B | XXX |
| 2 | XXX | C | XXX | A | XXX |
| 3 | B | C | XXX | A | XXX |
x--------------x----------x----------x----------x----------x----------x
Is it possible to join these two tables together and produce these results (or similar results)?

coredata - fetch distinct rows having maximum value

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.

Fetch unique object based on attribute name from coredata

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);

Resources