I am trying to create a query where the results will be displayed like:
Product Ordered Sold Stock Remaining
Trader A 1 5 3 2
Trader B 1 4 1 3
At the moment the information is contained in tables:
Sales Table which contains the TraderID field. this is linked to the salesdetails table which contains all the information, product sold, quantity, amount and so on.
Then there is an orders table, which has the traderID and linked to the orders details table which contains information relating to product, quantity and cost price.
At the moment I have two queries, which are:
For Orders
And for Sales
These queries display the correct information in the correct format:
TraderID Product Quantity(Sum)
But I cant get the two queries merged together where the information can be displayed like my table example above
Product Ordered Sold Stock Remaining
Trader A 1 5 3 2
Trader B 1 4 1 3
At the moment the information is contained in tables:
Sales Table which contains the TraderID field. this is linked to the salesdetails table which contains all the information, product sold, quantity, amount and so on.
Then there is an orders table, which has the traderID and linked to the orders details table which contains information relating to product, quantity and cost price.
At the moment I have two queries, which are:
For Orders
Code:
SELECT dbo.Orders.TraderID, dbo.OrderDetails.Product, SUM(dbo.OrderDetails.Quantity) AS TotalOrdered
FROM dbo.OrderDetails INNER JOIN
dbo.Orders ON dbo.OrderDetails.OrderID = dbo.Orders.OrderID
GROUP BY dbo.Orders.TraderID, dbo.OrderDetails.Product
And for Sales
Code:
SELECT dbo.Sales.TraderID, dbo.SalesDetails.ProductID, SUM(dbo.SalesDetails.Quantity) AS TotalSold
FROM dbo.Sales LEFT OUTER JOIN
dbo.SalesDetails ON dbo.Sales.SalesID = dbo.SalesDetails.SalesID
GROUP BY dbo.Sales.TraderID, dbo.SalesDetails.ProductID
These queries display the correct information in the correct format:
TraderID Product Quantity(Sum)
But I cant get the two queries merged together where the information can be displayed like my table example above