Fetch unique object based on attribute name from coredata - ios

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

Related

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...

Rail Query includes + filter by column

I start in Ruby and Ruby on rails. I am trying to modify the access to this object (below) in base via the form and I will like to filter by the values that are in the table node_tags.
def map
......
nodes = Node.bbox(bbox).where(:visible => true).includes(:node_tags).limit(MAX_NUMBER_OF_NODES + 1)
.....
end
Probably on SQL request
SELECT * FROM nodes n INNER JOIN node_tags nt on n.node_id = nt.node_id where nt.k = 'alert' and nt.v = 'true'
Table nodes
| node_id | latitude | longitude | changeset_id | visible | timestamp | tile | version | redaction_id |
|---------|-----------|-----------|--------------|---------|-----------|------|---------|--------------|
| 11 | 473705641 | 3955487 | 11 | TRUE |
| 12 | 473705641 | 3955487 | 12 | TRUE |
table node_tags
| node_id | k | v |
|---------|-------|------|
| 11 | name | bob |
| 12 | alert | true |
If I understand right, you need to get sql as in your example by ORM (ActiveRecord).
Try this:
nodes = Node.where(:visible => true).join(:node_tags).where("node_tags.k = 'alert' and node_tags.v = 'true'")

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.

core data Group By fetch all propetries issues

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 .

How to join 2 columns from one mysql database with 4 columns in a different database

I have 2 mysql databases. (1) authorID, authorFirst, authorLast. (2) courseID, courseName, courseAuthor1, courseAuthor2, courseAuthor3, courseAuthor4. How to join 2 columns from authors database into one OR ALL 4 of the courseAuthor columns in the courses database?
authors
authorID | authorFirst | authorLast
1 | firstA | lastA
2 | firstB | lastB
3 | firstC | lastC
4 | firstD | lastC
courses
courseID | courseName | courseAuthor1 | courseAuthor2 | courseAuthor3 | courseAuthor4
1 | course1 | 2 | 3 | NULL | NULL
2 | course 2 | 1 | 4 | 3 | NULL
3 | course 33 | 4 | 1 | 2 | NULL
4 | course 4 | 3 | NULL | NULL | NULL
I Want Results to look like this:
courseID | courseName | courseAuthor1 | courseAuthor2 | courseAuthor3 | courseAuthor4
1 | course1 | firstB LastB | firstC LastC | NULL | NULL
2 | course 2 | firstA LastA | firstD LastD | firstC LastC | NULL
3 | course 33 | firstD LastD | firstA LastA | firstB LastB | NULL
4 | course 4 | firstC LastC | NULL | NULL | NULL
I don't even know where to start. Here is my code:
SELECT * FROM courses JOIN authors ON authors.authorID IN
(courses.courseAuthor1, courses.courseAuthor2, courses.courseAuthor3,
courses.courseAuthor4)
Which shows:
results | courseID | courseName | courseAuthor1 | courseAuthor2 | courseAuthor3 | courseAuthor4 | authorID | authorFirst | authorLast
1 | 1 | course1 | 2 | 3 | NULL | NULL | 2 | firstB | lastB
2 | 1 | course1 | 2 | 3 | NULL | NULL | 3 | firstC | lastC
3 | 2 | course 2 | 1 | 4 | 3 | NULL | 1 | firstA | lastA
4 | 2 | course 2 | 1 | 4 | 3 | NULL | 4 | firstD | lastD
5 | 2 | course 2 | 1 | 4 | 3 | NULL | 3 | firstC | lastC
6 | 3 | course 33 | 4 | 1 | 2 | NULL | 4 | firstD | lastD
7 | 3 | course 33 | 4 | 1 | 2 | NULL | 1 | firstA | lastA
8 | 3 | course 33 | 4 | 1 | 2 | NULL | 2 | firstB | lastB
9 | 4 | course 4 | 3 | NULL | NULL | NULL | 3 | firstC | lastC
Thanks So Much!
It took me a while, but i've answered my question. Here it is:
SELECT c1.courseID, c1.courseName,
CONCAT(a1.authorFirst,' ',a1.authorLast) AS aOne,
CONCAT(a2.authorFirst,' ',a2.authorLast) AS aTwo,
CONCAT(a3.authorFirst,' ',a3.authorLast) AS aThree,
CONCAT(a4.authorFirst,' ',a4.authorLast) AS aFour
FROM courses c1
LEFT JOIN authors a1 ON c1.courseAuthor1 = a1.authorID
LEFT JOIN authors a2 ON c1.courseAuthor2 = a2.authorID
LEFT JOIN authors a3 ON c1.courseAuthor3 = a3.authorID
LEFT JOIN authors a4 ON c1.courseAuthor4 = a4.authorID

Resources