Create a Matrix-View in MySQL [duplicate] - mysql

This question already has answers here:
How can I return pivot table output in MySQL?
(10 answers)
MySQL - Rows to Columns
(13 answers)
Closed last month.
if have 2 tables:
tool_id
name
status
1
Mike
1
1
Claus
3
1
Max
3
2
Mike
2
2
Claus
2
3
Mike
1
3
Claus
3
3
Max
1
3
Sam
1
4
Mike
2
and
tool_id
tool_name
1
51363
2
52514
3
51458
4
51608
I need a view that shows me these 2 tables as a matrix. The tools should be displayed in the table headers and the names and their status for the corresponding tool as a table.
If a name is not assigned to a tool, it should be given a zero.
name
51363
52514
51458
51608
Mike
1
2
1
2
Claus
3
2
3
0
Max
3
0
3
0
Sam
0
0
1
0
I have more than 400 tools and workers each in the two tables.
Is something like this possible?
Edit: This is not an duplicate of the linked posts.
I need a dynamic creation of columns.

Related

Count the duplicate values on multiple array and sort it to highest

I have a target where I have this DUPLICATE checker with COUNT for multiple arrays. As we can see. Any suggestions on how I make this work? Thank you and have a nice day.
DATA:
id
value
1
[5,6,8,4,2]
2
[2,3,4,1,8]
3
[9,3,2,1,10]
Normal result:
number
count
1
2
2
3
3
2
4
2
5
1
6
1
7
0
8
2
9
1
10
1
This is my target result with sorting (Highest count):
number
count
2
3
1
2
3
2
4
2
8
2
5
1
6
1
10
1
9
1

Unique combination with no repeated individual values

I have ranked rows based on certain criteria. Lets call the rank columns as
time_rank and id_rank.
Window_id time_rank id_rank
1 1 1
1 2 1 --> 1 is already considered, reject this row
2 1 1
2 1 2 --> 1 is already considered, reject this row
2 2 2
3 1 1
3 2 1 --> 1 is already considered, reject this row
3 1 2
3 3 2
4 1 1
4 2 1 --> 1 is already considered, reject this row
4 2 2
4 3 1 --> 1 is already considered, reject this row
Tried few tricks with lag , another ranking and self join. None seem to work. I need to find the unique combination with no repetition:
Desired Output :
Window_id time_rank id_rank
1 1 1
2 1 1
2 2 2
3 1 1
3 3 2
4 1 1
4 2 2

MySQL join two Tables to n:m Relation

I have two tables (Table 1 and Table 2) and I want to create a table (Table 3) with the ID's. So to speak a n to m relationship table.
Table 1
1 Mark
2 George
3 David
Table 2
5 Bank
6 Construction
7 Carfactory
Table 3
1 5
1 6
1 7
2 5
2 6
2 7
3 5
3 6
3 7
I can make it so that the rows from table 1 are taken from 1 to 3 but not as to the row 1 all rows are taken from the table 2. Can anyone help me ?
You can achieve your expected output using CROSS JOIN.
SELECT table1.id,table2.id FROM table1 CROSS JOIN table2

MySQL query - need assistance creating this query

I need some direction on how to create this MySQL query.
I have a table that looks like this.
id group name value
1 1 user mike
2 1 setting 1
3 2 user joe
4 2 setting 2
5 3 user jill
6 3 setting 1
7 4 user mark
8 4 setting 1
9 4 other 22
I would like to format the query to group users that have identical settings (IE mike and jill would be grouped in this example, not mark because of "other")
At the end of the day I am trying to consolidate and make the table look like this. If I can figure out the query to properly group them ,I will use PHP to combine the values and save it back to the DB.
id group name value
1 1 user mike OR jill
2 1 setting 1
3 2 user joe
4 2 setting 2
7 4 user mark
8 4 setting 1
9 4 other 22
Thank you!

I need sql query for getting the Required format

i am having table Category with the columns like
id(AutoIncrement),Parent_id,Level,Name
initially for level 1 datas has Parent_id is 0. autoincrement id will be Parent_id for next levels .my table table table datas will bw like this
id Parent_id Level Name
1 0 1 Indian
2 0 1 International
3 0 1 Nri
4 1 2 BC
5 2 2 Christian
6 2 2 Muslim
7 4 3 MBC-1
8 7 4 OBC-2
9 1 2 FC
i want to show records in this format (its like a tree view)
id Parent_id Level Name
1 0 1 Indian
4 1 2 BC
7 4 3 MBC-1
8 7 4 OBC-2
9 1 2 FC
5 2 2 Christian
6 2 2 Muslim
2 0 1 International
3 0 1 Nri
4 1 2 BC
Can any one should help me to get this arrangement of datas using sql Query?
If it does not have a set number of branches you likely want to loop over a query in your app or write an SP to obtain all nodes. Some good reading here:
http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/