Select and sum multiple columns for statistic purposes with Laravel query - laravel

I have one table scores where I have saving users scores. It's looks like this
table `scores`
id | points | user_id
1 5 1
2 2 1
3 4 1
4 1 3
5 10 2
I want to select each user, sum his points and show as a ranking. The result from above should be
user_id | points
1 11
2 10
3 1
The query with which I came up is
$sumPoints = Scores::select( \DB::raw("sum(points) as numberOfPoints"), \DB::raw("count(id) as numberId"))->groupBy("user_id")->first();
The problem is in ->first() because it's return only one result.. it is working as must. If I try to use ->get() instead I've got Undefined property error. How should I use this?
The query which is working in phpmyadmin
SELECT count(id) as numberId, sum(points) as numberOfPoints FROM `points` GROUP BY `user_id`

You can use something like this
$sumPoints = Scores::select( \DB::raw("sum(points) as numberOfPoints"), \DB::raw("count(id) as numberId"))->groupBy("user_id")->get();
foreach($sumPoints as $point){
dd($point); //OR dd($point->numberOfPoints)
}

Related

Laravel sum/count nested relation

i have a problem with simple relations.
Tables looks like that:
table players
id user_id game_id
1 2 1
2 5 1
3 3 1
4 4 2
5 2 2
table games
id result (win or lose)
1 1
2 0
What i need in result is:
Players Wins Losses
John 3 2
Philip 2 2
Jack 1 3
I tried alot of queries but i cant get proper result.
This one
`"select * from `players` inner join `games` on `players`.`game_id` = `games`.`id`"`
This one is best i can do, but its raw and no idea how to rewrite it to DB:: or Eloquent. And its not grouping anyway.
you can directly count from relation with model.
$category = Category::find($id);
$category->children->count();

How to limit the record of children's attribute in laravel eloquent

I am creating a comment section using laravel with the example entries below:
id | comment | parent_id
1 test 1
2 test 1
3 test 1
4 test 2
5 te 2
6 test 1
7 test 2
I get these data from an API request. Let's say, I want to get all comments and limit the replies up to 2 records per query. From the given test sample above, the result should be:
id | parent_id
1 1
2 1
4 2
5 2
My question is, how to get all replies (parent_id > 0) and limit & offset the query per request?
My current query is:
Comment::where('parent_id', '>' , 0)->get();
I think you should use pagination to limit the number of the elements:
Comment::where('parent_id', '>' , 0)->paginate(15);
Replace the "15" with your limit.
Also, you can check Laravel docs about pagination: https://laravel.com/docs/7.x/pagination

Distinct on two columns with same data type

In my game application I have a combats table:
id player_one_id player_two_id
---- --------------- ---------------
1 1 2
2 1 3
3 3 4
4 4 1
Now I need to know hoy many unique users played the game. How can I apply distinct, count on both columns player_one_id and player_two_id?
Many thanks.
By using union you can get unique distinct value.
$playerone = DB::table("combats")
->select("combats.player_one_id");
$playertwo = DB::table("combats")
->select("combats.player_two_id")
->union($playerone)
->count();

Filter out duplicate results in where clause in sql server

select * from user where username is equal to 'jw'
Add DISTINCT directly after SELECT, or alternatively, use the GROUP BY clause after your WHERE clause.
ex: GROUP BY works.memb_id
I think I understand what you are trying to do. WorkID, EmployeeID, and MembID all have to be 1:1 and then you want to group on all three fields.
You have asserted that each Memb_id is assigned to a unique employee. If WorkID is not then you will need to use an aggregate function to select one of the records returned after the group. Such as Min(), Max() or a window function like Row_Number and or Rank.
For example
WorkID, EmployeeID, MembID
1 1 1
2 1 1
3 2 2
4 1 1
If you use Min(WorkID), EmployeeID, MembID....Group By EmployeeID, MembID you will get:
WorkID, EmployeeID, MembID
1 1 1
3 2 2
If this is not what you are looking for some sample data as a whole and what results you expect would be necessary.

How to select two max value from different records that has same ID for every records in table

i have problem with this case, i have log table that has many same ID with diferent condition. i want to select two max condition from this. i've tried but it just show one record only, not every record in table.
Here's my records table:
order_id seq status____________________
1256 2 4
1256 1 2
1257 0 2
1257 3 1
Here my code:
WITH t AS(
SELECT x.order_id
,MAX(y.seq) AS seq2
,MAX(y.extern_order_status) AS status
FROM t_order_demand x
JOIN t_order_log y
ON x.order_id = y.order_id
where x.order_id like '%12%'
GROUP BY x.order_id)
SELECT *
FROM t
WHERE (t.seq2 || t.status) IN (SELECT MAX(tt.seq2 || tt.status) FROM t tt);
this query works, but sometime it gave wrong value or just show some records, not every records.
i want the result is like this:
order_id seq2 status____________________
1256 2 4
1257 3 2
I think you just want an aggregation:
select d.order_id, max(l.seq2) as seq2, max(l.status) as status
from t_order_demand d join
t_order_log l
on d.order_id = l.order_id
where d.order_id like '%12%'
group by d.order_id;
I'm not sure what your final where clause is supposed to do, but it appears to do unnecessary filtering, compared to what you want.

Resources