If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Access > Join tables with additional Sum Query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-16-12, 18:33
ktab ktab is offline
Registered User
 
Join Date: Jan 2012
Posts: 2
Join tables with additional Sum Query

Hello all
I have the following SQL statement to fill a datagrid from a mdb:

Code:
SELECT        modProducts.Product_Name, SysProductCategories.Product_Category, SysProductSubCategories.Product_SubCategory, modProducts.ProductDescription, 
                         modContacts.Company_Name, modProducts.VendorCode, modProducts.ProductPrice, modProducts.ProductCost, modProducts.ProductProfit, modProducts.StockLimit,
                          SysMetrics.MetricsValue
FROM            ((((modProducts LEFT OUTER JOIN
                         modContacts ON modProducts.ProductVendor = modContacts.RecordID) LEFT OUTER JOIN
                         SysProductSubCategories ON modProducts.ProductSubCategory = SysProductSubCategories.RecordID) LEFT OUTER JOIN
                         SysProductCategories ON modProducts.ProductCategory = SysProductCategories.RecordID) LEFT OUTER JOIN
                         SysMetrics ON modProducts.MetricUnit = SysMetrics.RecordID)
ORDER BY modProducts.Product_Name
To get the sum column I'm searching for, I have to use this SQL:
Code:
SELECT        SUM(modTransaction_Items.Quantity) AS TotalSales
FROM            (modProducts LEFT OUTER JOIN
                         modTransaction_Items ON modProducts.RecordID = modTransaction_Items.Product_ID)
HAVING        (modTransaction_Items.InvoiceType = 1)
Another sum is this:
Code:
SELECT        SUM(modTransaction_Items.Quantity) AS TotalBuys
FROM            (modProducts LEFT OUTER JOIN
                         modTransaction_Items ON modProducts.RecordID = modTransaction_Items.Product_ID)
HAVING        (modTransaction_Items.InvoiceType = 2)
and finally I would like to have the difference between the 2 sums: totalbuys-totalsales

How can all of these have place in one and only statement? How can I fix the first SQL query to include the rest 3 sums?

Thank you.

Last edited by ktab; 01-17-12 at 09:08.
Reply With Quote
  #2 (permalink)  
Old 01-17-12, 15:20
ktab ktab is offline
Registered User
 
Join Date: Jan 2012
Posts: 2
any ideas?
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On