How to find total count of distinct items along with item using Room? - android-room

Let's say I have a table kids_data
id
kid_name
father_name
1
Alisa
Bosconovitch
2
Ebby
Bosconovitch
3
John
Peter
4
simmy
Alladin
5
sara
Alladin
Now I want to fetch distinct names and their count too.
So if I perform
SELECT DISTINCT father_name from kids_data
What I will get is
father_name
Bosconovitch
Peter
Alladin
I want count of each kid too.
As Bosconovitch & Alladin have Two children then I want that number also.
Using Room Database in Android? How?
My class is
data class FatherData(
val kid_count: Int,
val father_name: String
)
In Dao
// what Query or how can i do this to achieve that, please.
#Query("SELECT DISTINCT father_name from kids_data")
fun getFatherData(): List<FatherData>

This does what I wanted!
This query will be used.
#Query("select father_name, count(father_name) as kid_count from kids_data group by father_name")
fun getFatherData(): List<FatherData>

Related

DAX / Power Pivot - Count occurrence substring within string of another table without relation

Dears, i'm stuck in a difficult yet simple task.
I need to count the number of occurence of speficic substrings within a string in a column that as no relation.
Let me give you an example with Emojis :
Table 1:
People
Message
John
You got it 😊
Paul
So Beautifull 😍😍😍
John
😊
Paul
Good luck 😚
Table 2 :
Subgroup
Emoticones
face-smiling
😁
face-smiling
😂
face-smiling
😊
face-smiling
😇
face-affection
😍
face-affection
😚
In a pivot, i would like to be able to do this kind of thing with Puivot :
Subgroup
Total emoji
face-smiling
2
face-affection
4
Person
face-smiling
face-affection
John
2
0
Paul
0
4
I would create a calculated table using GENERATE() to iterate over each Emoticone
Then use ADDCOLUMNS() to iterate over each Messages, and add the count of the current Emoticone in the current Message.
To count the number of occurrences of an icon in a Message, compute the difference of lengths between Message and Message with removed icons (an icon counts for 2 characters)
And finally FILTER() out pairs without match:
EmotiCounts =
VAR cj = GENERATE(Emoticones,
VAR icon = Emoticones[Emoticones]
RETURN
ADDCOLUMNS(
Messages,
"Counts",
(
LEN(Messages[Message])-
LEN(
SUBSTITUTE(
Messages[Message],
icon,
""))
)/2
)
)
RETURN
FILTER( cj, [Counts] > 0 )
That table contains both People and Subgroup needed for your visuals.

Laravel eloquent deep count with relationship

I want to get count from deep table and the relationship is A->B->C->D and I want total count of D.
For example;
A: 3 Boss
B: 5 Managers
C: 8 Human Resources
D: 25 Employees
Imagine that every boss has managers and every manager has human resources and also every human resource has employees. How can I get total count every boss's employees' with Laravel. (For instance, first boss has 7 employees at the end.) Should I have to write hard sql code like joins or can I handle with eloquent?
By the way, my tables:
A: id, name
B: id, name
A and B has pivot table.
C: id, name
B and C has pivot table
D: id, name
C and D has pivot table
So far, I tried to:
$a = Boss::with("a.b.c.d")->where("id", 10)->first();
dd($a->b->c->d->count());
It just gave me d's count but I want to all of a's.
There is no native relationship for this case.
I created a HasManyThrough relationship with unlimited levels: Repository on GitHub
After the installation, you can use it like this:
class A extends Model {
use \Staudenmeir\EloquentHasManyDeep\HasRelationships;
public function d() {
return $this->hasManyDeep(D::class, ['a_b', B::class, 'b_c', C::class, 'c_d']);
}
}
$count = A::find($id)->d()->count();

Find Maximum Columns in a grouped row. [using PIG]

I have to find maximum number of posts created by person with some given set of data, where I am provided with user id, display name, age, comments count, view count, date, score and title of each post.
To get the number of maximum post, I think, we can group by user id.Now, after grouping, I need to check the id which has the most no. of columns. I don't understand how would I solve the latter part. Please help.
As What, I understand from your question. I am giving you answer Accordingly.
Let be try this code :
a = load '<path>' using PigStorage(',') as(userId,displayName,age,commentsCount,viewCount,date,score,title)
b = group a by userId;
c = foreach b generate group,COUNT(a.title);
dump c;

Pig - counting members across a group

Say I have a relation Students, with fields grade and teacher. I want to group by both grade and teacher, but retain a count of all the students per grade in each group. Something like:
classes = GROUP Students BY (grade,teacher);
classes = FOREACH classes {
GENERATE
(### COUNT OF ALL STUDENTS IN GRADE ###) as grade_size,
Students as students,
teacher as teacher;
}
But I can't figure out how to do the filter from inside the group statement. Some kind of filter, but I don't know to scope the grade of the students outside vs. inside the group.
There are 2 ways of doing it:
1) Using Group By grade and teacher, than count, than Flatten and Group By grade.
classes = GROUP Students BY (grade,teacher);
teachers = FOREACH classes GENEARATE FLATTEN(group) as (grade,teacher), COUNT(Students) as perTeacehr;
grade = GROUP teachers BY grade;
result = FOREACH grade GENERATE FLATTEN(teachers), SUM(teachers.perTeacher) as perGrade;
describe result;
dump result;
2) Group By grade, than use UDF from BagGroup from DataFu library to do in memory group by, but this is vulnerable to possible heap memory exceptions, but is faster.

Efficient algorithm that takes a Twitter user and finds top users by order of how many of his followers they follow

The title is very wordy. So I'll explain with an example.
We have a database of 10,000 twitter users with each following up to 2000 users. The algorithm takes as input one random never before seen user (including the people that follow him), and returns the twitter users from the database by order of how many of his followers they follow.
i.e.
We have:
User A follows 1,2,3,4
User B follows 3,4,5,6
User C follows 4,8,9
We enter user X who has users 3,4,5 following him.
The algorithm should return:
B: 3 matches (3,4,5)
A: 2 matches (3,4)
C: 1 match (4)
Store the data as a sparse integer matrix A of size 10^5x10^5 with ones at the appropriate places. Then, given a user i, compute A[i,] * A (matrix multiplication). Then sort.
Assuming you have a table structure similar to this:
Table Users
Id (PK, uniqueidentifier, not null)
Username (nvarchar(50), not null)
Table UserFollowers
UserId (FK, uniqueidentifier, not null)
FollowerId (uniqueidentifier, not null)
You can use the following query to get the common parents of followers of the followers of the user in query
SELECT Users_Inner.Username, COUNT(Users_Inner.Id) AS [Total Common Parents]
FROM Users INNER JOIN
UserFollowers ON Users.Id = UserFollowers.FollowerId INNER JOIN
UserFollowers AS UserFollowers_Inner ON UserFollowers.FollowerId = UserFollowers_Inner.UserId INNER JOIN
Users AS Users_Inner ON UserFollowers_Inner.FollowerId = Users_Computed.Id
WHERE (UserFollowers.UserId = 'BD34A1FF-FCF5-4D35-B8A3-EFFB1587A874')
GROUP BY Users_Inner.Username
ORDER BY COUNT(Users_Inner.Id) DESC
would something like this work?
for f in followers(x)
for ff in followers(f)
count[ff]++ // assume it is initially 0
sort the ff-s by their counts
Unlike the matrix solution, the complexity of this is proportional to the number of people involved rather than the number of users on twitter.

Resources