![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
#3
| |||
| |||
|
|
Hi, I have 2 tables as follows: CREATE TABLE [PlannedSales] ( [PlanYear] [smallint] , [PlanMonth] [smallint] , [PlanProdCode] [nvarchar] (20) , [PlanQty] [numeric](28, 8) , [PlanAmt] [money] ) ON [PRIMARY] GO CREATE TABLE [ActualSales] ( [SaleYear] [smallint] , [SaleMonth] [smallint] , [SoldProdCode] [nvarchar] (20) , [SaleQty] [numeric](28, 8) , [SaleAmt] [money] ) ON [PRIMARY] GO So basically I have Year,Month,and Product wise totals in the above tables (one table has the Plan while the 2nd table has the Actuals). Now I created a View and linked the Year,Month, and ProdCode fields between 2 tables but as a end result am getting incorrect number of rows. One logical reason could be that a Product might Not be planned (for a given Year or Month combination)but might have been sold in that month. Similarly it is possible that for a given Year and Month combination, a Product might have been planned but might not have been sold for the same year and month combination... I tried also tried the option to return all rows from both sides (which gives FULL OUTER JOIN). I am sure that I am on the wrong track and will appreciate your help and guidance. Many TIA. |
#4
| |||
| |||
|
|
| SELECT | COALESCE(P.planyear,A.saleyear), | COALESCE(P.planmonth,A.salemonth), | COALESCE(P.planprodcode,A.soldprodcode), | P.planqty, P.planamt, A.saleqty, A.saleamt | FROM PlannedSales AS P | FULL JOIN ActualSales AS A | ON P.planyear = A.saleyear | AND P.planmonth = A.salemonth | AND P.planprodcode = A.soldprodcode |
#5
| |||
| |||
|
|
IS " a reserved word/function??? This is the first time I'm coming across this |
#6
| |||
| |||
|
|
| | > IS " a reserved word/function??? This is the first time I'm coming | > across this | | | I am not sure what you mean by this... I guess you have a typo? The queries | David and I posted are essentially the same... |
|
| SELECT | COALESCE(P.planyear,A.saleyear), | COALESCE(P.planmonth,A.salemonth), | COALESCE(P.planprodcode,A.soldprodcode), | P.planqty, P.planamt, A.saleqty, A.saleamt | FROM PlannedSales AS P | FULL JOIN ActualSales AS A | ON P.planyear = A.saleyear | AND P.planmonth = A.salemonth | AND P.planprodcode = A.soldprodcode |
#7
| |||
| |||
|
#8
| |||
| |||
|
![]() |
| Thread Tools | |
| Display Modes | |
| |