Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...you guys have given us a way of asking a question and getting some very timely feedback from other users so we don't have to re-invent the wheel time and again..."

Geography

Where in the world do Tek-Tips members come from?
coolstrike23 (TechnicalUser)
20 Feb 12 7:27
Hi All

I need to use this query as a subquery

select sum("A"."QUANTITY" * "B"."Qty") as Qty
from A  Inner Join B on "B"."ITEMNO" = "A"."ITEMNO" and "A"."BOMNO"="B"."BOMNO" and "A"."STATUS" = 1 group by "B"."Component"

with the complete query returning

"C"."ITEMNO"   Qty     

with "C"."ITEMNO" = "B"."Component"

Appreciate any help with this. Thanks in advance
markros (Programmer)
20 Feb 12 10:27
What is your SQL Server version (assuming you're using SQL Server) and can you post the whole query?

I suggest to use CROSS APPLY in order to bring this information into the query, but that's all I can say based on the very limited information provided.

PluralSight Learning Library

coolstrike23 (TechnicalUser)
20 Feb 12 15:10
Markros, SQL 2008 server.

The whole query is selecting a Item from master item table, and adding subquery to return qty on sales order, PO and committed to production.

In the above instance

Table c
Itemno

Table a
Itemno (master item)
Bomn
Qty

Table b
Itemno (master item)
Bomno
Component (same as c.itemno)
Qty

Component Committed to production = a.qty*b.qty

Query to return ( the so and PO subquery look at one table and so straight forward, but the committed to prod is calc from 2 tables)

C.item.         Committed to production.     On so.       On PO
A.                      0.                                         20.             0
B.                    100.                                        0.              10
markros (Programmer)
20 Feb 12 16:28

CODE

select I.*, coalesce(A.TotalQty,0) * coalesce(B.TotalQty,0) as [Commited to Production]
from Items I
OUTER APPLY (select sum(Qty) as TotalQty from TableA where A.ItemNo = I.ItemNo) A
OUTER APPLY (select sum(Qty) as TotalQty from TableB where B.ItemNo = I.ItemNo) B

Check this blog post
Aggregates with multiple tables
 

PluralSight Learning Library

Helpful Member!  JonFer (Programmer)
20 Feb 12 19:10
It looks like you want the Sum of (A.Qty * B.Qty), not the sum of A.Qty multiplied by the sum of B.Qty.  I would use this SQL with a derived table based on your subquery above:

CODE

Select C.*, D.Qty
From C Left Join
(Select B.Component,
    Sum(IsNull(A.Qty,0) * IsNull(B.Qty,0)) as Qty
 From B Inner Join A on B.ITEMNO = A.ITEMNO  And
      B.BOMNO=A.BOMNO and A.STATUS = 1
Group By B.Component) as D
   On C.ItemNo = D.Component

You may want to change the join types depending on what you want to see if there are no records in B or A for your C.ItemNo values.
 
coolstrike23 (TechnicalUser)
21 Feb 12 1:04
Just what I needed.Many Thanks Jonfer.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close