Distinct on two columns with same data type - laravel

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

Related

How to create a lot of categories for card laravel voyager

image
image2
Now I have like this
image3
but I want have more categories
like this
How to select many category?
Let’s say in your case, we need to define the category for every features. So feature can have many categories and inverse category can have many features. Thus, it will be a Many To Many relationships.
To accomplish this we need to create 3 tables features, categories, and intermediate table feature_category. The feature_category table will have the feature_id and category_id column which connects both feature and category table and this intermediate table called the pivot table.
Here are the table structures:
features
id - integer
name - string
categories
id - integer
name - string
feature_category
feature_id - integer
category_id - integer
=> category
id name
-- -------
1 Category 1
2 Category 2
3 Category 3
4 Category 4
=> features
id name
-- -------
1 Feature 1
2 Feature 2
3 Feature 3
4 Feature 4
=> feature_category
id feature_id category_id
-- ------- -------
1 1 1
2 2 2
3 3 2
4 3 3
4 3 4
===============================
Our feature_category table before sync() operation:
id feature_id category_id
-- ------- -------
1 1 1
2 2 2
3 2 3
4 2 4
5 3 2
6 4 3
Laravel Sync() example:
<?php
use App\Models\Feature;
$user = Feature::find(2);
// Want to keep only "Category 2" (Id 2) category
$user->category()->sync([2]);
After performing the above operation, our feature_category table will look like below:
id feature_id category_id
-- ------- -------
1 1 1
2 2 2
5 3 2
6 4 3
Checkboxes or dropdowns can be used from the frontend to select multiple categories for features and sync() method can be used to update feature cards accordingly.
First you need to create new table feature_category with two fields :
(same type of features.id) feature_id
(same type of categories.id) category_id
Second create belongsToMany relationship directly in Voyager.
Example :

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

Complex Networks in Hive - Optimization Code

I have a problem with how to get my Hive code optimized.
I have a huge table as follows:
Customer_id Product_id Date Value
1 1 02/28 100.0
1 2 02/02 120.0
1 3 02/10 144.0
2 2 02/15 120.0
2 3 02/28 144.0
... ... ... ...
I want to create a complex network where I link the products through the buyers. The graph does not have to be directed and I have to count the number of links between them.
In the end I need this:
Product_x Product_y amount
1 2 1
1 3 1
2 3 2
Can anyone help me with this?
I need an optimized way to do this. The join of the table with itself is not the solution. I really need an optimum way on this =/
CREATE TABLE X AS
SELECT
a.product_id as product_x,
b.product_id as product_y,
count(*) as amout
FROM table as a
JOIN table as b
ON a.customer_id = b.customer_id
WHERE a.product_id < b.product_id
GROUP BY product_x, product_y;

Select and sum multiple columns for statistic purposes with Laravel query

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

SAS Sorting within group

I would like to try and sort this data by descending number of events and from latest date, grouped by ID
I have tried proc sql;
proc sql;
create table new as
select *
from old
group by ID
order by events desc, date desc;
quit;
The result I currently get is
ID Date Events
1 09/10/2015 3
1 27/06/2014 3
1 03/01/2014 3
2 09/11/2015 2
3 01/01/2015 2
2 16/10/2014 2
3 08/12/2013 2
4 08/10/2015 1
5 09/11/2014 1
6 02/02/2013 1
Although the dates and events are sorted descending. Those IDs with multiple events are no longer grouped.
Would it be possible to achieve the below in fewer steps?
ID Date Events
1 09/10/2015 3
1 27/06/2014 3
1 03/01/2014 3
3 01/01/2015 2
3 08/12/2013 2
2 09/11/2015 2
2 16/10/2014 2
4 08/10/2015 1
5 09/11/2014 1
6 02/02/2013 1
Thanks
It looks to me like you're trying to sort by descending event, then by either the earliest or latest date (I can't tell which one from your explanation), also descending, and then by id. In your proc sql query, you could try calculating the min or max of the Date variable, grouped by event and id, and then sort the result by descending event, the descending min/max of the date, and id.

Resources