Using columns from different tables to create one table












0















I am not sure how I can get this code to have three columns for each category (Portrait,Landscape, and Abstract). As of now, it only has one column and all three of these values computed values:



SELECT SUM(Price) AS TotalSales_Portrait
FROM Photo
WHERE TransID IS NOT NULL AND PhotoID IN (
SELECT PhotoID
FROM Models)

UNION

SELECT SUM(Price) AS TotalSales_Landscape
FROM Photo
WHERE TransID IS NOT NULL AND PhotoID IN (
SELECT PhotoID
FROM Landscape)

UNION

SELECT SUM(Price) AS TotalSales_Abstract
FROM Photo
WHERE TransID IS NOT NULL AND PhotoID IN (
SELECT PhotoID
FROM Abstract)


Query output



Any advice is greatly appreciated










share|improve this question





























    0















    I am not sure how I can get this code to have three columns for each category (Portrait,Landscape, and Abstract). As of now, it only has one column and all three of these values computed values:



    SELECT SUM(Price) AS TotalSales_Portrait
    FROM Photo
    WHERE TransID IS NOT NULL AND PhotoID IN (
    SELECT PhotoID
    FROM Models)

    UNION

    SELECT SUM(Price) AS TotalSales_Landscape
    FROM Photo
    WHERE TransID IS NOT NULL AND PhotoID IN (
    SELECT PhotoID
    FROM Landscape)

    UNION

    SELECT SUM(Price) AS TotalSales_Abstract
    FROM Photo
    WHERE TransID IS NOT NULL AND PhotoID IN (
    SELECT PhotoID
    FROM Abstract)


    Query output



    Any advice is greatly appreciated










    share|improve this question



























      0












      0








      0








      I am not sure how I can get this code to have three columns for each category (Portrait,Landscape, and Abstract). As of now, it only has one column and all three of these values computed values:



      SELECT SUM(Price) AS TotalSales_Portrait
      FROM Photo
      WHERE TransID IS NOT NULL AND PhotoID IN (
      SELECT PhotoID
      FROM Models)

      UNION

      SELECT SUM(Price) AS TotalSales_Landscape
      FROM Photo
      WHERE TransID IS NOT NULL AND PhotoID IN (
      SELECT PhotoID
      FROM Landscape)

      UNION

      SELECT SUM(Price) AS TotalSales_Abstract
      FROM Photo
      WHERE TransID IS NOT NULL AND PhotoID IN (
      SELECT PhotoID
      FROM Abstract)


      Query output



      Any advice is greatly appreciated










      share|improve this question
















      I am not sure how I can get this code to have three columns for each category (Portrait,Landscape, and Abstract). As of now, it only has one column and all three of these values computed values:



      SELECT SUM(Price) AS TotalSales_Portrait
      FROM Photo
      WHERE TransID IS NOT NULL AND PhotoID IN (
      SELECT PhotoID
      FROM Models)

      UNION

      SELECT SUM(Price) AS TotalSales_Landscape
      FROM Photo
      WHERE TransID IS NOT NULL AND PhotoID IN (
      SELECT PhotoID
      FROM Landscape)

      UNION

      SELECT SUM(Price) AS TotalSales_Abstract
      FROM Photo
      WHERE TransID IS NOT NULL AND PhotoID IN (
      SELECT PhotoID
      FROM Abstract)


      Query output



      Any advice is greatly appreciated







      sql






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 26 '18 at 2:39







      Johngianni

















      asked Nov 26 '18 at 1:45









      JohngianniJohngianni

      207




      207
























          4 Answers
          4






          active

          oldest

          votes


















          0














          This can be done by using pivot as follows



          select *
          from (
          SELECT MAX('Portrait') as photo_type,SUM(Price) AS Total_Sales
          FROM Photo
          WHERE TransID IS NOT NULL AND PhotoID IN (
          SELECT PhotoID
          FROM Models)
          UNION
          SELECT MAX('Landscape'),SUM(Price) AS Total_Sales
          FROM Photo
          WHERE TransID IS NOT NULL AND PhotoID IN (
          SELECT PhotoID
          FROM Landscape)
          )x
          pivot (sum(total_sales) for photo_type in(Portrait,Landscape))y





          share|improve this answer
























          • This seems much more complicated than necessary.

            – Gordon Linoff
            Nov 26 '18 at 2:57



















          1














          Thank you George, this solved my problem. Here is the final code I have after revising yours:



          select *
          from (
          SELECT MAX('Portrait') as photo_type,SUM(Price) AS Total_Sales
          FROM Photo
          WHERE TransID IS NOT NULL AND PhotoID IN (
          SELECT PhotoID
          FROM Models)
          UNION
          SELECT MAX('Landscape'),SUM(Price) AS Total_Sales
          FROM Photo
          WHERE TransID IS NOT NULL AND PhotoID IN (
          SELECT PhotoID
          FROM Landscape)
          UNION
          SELECT MAX('Abstract'),SUM(Price) AS Total_Sales
          FROM Photo
          WHERE TransID IS NOT NULL AND PhotoID IN (
          SELECT PhotoID
          FROM Abstract)
          )x
          pivot (sum(Total_Sales) for photo_type in(Landscape,Abstract,Portrait))y





          share|improve this answer































            0














            If you have a photo type field in your Photos table it would be easier:



            SELECT SUM(price), photo_type 
            FROM Photos
            JOIN Models
            ON Models.PhotoID = Photos.PhotoID
            JOIN Landscape
            ON Landscape.PhotoID = Photos.PhotoID
            JOIN Abstract
            ON Abstract.PhotoID = Photos.PhotoID
            GROUP BY photo_type


            If not, you could probably do something like:



            SELECT SUM(price), type FROM (
            SELECT price, 'Models' as type from Photos
            WHERE TransID IS NOT NULL AND PhotoID IN (
            SELECT PhotoID
            FROM Models
            )

            UNION

            SELECT price, 'Landscape' as type from Photos
            WHERE TransID IS NOT NULL AND PhotoID IN (
            SELECT PhotoID
            FROM Landscape
            )

            UNION

            SELECT price, 'Abstract' as type from Abstract
            ) as photos
            GROUP BY type


            I haven't tested these queries, but they should be at least close to a working solution.






            share|improve this answer































              0














              I think I would simply use subqueries:



              SELECT (SELECT SUM(Price) AS TotalSales_Portrait
              FROM Photo
              WHERE TransID IS NOT NULL AND PhotoID IN (SELECT PhotoID FROM Models)
              ) as TotalSales_Portrait,
              (SELECT SUM(Price) AS TotalSales_Landscape
              FROM Photo
              WHERE TransID IS NOT NULL AND PhotoID IN (SELECT PhotoID FROM Landscape)
              ) as TotalSales_Landscape,
              (SELECT SUM(Price) AS TotalSales_Abstract
              FROM Photo
              WHERE TransID IS NOT NULL AND PhotoID IN (SELECT PhotoID FROM Abstract)
              ) as TotalSales_Abstract;


              Alternatively, if the secondary tables have no duplicates:



              SELECT SUM(CASE WHEN m.PhotoId IS NOT NULL THEN price ELSE 0 END),
              SUM(CASE WHEN l.PhotoId IS NOT NULL THEN price ELSE 0 END),
              SUM(CASE WHEN a.PhotoId IS NOT NULL THEN price ELSE 0 END)
              FROM Photos p LEFT JOIN
              Models m
              ON m.PhotoID = p.PhotoID LEFT JOIN
              Landscape l
              ON l.PhotoID = p.PhotoID LEFT JOIN
              Abstract a
              ON a.PhotoID = p.PhotoID





              share|improve this answer























                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%2f53473788%2fusing-columns-from-different-tables-to-create-one-table%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                0














                This can be done by using pivot as follows



                select *
                from (
                SELECT MAX('Portrait') as photo_type,SUM(Price) AS Total_Sales
                FROM Photo
                WHERE TransID IS NOT NULL AND PhotoID IN (
                SELECT PhotoID
                FROM Models)
                UNION
                SELECT MAX('Landscape'),SUM(Price) AS Total_Sales
                FROM Photo
                WHERE TransID IS NOT NULL AND PhotoID IN (
                SELECT PhotoID
                FROM Landscape)
                )x
                pivot (sum(total_sales) for photo_type in(Portrait,Landscape))y





                share|improve this answer
























                • This seems much more complicated than necessary.

                  – Gordon Linoff
                  Nov 26 '18 at 2:57
















                0














                This can be done by using pivot as follows



                select *
                from (
                SELECT MAX('Portrait') as photo_type,SUM(Price) AS Total_Sales
                FROM Photo
                WHERE TransID IS NOT NULL AND PhotoID IN (
                SELECT PhotoID
                FROM Models)
                UNION
                SELECT MAX('Landscape'),SUM(Price) AS Total_Sales
                FROM Photo
                WHERE TransID IS NOT NULL AND PhotoID IN (
                SELECT PhotoID
                FROM Landscape)
                )x
                pivot (sum(total_sales) for photo_type in(Portrait,Landscape))y





                share|improve this answer
























                • This seems much more complicated than necessary.

                  – Gordon Linoff
                  Nov 26 '18 at 2:57














                0












                0








                0







                This can be done by using pivot as follows



                select *
                from (
                SELECT MAX('Portrait') as photo_type,SUM(Price) AS Total_Sales
                FROM Photo
                WHERE TransID IS NOT NULL AND PhotoID IN (
                SELECT PhotoID
                FROM Models)
                UNION
                SELECT MAX('Landscape'),SUM(Price) AS Total_Sales
                FROM Photo
                WHERE TransID IS NOT NULL AND PhotoID IN (
                SELECT PhotoID
                FROM Landscape)
                )x
                pivot (sum(total_sales) for photo_type in(Portrait,Landscape))y





                share|improve this answer













                This can be done by using pivot as follows



                select *
                from (
                SELECT MAX('Portrait') as photo_type,SUM(Price) AS Total_Sales
                FROM Photo
                WHERE TransID IS NOT NULL AND PhotoID IN (
                SELECT PhotoID
                FROM Models)
                UNION
                SELECT MAX('Landscape'),SUM(Price) AS Total_Sales
                FROM Photo
                WHERE TransID IS NOT NULL AND PhotoID IN (
                SELECT PhotoID
                FROM Landscape)
                )x
                pivot (sum(total_sales) for photo_type in(Portrait,Landscape))y






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 26 '18 at 2:23









                George JosephGeorge Joseph

                1,590510




                1,590510













                • This seems much more complicated than necessary.

                  – Gordon Linoff
                  Nov 26 '18 at 2:57



















                • This seems much more complicated than necessary.

                  – Gordon Linoff
                  Nov 26 '18 at 2:57

















                This seems much more complicated than necessary.

                – Gordon Linoff
                Nov 26 '18 at 2:57





                This seems much more complicated than necessary.

                – Gordon Linoff
                Nov 26 '18 at 2:57













                1














                Thank you George, this solved my problem. Here is the final code I have after revising yours:



                select *
                from (
                SELECT MAX('Portrait') as photo_type,SUM(Price) AS Total_Sales
                FROM Photo
                WHERE TransID IS NOT NULL AND PhotoID IN (
                SELECT PhotoID
                FROM Models)
                UNION
                SELECT MAX('Landscape'),SUM(Price) AS Total_Sales
                FROM Photo
                WHERE TransID IS NOT NULL AND PhotoID IN (
                SELECT PhotoID
                FROM Landscape)
                UNION
                SELECT MAX('Abstract'),SUM(Price) AS Total_Sales
                FROM Photo
                WHERE TransID IS NOT NULL AND PhotoID IN (
                SELECT PhotoID
                FROM Abstract)
                )x
                pivot (sum(Total_Sales) for photo_type in(Landscape,Abstract,Portrait))y





                share|improve this answer




























                  1














                  Thank you George, this solved my problem. Here is the final code I have after revising yours:



                  select *
                  from (
                  SELECT MAX('Portrait') as photo_type,SUM(Price) AS Total_Sales
                  FROM Photo
                  WHERE TransID IS NOT NULL AND PhotoID IN (
                  SELECT PhotoID
                  FROM Models)
                  UNION
                  SELECT MAX('Landscape'),SUM(Price) AS Total_Sales
                  FROM Photo
                  WHERE TransID IS NOT NULL AND PhotoID IN (
                  SELECT PhotoID
                  FROM Landscape)
                  UNION
                  SELECT MAX('Abstract'),SUM(Price) AS Total_Sales
                  FROM Photo
                  WHERE TransID IS NOT NULL AND PhotoID IN (
                  SELECT PhotoID
                  FROM Abstract)
                  )x
                  pivot (sum(Total_Sales) for photo_type in(Landscape,Abstract,Portrait))y





                  share|improve this answer


























                    1












                    1








                    1







                    Thank you George, this solved my problem. Here is the final code I have after revising yours:



                    select *
                    from (
                    SELECT MAX('Portrait') as photo_type,SUM(Price) AS Total_Sales
                    FROM Photo
                    WHERE TransID IS NOT NULL AND PhotoID IN (
                    SELECT PhotoID
                    FROM Models)
                    UNION
                    SELECT MAX('Landscape'),SUM(Price) AS Total_Sales
                    FROM Photo
                    WHERE TransID IS NOT NULL AND PhotoID IN (
                    SELECT PhotoID
                    FROM Landscape)
                    UNION
                    SELECT MAX('Abstract'),SUM(Price) AS Total_Sales
                    FROM Photo
                    WHERE TransID IS NOT NULL AND PhotoID IN (
                    SELECT PhotoID
                    FROM Abstract)
                    )x
                    pivot (sum(Total_Sales) for photo_type in(Landscape,Abstract,Portrait))y





                    share|improve this answer













                    Thank you George, this solved my problem. Here is the final code I have after revising yours:



                    select *
                    from (
                    SELECT MAX('Portrait') as photo_type,SUM(Price) AS Total_Sales
                    FROM Photo
                    WHERE TransID IS NOT NULL AND PhotoID IN (
                    SELECT PhotoID
                    FROM Models)
                    UNION
                    SELECT MAX('Landscape'),SUM(Price) AS Total_Sales
                    FROM Photo
                    WHERE TransID IS NOT NULL AND PhotoID IN (
                    SELECT PhotoID
                    FROM Landscape)
                    UNION
                    SELECT MAX('Abstract'),SUM(Price) AS Total_Sales
                    FROM Photo
                    WHERE TransID IS NOT NULL AND PhotoID IN (
                    SELECT PhotoID
                    FROM Abstract)
                    )x
                    pivot (sum(Total_Sales) for photo_type in(Landscape,Abstract,Portrait))y






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 26 '18 at 2:40









                    JohngianniJohngianni

                    207




                    207























                        0














                        If you have a photo type field in your Photos table it would be easier:



                        SELECT SUM(price), photo_type 
                        FROM Photos
                        JOIN Models
                        ON Models.PhotoID = Photos.PhotoID
                        JOIN Landscape
                        ON Landscape.PhotoID = Photos.PhotoID
                        JOIN Abstract
                        ON Abstract.PhotoID = Photos.PhotoID
                        GROUP BY photo_type


                        If not, you could probably do something like:



                        SELECT SUM(price), type FROM (
                        SELECT price, 'Models' as type from Photos
                        WHERE TransID IS NOT NULL AND PhotoID IN (
                        SELECT PhotoID
                        FROM Models
                        )

                        UNION

                        SELECT price, 'Landscape' as type from Photos
                        WHERE TransID IS NOT NULL AND PhotoID IN (
                        SELECT PhotoID
                        FROM Landscape
                        )

                        UNION

                        SELECT price, 'Abstract' as type from Abstract
                        ) as photos
                        GROUP BY type


                        I haven't tested these queries, but they should be at least close to a working solution.






                        share|improve this answer




























                          0














                          If you have a photo type field in your Photos table it would be easier:



                          SELECT SUM(price), photo_type 
                          FROM Photos
                          JOIN Models
                          ON Models.PhotoID = Photos.PhotoID
                          JOIN Landscape
                          ON Landscape.PhotoID = Photos.PhotoID
                          JOIN Abstract
                          ON Abstract.PhotoID = Photos.PhotoID
                          GROUP BY photo_type


                          If not, you could probably do something like:



                          SELECT SUM(price), type FROM (
                          SELECT price, 'Models' as type from Photos
                          WHERE TransID IS NOT NULL AND PhotoID IN (
                          SELECT PhotoID
                          FROM Models
                          )

                          UNION

                          SELECT price, 'Landscape' as type from Photos
                          WHERE TransID IS NOT NULL AND PhotoID IN (
                          SELECT PhotoID
                          FROM Landscape
                          )

                          UNION

                          SELECT price, 'Abstract' as type from Abstract
                          ) as photos
                          GROUP BY type


                          I haven't tested these queries, but they should be at least close to a working solution.






                          share|improve this answer


























                            0












                            0








                            0







                            If you have a photo type field in your Photos table it would be easier:



                            SELECT SUM(price), photo_type 
                            FROM Photos
                            JOIN Models
                            ON Models.PhotoID = Photos.PhotoID
                            JOIN Landscape
                            ON Landscape.PhotoID = Photos.PhotoID
                            JOIN Abstract
                            ON Abstract.PhotoID = Photos.PhotoID
                            GROUP BY photo_type


                            If not, you could probably do something like:



                            SELECT SUM(price), type FROM (
                            SELECT price, 'Models' as type from Photos
                            WHERE TransID IS NOT NULL AND PhotoID IN (
                            SELECT PhotoID
                            FROM Models
                            )

                            UNION

                            SELECT price, 'Landscape' as type from Photos
                            WHERE TransID IS NOT NULL AND PhotoID IN (
                            SELECT PhotoID
                            FROM Landscape
                            )

                            UNION

                            SELECT price, 'Abstract' as type from Abstract
                            ) as photos
                            GROUP BY type


                            I haven't tested these queries, but they should be at least close to a working solution.






                            share|improve this answer













                            If you have a photo type field in your Photos table it would be easier:



                            SELECT SUM(price), photo_type 
                            FROM Photos
                            JOIN Models
                            ON Models.PhotoID = Photos.PhotoID
                            JOIN Landscape
                            ON Landscape.PhotoID = Photos.PhotoID
                            JOIN Abstract
                            ON Abstract.PhotoID = Photos.PhotoID
                            GROUP BY photo_type


                            If not, you could probably do something like:



                            SELECT SUM(price), type FROM (
                            SELECT price, 'Models' as type from Photos
                            WHERE TransID IS NOT NULL AND PhotoID IN (
                            SELECT PhotoID
                            FROM Models
                            )

                            UNION

                            SELECT price, 'Landscape' as type from Photos
                            WHERE TransID IS NOT NULL AND PhotoID IN (
                            SELECT PhotoID
                            FROM Landscape
                            )

                            UNION

                            SELECT price, 'Abstract' as type from Abstract
                            ) as photos
                            GROUP BY type


                            I haven't tested these queries, but they should be at least close to a working solution.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 26 '18 at 2:14









                            arodarod

                            5,00151725




                            5,00151725























                                0














                                I think I would simply use subqueries:



                                SELECT (SELECT SUM(Price) AS TotalSales_Portrait
                                FROM Photo
                                WHERE TransID IS NOT NULL AND PhotoID IN (SELECT PhotoID FROM Models)
                                ) as TotalSales_Portrait,
                                (SELECT SUM(Price) AS TotalSales_Landscape
                                FROM Photo
                                WHERE TransID IS NOT NULL AND PhotoID IN (SELECT PhotoID FROM Landscape)
                                ) as TotalSales_Landscape,
                                (SELECT SUM(Price) AS TotalSales_Abstract
                                FROM Photo
                                WHERE TransID IS NOT NULL AND PhotoID IN (SELECT PhotoID FROM Abstract)
                                ) as TotalSales_Abstract;


                                Alternatively, if the secondary tables have no duplicates:



                                SELECT SUM(CASE WHEN m.PhotoId IS NOT NULL THEN price ELSE 0 END),
                                SUM(CASE WHEN l.PhotoId IS NOT NULL THEN price ELSE 0 END),
                                SUM(CASE WHEN a.PhotoId IS NOT NULL THEN price ELSE 0 END)
                                FROM Photos p LEFT JOIN
                                Models m
                                ON m.PhotoID = p.PhotoID LEFT JOIN
                                Landscape l
                                ON l.PhotoID = p.PhotoID LEFT JOIN
                                Abstract a
                                ON a.PhotoID = p.PhotoID





                                share|improve this answer




























                                  0














                                  I think I would simply use subqueries:



                                  SELECT (SELECT SUM(Price) AS TotalSales_Portrait
                                  FROM Photo
                                  WHERE TransID IS NOT NULL AND PhotoID IN (SELECT PhotoID FROM Models)
                                  ) as TotalSales_Portrait,
                                  (SELECT SUM(Price) AS TotalSales_Landscape
                                  FROM Photo
                                  WHERE TransID IS NOT NULL AND PhotoID IN (SELECT PhotoID FROM Landscape)
                                  ) as TotalSales_Landscape,
                                  (SELECT SUM(Price) AS TotalSales_Abstract
                                  FROM Photo
                                  WHERE TransID IS NOT NULL AND PhotoID IN (SELECT PhotoID FROM Abstract)
                                  ) as TotalSales_Abstract;


                                  Alternatively, if the secondary tables have no duplicates:



                                  SELECT SUM(CASE WHEN m.PhotoId IS NOT NULL THEN price ELSE 0 END),
                                  SUM(CASE WHEN l.PhotoId IS NOT NULL THEN price ELSE 0 END),
                                  SUM(CASE WHEN a.PhotoId IS NOT NULL THEN price ELSE 0 END)
                                  FROM Photos p LEFT JOIN
                                  Models m
                                  ON m.PhotoID = p.PhotoID LEFT JOIN
                                  Landscape l
                                  ON l.PhotoID = p.PhotoID LEFT JOIN
                                  Abstract a
                                  ON a.PhotoID = p.PhotoID





                                  share|improve this answer


























                                    0












                                    0








                                    0







                                    I think I would simply use subqueries:



                                    SELECT (SELECT SUM(Price) AS TotalSales_Portrait
                                    FROM Photo
                                    WHERE TransID IS NOT NULL AND PhotoID IN (SELECT PhotoID FROM Models)
                                    ) as TotalSales_Portrait,
                                    (SELECT SUM(Price) AS TotalSales_Landscape
                                    FROM Photo
                                    WHERE TransID IS NOT NULL AND PhotoID IN (SELECT PhotoID FROM Landscape)
                                    ) as TotalSales_Landscape,
                                    (SELECT SUM(Price) AS TotalSales_Abstract
                                    FROM Photo
                                    WHERE TransID IS NOT NULL AND PhotoID IN (SELECT PhotoID FROM Abstract)
                                    ) as TotalSales_Abstract;


                                    Alternatively, if the secondary tables have no duplicates:



                                    SELECT SUM(CASE WHEN m.PhotoId IS NOT NULL THEN price ELSE 0 END),
                                    SUM(CASE WHEN l.PhotoId IS NOT NULL THEN price ELSE 0 END),
                                    SUM(CASE WHEN a.PhotoId IS NOT NULL THEN price ELSE 0 END)
                                    FROM Photos p LEFT JOIN
                                    Models m
                                    ON m.PhotoID = p.PhotoID LEFT JOIN
                                    Landscape l
                                    ON l.PhotoID = p.PhotoID LEFT JOIN
                                    Abstract a
                                    ON a.PhotoID = p.PhotoID





                                    share|improve this answer













                                    I think I would simply use subqueries:



                                    SELECT (SELECT SUM(Price) AS TotalSales_Portrait
                                    FROM Photo
                                    WHERE TransID IS NOT NULL AND PhotoID IN (SELECT PhotoID FROM Models)
                                    ) as TotalSales_Portrait,
                                    (SELECT SUM(Price) AS TotalSales_Landscape
                                    FROM Photo
                                    WHERE TransID IS NOT NULL AND PhotoID IN (SELECT PhotoID FROM Landscape)
                                    ) as TotalSales_Landscape,
                                    (SELECT SUM(Price) AS TotalSales_Abstract
                                    FROM Photo
                                    WHERE TransID IS NOT NULL AND PhotoID IN (SELECT PhotoID FROM Abstract)
                                    ) as TotalSales_Abstract;


                                    Alternatively, if the secondary tables have no duplicates:



                                    SELECT SUM(CASE WHEN m.PhotoId IS NOT NULL THEN price ELSE 0 END),
                                    SUM(CASE WHEN l.PhotoId IS NOT NULL THEN price ELSE 0 END),
                                    SUM(CASE WHEN a.PhotoId IS NOT NULL THEN price ELSE 0 END)
                                    FROM Photos p LEFT JOIN
                                    Models m
                                    ON m.PhotoID = p.PhotoID LEFT JOIN
                                    Landscape l
                                    ON l.PhotoID = p.PhotoID LEFT JOIN
                                    Abstract a
                                    ON a.PhotoID = p.PhotoID






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 26 '18 at 2:57









                                    Gordon LinoffGordon Linoff

                                    789k35314418




                                    789k35314418






























                                        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%2f53473788%2fusing-columns-from-different-tables-to-create-one-table%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

                                        Costa Masnaga

                                        Fotorealismo

                                        Create new schema in PostgreSQL using DBeaver