SQL subquery column not showing












-1















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:



enter image description here



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?










share|improve this question























  • 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
















-1















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:



enter image description here



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?










share|improve this question























  • 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














-1












-1








-1








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:



enter image description here



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?










share|improve this question














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:



enter image description here



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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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



















  • 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












2 Answers
2






active

oldest

votes


















1














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





share|improve this answer































    2














    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





    share|improve this answer
























    • 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











    • 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














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


    }
    });














    draft saved

    draft discarded


















    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









    1














    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





    share|improve this answer




























      1














      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





      share|improve this answer


























        1












        1








        1







        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





        share|improve this answer













        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






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 2 at 22:54









        forpasforpas

        19.2k3828




        19.2k3828

























            2














            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





            share|improve this answer
























            • 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











            • 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


















            2














            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





            share|improve this answer
























            • 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











            • 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
















            2












            2








            2







            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





            share|improve this answer













            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






            share|improve this answer












            share|improve this answer



            share|improve this answer










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











            • 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













            • 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



















            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




















            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            'app-layout' is not a known element: how to share Component with different Modules

            SQL update select statement