SQL subquery column not showing
Currently I am attempting to query particular columns from two separate tables. To do this, I am electing to use a SELECT
statement within the FROM
clause.
Here is the following query I am attempting:
SELECT Employees.Last_Name, Employees.First_Name, Employees.PositionID FROM
Employees,
(SELECT Positions.PositionID, SUM(Positions.Position_Salary) AS Salary FROM
Positions GROUP BY PositionID) AS Subquery
WHERE Employees.PositionID = Subquery.PositionID
As we can see, I am trying to group each record according to its respective PositionID
with a summation column for Salary
. Unfortunately, the Salary
column doesn't appear.
Observe:
In subsequent tests, I removed the Employee.First_Name, Employee.Last_Name but the summed column of Salary still did not appear.
Any idea what is going on here?
mysql sql
|
show 4 more comments
Currently I am attempting to query particular columns from two separate tables. To do this, I am electing to use a SELECT
statement within the FROM
clause.
Here is the following query I am attempting:
SELECT Employees.Last_Name, Employees.First_Name, Employees.PositionID FROM
Employees,
(SELECT Positions.PositionID, SUM(Positions.Position_Salary) AS Salary FROM
Positions GROUP BY PositionID) AS Subquery
WHERE Employees.PositionID = Subquery.PositionID
As we can see, I am trying to group each record according to its respective PositionID
with a summation column for Salary
. Unfortunately, the Salary
column doesn't appear.
Observe:
In subsequent tests, I removed the Employee.First_Name, Employee.Last_Name but the summed column of Salary still did not appear.
Any idea what is going on here?
mysql sql
Try Selecting Subquery.SALARY column in your first SELECT
– RN92
Jan 2 at 22:27
yes but Salary is located in the Positions table, not in the Employees table (which is what the first select statement is pulling from)
– Matthew
Jan 2 at 22:28
What happened when you try to SELECT Subquery.Salary in first SELECT?
– RN92
Jan 2 at 22:30
See my reply to The Impaler below. He suggested that as well.
– Matthew
Jan 2 at 22:31
1
@Matthew Is an employee assigned more than 1 positions and are there more than 1 salaries for the same position?
– forpas
Jan 2 at 22:38
|
show 4 more comments
Currently I am attempting to query particular columns from two separate tables. To do this, I am electing to use a SELECT
statement within the FROM
clause.
Here is the following query I am attempting:
SELECT Employees.Last_Name, Employees.First_Name, Employees.PositionID FROM
Employees,
(SELECT Positions.PositionID, SUM(Positions.Position_Salary) AS Salary FROM
Positions GROUP BY PositionID) AS Subquery
WHERE Employees.PositionID = Subquery.PositionID
As we can see, I am trying to group each record according to its respective PositionID
with a summation column for Salary
. Unfortunately, the Salary
column doesn't appear.
Observe:
In subsequent tests, I removed the Employee.First_Name, Employee.Last_Name but the summed column of Salary still did not appear.
Any idea what is going on here?
mysql sql
Currently I am attempting to query particular columns from two separate tables. To do this, I am electing to use a SELECT
statement within the FROM
clause.
Here is the following query I am attempting:
SELECT Employees.Last_Name, Employees.First_Name, Employees.PositionID FROM
Employees,
(SELECT Positions.PositionID, SUM(Positions.Position_Salary) AS Salary FROM
Positions GROUP BY PositionID) AS Subquery
WHERE Employees.PositionID = Subquery.PositionID
As we can see, I am trying to group each record according to its respective PositionID
with a summation column for Salary
. Unfortunately, the Salary
column doesn't appear.
Observe:
In subsequent tests, I removed the Employee.First_Name, Employee.Last_Name but the summed column of Salary still did not appear.
Any idea what is going on here?
mysql sql
mysql sql
asked Jan 2 at 22:25
MatthewMatthew
198114
198114
Try Selecting Subquery.SALARY column in your first SELECT
– RN92
Jan 2 at 22:27
yes but Salary is located in the Positions table, not in the Employees table (which is what the first select statement is pulling from)
– Matthew
Jan 2 at 22:28
What happened when you try to SELECT Subquery.Salary in first SELECT?
– RN92
Jan 2 at 22:30
See my reply to The Impaler below. He suggested that as well.
– Matthew
Jan 2 at 22:31
1
@Matthew Is an employee assigned more than 1 positions and are there more than 1 salaries for the same position?
– forpas
Jan 2 at 22:38
|
show 4 more comments
Try Selecting Subquery.SALARY column in your first SELECT
– RN92
Jan 2 at 22:27
yes but Salary is located in the Positions table, not in the Employees table (which is what the first select statement is pulling from)
– Matthew
Jan 2 at 22:28
What happened when you try to SELECT Subquery.Salary in first SELECT?
– RN92
Jan 2 at 22:30
See my reply to The Impaler below. He suggested that as well.
– Matthew
Jan 2 at 22:31
1
@Matthew Is an employee assigned more than 1 positions and are there more than 1 salaries for the same position?
– forpas
Jan 2 at 22:38
Try Selecting Subquery.SALARY column in your first SELECT
– RN92
Jan 2 at 22:27
Try Selecting Subquery.SALARY column in your first SELECT
– RN92
Jan 2 at 22:27
yes but Salary is located in the Positions table, not in the Employees table (which is what the first select statement is pulling from)
– Matthew
Jan 2 at 22:28
yes but Salary is located in the Positions table, not in the Employees table (which is what the first select statement is pulling from)
– Matthew
Jan 2 at 22:28
What happened when you try to SELECT Subquery.Salary in first SELECT?
– RN92
Jan 2 at 22:30
What happened when you try to SELECT Subquery.Salary in first SELECT?
– RN92
Jan 2 at 22:30
See my reply to The Impaler below. He suggested that as well.
– Matthew
Jan 2 at 22:31
See my reply to The Impaler below. He suggested that as well.
– Matthew
Jan 2 at 22:31
1
1
@Matthew Is an employee assigned more than 1 positions and are there more than 1 salaries for the same position?
– forpas
Jan 2 at 22:38
@Matthew Is an employee assigned more than 1 positions and are there more than 1 salaries for the same position?
– forpas
Jan 2 at 22:38
|
show 4 more comments
2 Answers
2
active
oldest
votes
If I understood you want for each employee the total salaries of its position:
SELECT
Employees.Last_Name,
Employees.First_Name,
Employees.PositionID,
(SELECT SUM(Positions.Position_Salary)
FROM Positions
WHERE Employees.PositionID = Positions.PositionID) AS TotalSalary
FROM Employees
add a comment |
You need to add the Salary
column to the SELECT
clause:
SELECT Employees.Last_Name, Employees.First_Name, Employees.PositionID,
Subquery.Salary -- added it here
FROM Employees,
(SELECT Positions.PositionID, SUM(Positions.Position_Salary) AS Salary FROM
Positions GROUP BY PositionID) AS Subquery
WHERE Employees.PositionID = Subquery.PositionID
That shows the Salary per individual, but unfortunately it does not group salaries according to the PositionID (indicated by theGROUP BY
clause)
– Matthew
Jan 2 at 22:30
How come? In your query you definedSalary
asSUM(Positions.Position_Salary)
. It is the aggregation ofPosition_Salary
.
– The Impaler
Jan 2 at 22:32
I'm not sure why its not working. It displays Salary as I said, but it does not aggregate according to Position_Salary*
– Matthew
Jan 2 at 22:35
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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%2f54013962%2fsql-subquery-column-not-showing%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
If I understood you want for each employee the total salaries of its position:
SELECT
Employees.Last_Name,
Employees.First_Name,
Employees.PositionID,
(SELECT SUM(Positions.Position_Salary)
FROM Positions
WHERE Employees.PositionID = Positions.PositionID) AS TotalSalary
FROM Employees
add a comment |
If I understood you want for each employee the total salaries of its position:
SELECT
Employees.Last_Name,
Employees.First_Name,
Employees.PositionID,
(SELECT SUM(Positions.Position_Salary)
FROM Positions
WHERE Employees.PositionID = Positions.PositionID) AS TotalSalary
FROM Employees
add a comment |
If I understood you want for each employee the total salaries of its position:
SELECT
Employees.Last_Name,
Employees.First_Name,
Employees.PositionID,
(SELECT SUM(Positions.Position_Salary)
FROM Positions
WHERE Employees.PositionID = Positions.PositionID) AS TotalSalary
FROM Employees
If I understood you want for each employee the total salaries of its position:
SELECT
Employees.Last_Name,
Employees.First_Name,
Employees.PositionID,
(SELECT SUM(Positions.Position_Salary)
FROM Positions
WHERE Employees.PositionID = Positions.PositionID) AS TotalSalary
FROM Employees
answered Jan 2 at 22:54
forpasforpas
19.2k3828
19.2k3828
add a comment |
add a comment |
You need to add the Salary
column to the SELECT
clause:
SELECT Employees.Last_Name, Employees.First_Name, Employees.PositionID,
Subquery.Salary -- added it here
FROM Employees,
(SELECT Positions.PositionID, SUM(Positions.Position_Salary) AS Salary FROM
Positions GROUP BY PositionID) AS Subquery
WHERE Employees.PositionID = Subquery.PositionID
That shows the Salary per individual, but unfortunately it does not group salaries according to the PositionID (indicated by theGROUP BY
clause)
– Matthew
Jan 2 at 22:30
How come? In your query you definedSalary
asSUM(Positions.Position_Salary)
. It is the aggregation ofPosition_Salary
.
– The Impaler
Jan 2 at 22:32
I'm not sure why its not working. It displays Salary as I said, but it does not aggregate according to Position_Salary*
– Matthew
Jan 2 at 22:35
add a comment |
You need to add the Salary
column to the SELECT
clause:
SELECT Employees.Last_Name, Employees.First_Name, Employees.PositionID,
Subquery.Salary -- added it here
FROM Employees,
(SELECT Positions.PositionID, SUM(Positions.Position_Salary) AS Salary FROM
Positions GROUP BY PositionID) AS Subquery
WHERE Employees.PositionID = Subquery.PositionID
That shows the Salary per individual, but unfortunately it does not group salaries according to the PositionID (indicated by theGROUP BY
clause)
– Matthew
Jan 2 at 22:30
How come? In your query you definedSalary
asSUM(Positions.Position_Salary)
. It is the aggregation ofPosition_Salary
.
– The Impaler
Jan 2 at 22:32
I'm not sure why its not working. It displays Salary as I said, but it does not aggregate according to Position_Salary*
– Matthew
Jan 2 at 22:35
add a comment |
You need to add the Salary
column to the SELECT
clause:
SELECT Employees.Last_Name, Employees.First_Name, Employees.PositionID,
Subquery.Salary -- added it here
FROM Employees,
(SELECT Positions.PositionID, SUM(Positions.Position_Salary) AS Salary FROM
Positions GROUP BY PositionID) AS Subquery
WHERE Employees.PositionID = Subquery.PositionID
You need to add the Salary
column to the SELECT
clause:
SELECT Employees.Last_Name, Employees.First_Name, Employees.PositionID,
Subquery.Salary -- added it here
FROM Employees,
(SELECT Positions.PositionID, SUM(Positions.Position_Salary) AS Salary FROM
Positions GROUP BY PositionID) AS Subquery
WHERE Employees.PositionID = Subquery.PositionID
answered Jan 2 at 22:27
The ImpalerThe Impaler
11.5k41441
11.5k41441
That shows the Salary per individual, but unfortunately it does not group salaries according to the PositionID (indicated by theGROUP BY
clause)
– Matthew
Jan 2 at 22:30
How come? In your query you definedSalary
asSUM(Positions.Position_Salary)
. It is the aggregation ofPosition_Salary
.
– The Impaler
Jan 2 at 22:32
I'm not sure why its not working. It displays Salary as I said, but it does not aggregate according to Position_Salary*
– Matthew
Jan 2 at 22:35
add a comment |
That shows the Salary per individual, but unfortunately it does not group salaries according to the PositionID (indicated by theGROUP BY
clause)
– Matthew
Jan 2 at 22:30
How come? In your query you definedSalary
asSUM(Positions.Position_Salary)
. It is the aggregation ofPosition_Salary
.
– The Impaler
Jan 2 at 22:32
I'm not sure why its not working. It displays Salary as I said, but it does not aggregate according to Position_Salary*
– Matthew
Jan 2 at 22:35
That shows the Salary per individual, but unfortunately it does not group salaries according to the PositionID (indicated by the
GROUP BY
clause)– Matthew
Jan 2 at 22:30
That shows the Salary per individual, but unfortunately it does not group salaries according to the PositionID (indicated by the
GROUP BY
clause)– Matthew
Jan 2 at 22:30
How come? In your query you defined
Salary
as SUM(Positions.Position_Salary)
. It is the aggregation of Position_Salary
.– The Impaler
Jan 2 at 22:32
How come? In your query you defined
Salary
as SUM(Positions.Position_Salary)
. It is the aggregation of Position_Salary
.– The Impaler
Jan 2 at 22:32
I'm not sure why its not working. It displays Salary as I said, but it does not aggregate according to Position_Salary*
– Matthew
Jan 2 at 22:35
I'm not sure why its not working. It displays Salary as I said, but it does not aggregate according to Position_Salary*
– Matthew
Jan 2 at 22:35
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f54013962%2fsql-subquery-column-not-showing%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
Try Selecting Subquery.SALARY column in your first SELECT
– RN92
Jan 2 at 22:27
yes but Salary is located in the Positions table, not in the Employees table (which is what the first select statement is pulling from)
– Matthew
Jan 2 at 22:28
What happened when you try to SELECT Subquery.Salary in first SELECT?
– RN92
Jan 2 at 22:30
See my reply to The Impaler below. He suggested that as well.
– Matthew
Jan 2 at 22:31
1
@Matthew Is an employee assigned more than 1 positions and are there more than 1 salaries for the same position?
– forpas
Jan 2 at 22:38