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?
sql subquery visual-foxpro
add a comment |
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?
sql subquery visual-foxpro
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
add a comment |
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?
sql subquery visual-foxpro
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
sql subquery visual-foxpro
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
edited Nov 12 at 16:33
Rob
247
247
answered Nov 9 at 21:11
DouadyRabbit
262
262
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53195507%2fsql-math-operation-in-correlated-subquery%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
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