Finding a unique value from 2 columns and then a ratio of those values

flip66

I have a table with the columns id, player1, and player2. The ID's for a player can be in either of the two columns. I need to find all the unique ID's in column player1 and column player2 combined. Then I need to also find the amount of times each player appears in each column. Finally I want to order it by the ratio of column player1 / column player2.

For instance, I have the following values in the table.

1 103 101

2 103 111

3 232 103

4 223 111

My query would return..

Player 223: 1/0

Player 232: 1/0

Player 103: 2/1

Player 101: 0/1

Player 111: 0/2

I know for the unique IDs I can do something like this

select 
(SELECT group_concat(DISTINCT player1) FROM table) as player1,
(SELECT group_concat(DISTINCT player2) FROM table) as player2

and I know for the order I can do something like this

ORDER BY player1 / player2 DESC

I'm really just having a hard time figuring out what to do once I get the unique ID and then try to get the ratios outputed and sorted

A. Zalonis

Try this

select player1,player2, CONCAT('Player',IF( (select count(DT.player1) from table as DT where MT.player1 = DT.player1) > 0, player1,player2),':', (select count(DT.player1) from table as DT where MT.player1 = DT.player1), ' / ', (select count(DT.player2) from table as DT where MT.player2 = DT.player2) )  
from table MT
where player1 != player2
group by palyer1,player2
order by (player1/IF(player2 is not null and player2 != 0,player2,1) desc

I hope it helps you

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Finding unique combinations of columns from a dataframe

Finding location of value from multiple columns

Finding counts of unique values in column for each unique value in other column

In R: ordering values from 2 DF columns for use in ratio for each row

Finding columns with more than one unique value

VBA Unique values from 2 or more columns

Pandas, Pivot table from 2 columns with values being a count of one of those columns

Finding the index or unique values from a dataframe column

Binding the values from response and make the checkboxes with those value using angular2

R -Finding the columns with only one non-missing value and filling its missing values with that unique non-missing value

How can I do something similar to insert ignore on a combination of 2 values without having those columns as primary key or unique?

Trouble finding unique values

Finding all unique values that add up to specific target value

creating columns from values and placing other columns as those values

Finding unique values in Array

Finding the values from same table that have value 1 and 2 but not 3

Finding unique columns

Sql-find duplicate values of two columns and display all values of "column 1" and unique value of "column 2"

Get the non unique values from 2 columns using formula

Finding unique set of values across columns [SQL]

Finding unique data from two columns

Obtaining top 2 max values from a list and finding its corresponding value from 2nd list

Finding unique values in table

Unique case of finding duplicate values flexibly across columns in R

R: Get unique values based on criteria from 2 other columns

Destacking a table into variable columns (e.g. item1, item2, item3) such that the unique values in a column occupy those columns

Finding unique number of logins from a database based on two columns

Generate two columns from three different tables and calculate the ratio of those columns using a grouping variable

Updating values of a column from multiple columns if the values are present in those columns