SQL Math Operation In Correlated Subquery











up vote
0
down vote

favorite












I am working with three tables, basically, one is a bill of materials, one contains part inventory, and the last one contains work orders or jobs. I am trying to find out if it is possible to have a correlated subquery that can perform a math operation using a value from the outer query. Here's an example of what I'm trying to do:



SELECT A.work_order,A.assembly,A.job_quantity,
(SELECT COUNT(X.part_number)
FROM bom X
WHERE X.assembly = A.assembly
AND (X.quantity_required * A.job_quantity) >= (SELECT Y.quantity_available FROM inventory Y WHERE
Y.part_number = X.part_number)) AS negatives
FROM work_orders A
ORDER BY A.assembly ASC


I am attempting to find out, for a given work order, if there are parts that we do not have enough of to build the assembly. I'm currently getting an "Error correlating fields" error. Is it possible to do this kind of operation in a single query?










share|improve this question






















  • Can you do the calculations in a derived table that you then join to the main table in the FROM clause?
    – Tamar E. Granor
    Nov 7 at 21:42










  • @TamarE.Granor - Would you mind giving an example of what you were thinking? I can't seem to figure out how it would work with a derived table.
    – Rob
    Nov 7 at 22:40










  • I'd have to understand your data better, I think. But the question is basically whether you can create some intermediate tables via query that contain data you need and then join them to the main table. I'm not sure you can looking at your example, but start by trying to do the task in a series of queries. If you can make that work, post that here and we can help you turn it into a single query.
    – Tamar E. Granor
    Nov 8 at 21:22

















up vote
0
down vote

favorite












I am working with three tables, basically, one is a bill of materials, one contains part inventory, and the last one contains work orders or jobs. I am trying to find out if it is possible to have a correlated subquery that can perform a math operation using a value from the outer query. Here's an example of what I'm trying to do:



SELECT A.work_order,A.assembly,A.job_quantity,
(SELECT COUNT(X.part_number)
FROM bom X
WHERE X.assembly = A.assembly
AND (X.quantity_required * A.job_quantity) >= (SELECT Y.quantity_available FROM inventory Y WHERE
Y.part_number = X.part_number)) AS negatives
FROM work_orders A
ORDER BY A.assembly ASC


I am attempting to find out, for a given work order, if there are parts that we do not have enough of to build the assembly. I'm currently getting an "Error correlating fields" error. Is it possible to do this kind of operation in a single query?










share|improve this question






















  • Can you do the calculations in a derived table that you then join to the main table in the FROM clause?
    – Tamar E. Granor
    Nov 7 at 21:42










  • @TamarE.Granor - Would you mind giving an example of what you were thinking? I can't seem to figure out how it would work with a derived table.
    – Rob
    Nov 7 at 22:40










  • I'd have to understand your data better, I think. But the question is basically whether you can create some intermediate tables via query that contain data you need and then join them to the main table. I'm not sure you can looking at your example, but start by trying to do the task in a series of queries. If you can make that work, post that here and we can help you turn it into a single query.
    – Tamar E. Granor
    Nov 8 at 21:22















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am working with three tables, basically, one is a bill of materials, one contains part inventory, and the last one contains work orders or jobs. I am trying to find out if it is possible to have a correlated subquery that can perform a math operation using a value from the outer query. Here's an example of what I'm trying to do:



SELECT A.work_order,A.assembly,A.job_quantity,
(SELECT COUNT(X.part_number)
FROM bom X
WHERE X.assembly = A.assembly
AND (X.quantity_required * A.job_quantity) >= (SELECT Y.quantity_available FROM inventory Y WHERE
Y.part_number = X.part_number)) AS negatives
FROM work_orders A
ORDER BY A.assembly ASC


I am attempting to find out, for a given work order, if there are parts that we do not have enough of to build the assembly. I'm currently getting an "Error correlating fields" error. Is it possible to do this kind of operation in a single query?










share|improve this question













I am working with three tables, basically, one is a bill of materials, one contains part inventory, and the last one contains work orders or jobs. I am trying to find out if it is possible to have a correlated subquery that can perform a math operation using a value from the outer query. Here's an example of what I'm trying to do:



SELECT A.work_order,A.assembly,A.job_quantity,
(SELECT COUNT(X.part_number)
FROM bom X
WHERE X.assembly = A.assembly
AND (X.quantity_required * A.job_quantity) >= (SELECT Y.quantity_available FROM inventory Y WHERE
Y.part_number = X.part_number)) AS negatives
FROM work_orders A
ORDER BY A.assembly ASC


I am attempting to find out, for a given work order, if there are parts that we do not have enough of to build the assembly. I'm currently getting an "Error correlating fields" error. Is it possible to do this kind of operation in a single query?







sql subquery visual-foxpro






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 7 at 18:21









Rob

247




247












  • Can you do the calculations in a derived table that you then join to the main table in the FROM clause?
    – Tamar E. Granor
    Nov 7 at 21:42










  • @TamarE.Granor - Would you mind giving an example of what you were thinking? I can't seem to figure out how it would work with a derived table.
    – Rob
    Nov 7 at 22:40










  • I'd have to understand your data better, I think. But the question is basically whether you can create some intermediate tables via query that contain data you need and then join them to the main table. I'm not sure you can looking at your example, but start by trying to do the task in a series of queries. If you can make that work, post that here and we can help you turn it into a single query.
    – Tamar E. Granor
    Nov 8 at 21:22




















  • Can you do the calculations in a derived table that you then join to the main table in the FROM clause?
    – Tamar E. Granor
    Nov 7 at 21:42










  • @TamarE.Granor - Would you mind giving an example of what you were thinking? I can't seem to figure out how it would work with a derived table.
    – Rob
    Nov 7 at 22:40










  • I'd have to understand your data better, I think. But the question is basically whether you can create some intermediate tables via query that contain data you need and then join them to the main table. I'm not sure you can looking at your example, but start by trying to do the task in a series of queries. If you can make that work, post that here and we can help you turn it into a single query.
    – Tamar E. Granor
    Nov 8 at 21:22


















Can you do the calculations in a derived table that you then join to the main table in the FROM clause?
– Tamar E. Granor
Nov 7 at 21:42




Can you do the calculations in a derived table that you then join to the main table in the FROM clause?
– Tamar E. Granor
Nov 7 at 21:42












@TamarE.Granor - Would you mind giving an example of what you were thinking? I can't seem to figure out how it would work with a derived table.
– Rob
Nov 7 at 22:40




@TamarE.Granor - Would you mind giving an example of what you were thinking? I can't seem to figure out how it would work with a derived table.
– Rob
Nov 7 at 22:40












I'd have to understand your data better, I think. But the question is basically whether you can create some intermediate tables via query that contain data you need and then join them to the main table. I'm not sure you can looking at your example, but start by trying to do the task in a series of queries. If you can make that work, post that here and we can help you turn it into a single query.
– Tamar E. Granor
Nov 8 at 21:22






I'd have to understand your data better, I think. But the question is basically whether you can create some intermediate tables via query that contain data you need and then join them to the main table. I'm not sure you can looking at your example, but start by trying to do the task in a series of queries. If you can make that work, post that here and we can help you turn it into a single query.
– Tamar E. Granor
Nov 8 at 21:22














1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










Try moving the subquery to a join, something like this:



   SELECT a.work_order, a.assembly, a.job_quantity, n.negatives
FROM work_orders a JOIN (SELECT x.part_number, COUNT(x.part_number) as negatives
FROM bom x JOIN work_orders b
ON x.assembly = b.assembly
WHERE (x.quantity_required * b.job_quantity) >= (SELECT y.quantity_available
FROM inventory y WHERE
y.part_number = x.part_number)
GROUP BY x.part_number) n
ON a.part_number = n.part_number
ORDER BY a.assembly ASC


Or create a temporary cursor with the subquery and then use it to join the main table.
Hope this helps.



Luis






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',
    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%2f53195507%2fsql-math-operation-in-correlated-subquery%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    Try moving the subquery to a join, something like this:



       SELECT a.work_order, a.assembly, a.job_quantity, n.negatives
    FROM work_orders a JOIN (SELECT x.part_number, COUNT(x.part_number) as negatives
    FROM bom x JOIN work_orders b
    ON x.assembly = b.assembly
    WHERE (x.quantity_required * b.job_quantity) >= (SELECT y.quantity_available
    FROM inventory y WHERE
    y.part_number = x.part_number)
    GROUP BY x.part_number) n
    ON a.part_number = n.part_number
    ORDER BY a.assembly ASC


    Or create a temporary cursor with the subquery and then use it to join the main table.
    Hope this helps.



    Luis






    share|improve this answer



























      up vote
      1
      down vote



      accepted










      Try moving the subquery to a join, something like this:



         SELECT a.work_order, a.assembly, a.job_quantity, n.negatives
      FROM work_orders a JOIN (SELECT x.part_number, COUNT(x.part_number) as negatives
      FROM bom x JOIN work_orders b
      ON x.assembly = b.assembly
      WHERE (x.quantity_required * b.job_quantity) >= (SELECT y.quantity_available
      FROM inventory y WHERE
      y.part_number = x.part_number)
      GROUP BY x.part_number) n
      ON a.part_number = n.part_number
      ORDER BY a.assembly ASC


      Or create a temporary cursor with the subquery and then use it to join the main table.
      Hope this helps.



      Luis






      share|improve this answer

























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        Try moving the subquery to a join, something like this:



           SELECT a.work_order, a.assembly, a.job_quantity, n.negatives
        FROM work_orders a JOIN (SELECT x.part_number, COUNT(x.part_number) as negatives
        FROM bom x JOIN work_orders b
        ON x.assembly = b.assembly
        WHERE (x.quantity_required * b.job_quantity) >= (SELECT y.quantity_available
        FROM inventory y WHERE
        y.part_number = x.part_number)
        GROUP BY x.part_number) n
        ON a.part_number = n.part_number
        ORDER BY a.assembly ASC


        Or create a temporary cursor with the subquery and then use it to join the main table.
        Hope this helps.



        Luis






        share|improve this answer














        Try moving the subquery to a join, something like this:



           SELECT a.work_order, a.assembly, a.job_quantity, n.negatives
        FROM work_orders a JOIN (SELECT x.part_number, COUNT(x.part_number) as negatives
        FROM bom x JOIN work_orders b
        ON x.assembly = b.assembly
        WHERE (x.quantity_required * b.job_quantity) >= (SELECT y.quantity_available
        FROM inventory y WHERE
        y.part_number = x.part_number)
        GROUP BY x.part_number) n
        ON a.part_number = n.part_number
        ORDER BY a.assembly ASC


        Or create a temporary cursor with the subquery and then use it to join the main table.
        Hope this helps.



        Luis







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 12 at 16:33









        Rob

        247




        247










        answered Nov 9 at 21:11









        DouadyRabbit

        262




        262






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53195507%2fsql-math-operation-in-correlated-subquery%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







            這個網誌中的熱門文章

            Hercules Kyvelos

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud