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 > Database Server Software > MySQL > Group By Inventory

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-25-09, 09:35
snail1984 snail1984 is offline
Registered User
 
Join Date: Oct 2009
Posts: 6
Group By Inventory

I have a table set up with like this

ActivityID | ActivityDate | ActivityDesc | ProductID | OrderNum | Ordered | Received | Used | SupplierID

I need to make a query so I can subtract the Used from the Received, and group the results by the ProductID so I can see what I have onhand for all products. This is what I have so far. Can someone please help me with this?

Code:
SELECT ( SELECT sum(Received)
           FROM InventoryActivity 
        )
     - ( SELECT sum(Used)
           FROM InventoryActivity
        ) AS Onhand
Reply With Quote
  #2 (permalink)  
Old 10-25-09, 09:52
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Code:
SELECT ProductID 
     , SUM(Received) AS received
     , SUM(Used) AS used
     , SUM(Received) -
       SUM(Used) AS onhand
  FROM InventoryActivity 
GROUP
    BY ProductID
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 10-25-09, 09:53
snail1984 snail1984 is offline
Registered User
 
Join Date: Oct 2009
Posts: 6
That was quick! Thanks for your help!
Reply With Quote
  #4 (permalink)  
Old 10-25-09, 14:04
snail1984 snail1984 is offline
Registered User
 
Join Date: Oct 2009
Posts: 6
I also have a trigger number in a product table:
ID | Name | Category | Trigger

How would the query go after that if I wanted:
Display all ProductIDs and ProductNames if the onhand is <= trigger? So I can tell what I need to order based on this.
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