How to join three row of result from a join one to many to multiple columns
up vote
2
down vote
favorite
I have to get the three first results of a one to many join.
For example
JOIN `company`.`Reason`
ON ((`company`.`Reason`.`quality_id` = `company`.`Quality`.`id`)))
where Quality has a one to many relation to Reason.
And I need to show in a column REASON_1 the first result from Reason, in REASON_2 the second and REASON_3 the third.
At the moment, I show only one row of result this way :
`company`.`Reason`.`status` AS `Reason`
And I don't know how this works, how does it select one row, and which one ?
And how can I do the same, but only with 3 rows and with different column name ?
Quality
ID | a | b
1 | a1 | b1
2 | a2 | b2
Reason
ID | quality_id | status
1 | quality_id_1 | status_1
2 | quality_id_1 | status_2
2 | quality_id_2 | status_1
2 | quality_id_2 | status_3
So if I want to get Reasons of Quality of id 1 I would get
REASON_1 | REASON_2 | REASON_3
status_1 | status_2 | null
mysql sql sql-view
add a comment |
up vote
2
down vote
favorite
I have to get the three first results of a one to many join.
For example
JOIN `company`.`Reason`
ON ((`company`.`Reason`.`quality_id` = `company`.`Quality`.`id`)))
where Quality has a one to many relation to Reason.
And I need to show in a column REASON_1 the first result from Reason, in REASON_2 the second and REASON_3 the third.
At the moment, I show only one row of result this way :
`company`.`Reason`.`status` AS `Reason`
And I don't know how this works, how does it select one row, and which one ?
And how can I do the same, but only with 3 rows and with different column name ?
Quality
ID | a | b
1 | a1 | b1
2 | a2 | b2
Reason
ID | quality_id | status
1 | quality_id_1 | status_1
2 | quality_id_1 | status_2
2 | quality_id_2 | status_1
2 | quality_id_2 | status_3
So if I want to get Reasons of Quality of id 1 I would get
REASON_1 | REASON_2 | REASON_3
status_1 | status_2 | null
mysql sql sql-view
1
It might be better to include some more information about the structure of the data you are querying so we can help better.Have you tried doing a row number on the columns ordered by reason and returning the first three results?
– auxoutin
Nov 7 at 9:15
What decides which row ends up in Reason_1, Reason_2, Reason_3 - do you have some kind of ordering column?
– Bridge
Nov 7 at 9:16
@Bridge Nothing, I only need to put the column "status" of the table Reason in Reason_1, 2 and 3.
– Dimitri Danilov
Nov 7 at 9:19
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have to get the three first results of a one to many join.
For example
JOIN `company`.`Reason`
ON ((`company`.`Reason`.`quality_id` = `company`.`Quality`.`id`)))
where Quality has a one to many relation to Reason.
And I need to show in a column REASON_1 the first result from Reason, in REASON_2 the second and REASON_3 the third.
At the moment, I show only one row of result this way :
`company`.`Reason`.`status` AS `Reason`
And I don't know how this works, how does it select one row, and which one ?
And how can I do the same, but only with 3 rows and with different column name ?
Quality
ID | a | b
1 | a1 | b1
2 | a2 | b2
Reason
ID | quality_id | status
1 | quality_id_1 | status_1
2 | quality_id_1 | status_2
2 | quality_id_2 | status_1
2 | quality_id_2 | status_3
So if I want to get Reasons of Quality of id 1 I would get
REASON_1 | REASON_2 | REASON_3
status_1 | status_2 | null
mysql sql sql-view
I have to get the three first results of a one to many join.
For example
JOIN `company`.`Reason`
ON ((`company`.`Reason`.`quality_id` = `company`.`Quality`.`id`)))
where Quality has a one to many relation to Reason.
And I need to show in a column REASON_1 the first result from Reason, in REASON_2 the second and REASON_3 the third.
At the moment, I show only one row of result this way :
`company`.`Reason`.`status` AS `Reason`
And I don't know how this works, how does it select one row, and which one ?
And how can I do the same, but only with 3 rows and with different column name ?
Quality
ID | a | b
1 | a1 | b1
2 | a2 | b2
Reason
ID | quality_id | status
1 | quality_id_1 | status_1
2 | quality_id_1 | status_2
2 | quality_id_2 | status_1
2 | quality_id_2 | status_3
So if I want to get Reasons of Quality of id 1 I would get
REASON_1 | REASON_2 | REASON_3
status_1 | status_2 | null
mysql sql sql-view
mysql sql sql-view
edited Nov 7 at 9:45
asked Nov 7 at 9:12
Dimitri Danilov
6931926
6931926
1
It might be better to include some more information about the structure of the data you are querying so we can help better.Have you tried doing a row number on the columns ordered by reason and returning the first three results?
– auxoutin
Nov 7 at 9:15
What decides which row ends up in Reason_1, Reason_2, Reason_3 - do you have some kind of ordering column?
– Bridge
Nov 7 at 9:16
@Bridge Nothing, I only need to put the column "status" of the table Reason in Reason_1, 2 and 3.
– Dimitri Danilov
Nov 7 at 9:19
add a comment |
1
It might be better to include some more information about the structure of the data you are querying so we can help better.Have you tried doing a row number on the columns ordered by reason and returning the first three results?
– auxoutin
Nov 7 at 9:15
What decides which row ends up in Reason_1, Reason_2, Reason_3 - do you have some kind of ordering column?
– Bridge
Nov 7 at 9:16
@Bridge Nothing, I only need to put the column "status" of the table Reason in Reason_1, 2 and 3.
– Dimitri Danilov
Nov 7 at 9:19
1
1
It might be better to include some more information about the structure of the data you are querying so we can help better.Have you tried doing a row number on the columns ordered by reason and returning the first three results?
– auxoutin
Nov 7 at 9:15
It might be better to include some more information about the structure of the data you are querying so we can help better.Have you tried doing a row number on the columns ordered by reason and returning the first three results?
– auxoutin
Nov 7 at 9:15
What decides which row ends up in Reason_1, Reason_2, Reason_3 - do you have some kind of ordering column?
– Bridge
Nov 7 at 9:16
What decides which row ends up in Reason_1, Reason_2, Reason_3 - do you have some kind of ordering column?
– Bridge
Nov 7 at 9:16
@Bridge Nothing, I only need to put the column "status" of the table Reason in Reason_1, 2 and 3.
– Dimitri Danilov
Nov 7 at 9:19
@Bridge Nothing, I only need to put the column "status" of the table Reason in Reason_1, 2 and 3.
– Dimitri Danilov
Nov 7 at 9:19
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
You need to create an ordering within the reason table partitioned by the id from quality and then pivot this out on the columns from quality that you wish to return. In my code below i have done this using variables and a subquery in order to avoid having to making modifications to the base tables.
CREATE TABLE Quality (
QualityID INT AUTO_INCREMENT,
Value VARCHAR(50),
PRIMARY KEY(QualityID)
);
CREATE TABLE Reason (
ReasonID INT AUTO_INCREMENT,
QualityID INT,
Status varchar(50),
PRIMARY KEY(ReasonID)
);
INSERT INTO Quality (Value) VALUES ('A1'),('A2');
INSERT INTO Reason (QualityID, Status) VALUES (1,'Test1'),(1,'Test2'),(2,'Test3'),(2,'Test4'),(2,'Test5');
SET @RC = 0;
SET @QC = 0;
SELECT
Q.Value,
MAX(IF(R.OrderNo = 1, R.Status, NULL)) AS Status1,
MAX(IF(R.OrderNo = 2, R.Status, NULL)) AS Status2,
MAX(IF(R.OrderNo = 3, R.Status, NULL)) AS Status3
FROM Quality Q
INNER JOIN (SELECT
@RC:=CASE
WHEN @QC = QualityID THEN @RC + 1
ELSE 1
END AS OrderNo,
@QC:=QualityID as QualityID,
Status
FROM Reason R)R ON R.QualityID = Q.QualityID
GROUP BY Q.Value;
Thanks for your complete answer, but I don't use SQL Server, I use mysql, and I don't think there is "pivot" here :/
– Dimitri Danilov
Nov 7 at 9:59
Your solution will need to be a lot less elegant then can you store an ordering for the Status in your reason table?
– auxoutin
Nov 7 at 10:32
Unfortunatly I can't, I don't have the permission de modify them.
– Dimitri Danilov
Nov 7 at 10:45
I have modified the code so it should now work in mysql but the principles are the same. Hopefully this will work for you.
– auxoutin
Nov 7 at 10:59
Thank you so much to try helping me ! I tried to adapt it to my database stucture but I got the error that I cannot use variables within a sql view. Sorry for ommiting it in my post, I just putted a tag.
– Dimitri Danilov
Nov 7 at 11:19
|
show 1 more comment
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
You need to create an ordering within the reason table partitioned by the id from quality and then pivot this out on the columns from quality that you wish to return. In my code below i have done this using variables and a subquery in order to avoid having to making modifications to the base tables.
CREATE TABLE Quality (
QualityID INT AUTO_INCREMENT,
Value VARCHAR(50),
PRIMARY KEY(QualityID)
);
CREATE TABLE Reason (
ReasonID INT AUTO_INCREMENT,
QualityID INT,
Status varchar(50),
PRIMARY KEY(ReasonID)
);
INSERT INTO Quality (Value) VALUES ('A1'),('A2');
INSERT INTO Reason (QualityID, Status) VALUES (1,'Test1'),(1,'Test2'),(2,'Test3'),(2,'Test4'),(2,'Test5');
SET @RC = 0;
SET @QC = 0;
SELECT
Q.Value,
MAX(IF(R.OrderNo = 1, R.Status, NULL)) AS Status1,
MAX(IF(R.OrderNo = 2, R.Status, NULL)) AS Status2,
MAX(IF(R.OrderNo = 3, R.Status, NULL)) AS Status3
FROM Quality Q
INNER JOIN (SELECT
@RC:=CASE
WHEN @QC = QualityID THEN @RC + 1
ELSE 1
END AS OrderNo,
@QC:=QualityID as QualityID,
Status
FROM Reason R)R ON R.QualityID = Q.QualityID
GROUP BY Q.Value;
Thanks for your complete answer, but I don't use SQL Server, I use mysql, and I don't think there is "pivot" here :/
– Dimitri Danilov
Nov 7 at 9:59
Your solution will need to be a lot less elegant then can you store an ordering for the Status in your reason table?
– auxoutin
Nov 7 at 10:32
Unfortunatly I can't, I don't have the permission de modify them.
– Dimitri Danilov
Nov 7 at 10:45
I have modified the code so it should now work in mysql but the principles are the same. Hopefully this will work for you.
– auxoutin
Nov 7 at 10:59
Thank you so much to try helping me ! I tried to adapt it to my database stucture but I got the error that I cannot use variables within a sql view. Sorry for ommiting it in my post, I just putted a tag.
– Dimitri Danilov
Nov 7 at 11:19
|
show 1 more comment
up vote
2
down vote
accepted
You need to create an ordering within the reason table partitioned by the id from quality and then pivot this out on the columns from quality that you wish to return. In my code below i have done this using variables and a subquery in order to avoid having to making modifications to the base tables.
CREATE TABLE Quality (
QualityID INT AUTO_INCREMENT,
Value VARCHAR(50),
PRIMARY KEY(QualityID)
);
CREATE TABLE Reason (
ReasonID INT AUTO_INCREMENT,
QualityID INT,
Status varchar(50),
PRIMARY KEY(ReasonID)
);
INSERT INTO Quality (Value) VALUES ('A1'),('A2');
INSERT INTO Reason (QualityID, Status) VALUES (1,'Test1'),(1,'Test2'),(2,'Test3'),(2,'Test4'),(2,'Test5');
SET @RC = 0;
SET @QC = 0;
SELECT
Q.Value,
MAX(IF(R.OrderNo = 1, R.Status, NULL)) AS Status1,
MAX(IF(R.OrderNo = 2, R.Status, NULL)) AS Status2,
MAX(IF(R.OrderNo = 3, R.Status, NULL)) AS Status3
FROM Quality Q
INNER JOIN (SELECT
@RC:=CASE
WHEN @QC = QualityID THEN @RC + 1
ELSE 1
END AS OrderNo,
@QC:=QualityID as QualityID,
Status
FROM Reason R)R ON R.QualityID = Q.QualityID
GROUP BY Q.Value;
Thanks for your complete answer, but I don't use SQL Server, I use mysql, and I don't think there is "pivot" here :/
– Dimitri Danilov
Nov 7 at 9:59
Your solution will need to be a lot less elegant then can you store an ordering for the Status in your reason table?
– auxoutin
Nov 7 at 10:32
Unfortunatly I can't, I don't have the permission de modify them.
– Dimitri Danilov
Nov 7 at 10:45
I have modified the code so it should now work in mysql but the principles are the same. Hopefully this will work for you.
– auxoutin
Nov 7 at 10:59
Thank you so much to try helping me ! I tried to adapt it to my database stucture but I got the error that I cannot use variables within a sql view. Sorry for ommiting it in my post, I just putted a tag.
– Dimitri Danilov
Nov 7 at 11:19
|
show 1 more comment
up vote
2
down vote
accepted
up vote
2
down vote
accepted
You need to create an ordering within the reason table partitioned by the id from quality and then pivot this out on the columns from quality that you wish to return. In my code below i have done this using variables and a subquery in order to avoid having to making modifications to the base tables.
CREATE TABLE Quality (
QualityID INT AUTO_INCREMENT,
Value VARCHAR(50),
PRIMARY KEY(QualityID)
);
CREATE TABLE Reason (
ReasonID INT AUTO_INCREMENT,
QualityID INT,
Status varchar(50),
PRIMARY KEY(ReasonID)
);
INSERT INTO Quality (Value) VALUES ('A1'),('A2');
INSERT INTO Reason (QualityID, Status) VALUES (1,'Test1'),(1,'Test2'),(2,'Test3'),(2,'Test4'),(2,'Test5');
SET @RC = 0;
SET @QC = 0;
SELECT
Q.Value,
MAX(IF(R.OrderNo = 1, R.Status, NULL)) AS Status1,
MAX(IF(R.OrderNo = 2, R.Status, NULL)) AS Status2,
MAX(IF(R.OrderNo = 3, R.Status, NULL)) AS Status3
FROM Quality Q
INNER JOIN (SELECT
@RC:=CASE
WHEN @QC = QualityID THEN @RC + 1
ELSE 1
END AS OrderNo,
@QC:=QualityID as QualityID,
Status
FROM Reason R)R ON R.QualityID = Q.QualityID
GROUP BY Q.Value;
You need to create an ordering within the reason table partitioned by the id from quality and then pivot this out on the columns from quality that you wish to return. In my code below i have done this using variables and a subquery in order to avoid having to making modifications to the base tables.
CREATE TABLE Quality (
QualityID INT AUTO_INCREMENT,
Value VARCHAR(50),
PRIMARY KEY(QualityID)
);
CREATE TABLE Reason (
ReasonID INT AUTO_INCREMENT,
QualityID INT,
Status varchar(50),
PRIMARY KEY(ReasonID)
);
INSERT INTO Quality (Value) VALUES ('A1'),('A2');
INSERT INTO Reason (QualityID, Status) VALUES (1,'Test1'),(1,'Test2'),(2,'Test3'),(2,'Test4'),(2,'Test5');
SET @RC = 0;
SET @QC = 0;
SELECT
Q.Value,
MAX(IF(R.OrderNo = 1, R.Status, NULL)) AS Status1,
MAX(IF(R.OrderNo = 2, R.Status, NULL)) AS Status2,
MAX(IF(R.OrderNo = 3, R.Status, NULL)) AS Status3
FROM Quality Q
INNER JOIN (SELECT
@RC:=CASE
WHEN @QC = QualityID THEN @RC + 1
ELSE 1
END AS OrderNo,
@QC:=QualityID as QualityID,
Status
FROM Reason R)R ON R.QualityID = Q.QualityID
GROUP BY Q.Value;
edited Nov 7 at 10:58
answered Nov 7 at 9:50
auxoutin
915
915
Thanks for your complete answer, but I don't use SQL Server, I use mysql, and I don't think there is "pivot" here :/
– Dimitri Danilov
Nov 7 at 9:59
Your solution will need to be a lot less elegant then can you store an ordering for the Status in your reason table?
– auxoutin
Nov 7 at 10:32
Unfortunatly I can't, I don't have the permission de modify them.
– Dimitri Danilov
Nov 7 at 10:45
I have modified the code so it should now work in mysql but the principles are the same. Hopefully this will work for you.
– auxoutin
Nov 7 at 10:59
Thank you so much to try helping me ! I tried to adapt it to my database stucture but I got the error that I cannot use variables within a sql view. Sorry for ommiting it in my post, I just putted a tag.
– Dimitri Danilov
Nov 7 at 11:19
|
show 1 more comment
Thanks for your complete answer, but I don't use SQL Server, I use mysql, and I don't think there is "pivot" here :/
– Dimitri Danilov
Nov 7 at 9:59
Your solution will need to be a lot less elegant then can you store an ordering for the Status in your reason table?
– auxoutin
Nov 7 at 10:32
Unfortunatly I can't, I don't have the permission de modify them.
– Dimitri Danilov
Nov 7 at 10:45
I have modified the code so it should now work in mysql but the principles are the same. Hopefully this will work for you.
– auxoutin
Nov 7 at 10:59
Thank you so much to try helping me ! I tried to adapt it to my database stucture but I got the error that I cannot use variables within a sql view. Sorry for ommiting it in my post, I just putted a tag.
– Dimitri Danilov
Nov 7 at 11:19
Thanks for your complete answer, but I don't use SQL Server, I use mysql, and I don't think there is "pivot" here :/
– Dimitri Danilov
Nov 7 at 9:59
Thanks for your complete answer, but I don't use SQL Server, I use mysql, and I don't think there is "pivot" here :/
– Dimitri Danilov
Nov 7 at 9:59
Your solution will need to be a lot less elegant then can you store an ordering for the Status in your reason table?
– auxoutin
Nov 7 at 10:32
Your solution will need to be a lot less elegant then can you store an ordering for the Status in your reason table?
– auxoutin
Nov 7 at 10:32
Unfortunatly I can't, I don't have the permission de modify them.
– Dimitri Danilov
Nov 7 at 10:45
Unfortunatly I can't, I don't have the permission de modify them.
– Dimitri Danilov
Nov 7 at 10:45
I have modified the code so it should now work in mysql but the principles are the same. Hopefully this will work for you.
– auxoutin
Nov 7 at 10:59
I have modified the code so it should now work in mysql but the principles are the same. Hopefully this will work for you.
– auxoutin
Nov 7 at 10:59
Thank you so much to try helping me ! I tried to adapt it to my database stucture but I got the error that I cannot use variables within a sql view. Sorry for ommiting it in my post, I just putted a tag.
– Dimitri Danilov
Nov 7 at 11:19
Thank you so much to try helping me ! I tried to adapt it to my database stucture but I got the error that I cannot use variables within a sql view. Sorry for ommiting it in my post, I just putted a tag.
– Dimitri Danilov
Nov 7 at 11:19
|
show 1 more comment
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53186411%2fhow-to-join-three-row-of-result-from-a-join-one-to-many-to-multiple-columns%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
It might be better to include some more information about the structure of the data you are querying so we can help better.Have you tried doing a row number on the columns ordered by reason and returning the first three results?
– auxoutin
Nov 7 at 9:15
What decides which row ends up in Reason_1, Reason_2, Reason_3 - do you have some kind of ordering column?
– Bridge
Nov 7 at 9:16
@Bridge Nothing, I only need to put the column "status" of the table Reason in Reason_1, 2 and 3.
– Dimitri Danilov
Nov 7 at 9:19