Rattlesnake
Well-known member
- Joined
- Dec 23, 2003
- Messages
- 47
Hi,
I am using SQL Server 2000. I have a database that store the SalesForecast and the SalesORders.
I have the following tables
SalesForecast
-------------
ForecastMonth
ForecastYear
CustomerID
ProductID
ForecastQuantity
SalesOrder
---------
OrderMonth
OrderYear
CustomerID
ProductID
OrderQuantity
I want to create a query that will give me the following information
CustomerID--Month--Year--ProductID--ForecastQuantity--OrderQuantity
i.e a query that will match the Forecast that was for a specific Product and the actual sales ORders that were received for that Product.
What I have done is created a Groupby query (vwTotalOrders) that Sums the Orders for a Product and another Query (vwTotalForecast) that sums the Forecast for that product
vwTotalForecast
---------------
Select CustomerID,ProductID,ForecastMonth,ForecastYear,Sum(ForecastQuantity) as TotalFC
From SalesForecast
GROUPBY CustomerID,ProductID,ForecastMonth,ForecastYear
vwTotalOrders
---------------
Select CustomerID,ProductID,OrderMonth,OrderYear,Sum(OrderQuantity) as TotalOrders
From SalesORder
GROUPBY CustomerID,ProductID,OrderMonth,OrderYear
Now I want to create a join query between these 2 queries that will give me the belwo record
CustomerID--Month--Year--ProductID--ForecastQuantity--OrderQuantity
I have created this query
SELECT vwTotalForecast.CustomerID, vwTotalForecast ,ProductID, vwTotalForecast.ForecastMonth, vwTotalForecast.ForecastYear, vwTotalForecast.TotalFC, vwTotalOrders.TotalOrders, vwTotalOrders.OrderYear, vwTotalOrders.OrderMonth
FROM vwTotalForecast INNER JOIN
vwTotalOrders ON vwTotalForecast.CustomerID = vwTotalOrders.CustomerID AND vwTotalForecast.ForecastYear = vwTotalOrders.OrderYear AND
vwFC.ForecastMonth = vwTotalOrders.OrderMonth AND vwTotalForecast.ProductID = vwTotalOrders.ProductID
But I have one issue with this, there might be some Products that MIGHT NOT be FORECASTED but will have Sales ORders for a specific month. The reverse can be also True i.e. there might be a forecast for a Product but no Orders are placed for that product.
How can I make the query return all the Forecast records even if they donot have a Sales ORder and all Sales ORders even if they donot have Forecast
I am using SQL Server 2000. I have a database that store the SalesForecast and the SalesORders.
I have the following tables
SalesForecast
-------------
ForecastMonth
ForecastYear
CustomerID
ProductID
ForecastQuantity
SalesOrder
---------
OrderMonth
OrderYear
CustomerID
ProductID
OrderQuantity
I want to create a query that will give me the following information
CustomerID--Month--Year--ProductID--ForecastQuantity--OrderQuantity
i.e a query that will match the Forecast that was for a specific Product and the actual sales ORders that were received for that Product.
What I have done is created a Groupby query (vwTotalOrders) that Sums the Orders for a Product and another Query (vwTotalForecast) that sums the Forecast for that product
vwTotalForecast
---------------
Select CustomerID,ProductID,ForecastMonth,ForecastYear,Sum(ForecastQuantity) as TotalFC
From SalesForecast
GROUPBY CustomerID,ProductID,ForecastMonth,ForecastYear
vwTotalOrders
---------------
Select CustomerID,ProductID,OrderMonth,OrderYear,Sum(OrderQuantity) as TotalOrders
From SalesORder
GROUPBY CustomerID,ProductID,OrderMonth,OrderYear
Now I want to create a join query between these 2 queries that will give me the belwo record
CustomerID--Month--Year--ProductID--ForecastQuantity--OrderQuantity
I have created this query
SELECT vwTotalForecast.CustomerID, vwTotalForecast ,ProductID, vwTotalForecast.ForecastMonth, vwTotalForecast.ForecastYear, vwTotalForecast.TotalFC, vwTotalOrders.TotalOrders, vwTotalOrders.OrderYear, vwTotalOrders.OrderMonth
FROM vwTotalForecast INNER JOIN
vwTotalOrders ON vwTotalForecast.CustomerID = vwTotalOrders.CustomerID AND vwTotalForecast.ForecastYear = vwTotalOrders.OrderYear AND
vwFC.ForecastMonth = vwTotalOrders.OrderMonth AND vwTotalForecast.ProductID = vwTotalOrders.ProductID
But I have one issue with this, there might be some Products that MIGHT NOT be FORECASTED but will have Sales ORders for a specific month. The reverse can be also True i.e. there might be a forecast for a Product but no Orders are placed for that product.
How can I make the query return all the Forecast records even if they donot have a Sales ORder and all Sales ORders even if they donot have Forecast