dbTalk Databases Forums  

Pleasehelp - Having problem getting correct results linking tables (JOIN related)

microsoft.public.sqlserver.olap microsoft.public.sqlserver.olap


Discuss Pleasehelp - Having problem getting correct results linking tables (JOIN related) in the microsoft.public.sqlserver.olap forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Learner
 
Posts: n/a

Default Pleasehelp - Having problem getting correct results linking tables (JOIN related) - 05-15-2004 , 02:34 PM






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.

Reply With Quote
  #2  
Old   
David Portas
 
Posts: n/a

Default Re: Pleasehelp - Having problem getting correct results linking tables (JOIN related) - 05-15-2004 , 02:47 PM






Looks like FULL JOIN is the way to go. Try this:

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

If that's not what you want then it would help if you could post a few rows
of example data and show us your required result.

--
David Portas
SQL Server MVP
--



Reply With Quote
  #3  
Old   
John Bell
 
Posts: n/a

Default Re: Pleasehelp - Having problem getting correct results linking tables (JOIN related) - 05-15-2004 , 03:06 PM



Hi

Thanks for the DDL it is also useful to post example data as Insert
statements e.g

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

INSERT INTO [PlannedSales] (
[PlanYear], [PlanMonth], [PlanProdCode], [PlanQty], [PlanAmt])
VALUES ( 2004, 1, 'Product A', 1, 100 )
GO

INSERT INTO [PlannedSales] (
[PlanYear], [PlanMonth], [PlanProdCode], [PlanQty], [PlanAmt])
VALUES ( 2004, 2, 'Product A', 2, 200 )
GO


INSERT INTO [PlannedSales] (
[PlanYear], [PlanMonth], [PlanProdCode], [PlanQty], [PlanAmt])
VALUES ( 2004, 2, 'Product B', 6, 50 )
GO

INSERT INTO [PlannedSales] (
[PlanYear], [PlanMonth], [PlanProdCode], [PlanQty], [PlanAmt])
VALUES ( 2004, 3, 'Product A', 3, 300 )
GO

INSERT INTO [PlannedSales] (
[PlanYear], [PlanMonth], [PlanProdCode], [PlanQty], [PlanAmt])
VALUES ( 2004, 3, 'Product B', 2, 150 )
GO

CREATE TABLE [ActualSales] (
[SaleYear] [smallint] ,
[SaleMonth] [smallint] ,
[SoldProdCode] [nvarchar] (20) ,
[SaleQty] [numeric](28, 8) ,
[SaleAmt] [money]
) ON [PRIMARY]
GO

INSERT INTO [ActualSales] (
[SaleYear], [SaleMonth], [SoldProdCode] , [SaleQty], [SaleAmt] )
VALUES ( 2004, 1, 'Product A', 2, 200 )
GO

INSERT INTO [ActualSales] (
[SaleYear], [SaleMonth], [SoldProdCode] , [SaleQty], [SaleAmt] )
VALUES ( 2004, 3, 'Product A', 3, 300 )
GO


INSERT INTO [ActualSales] (
[SaleYear], [SaleMonth], [SoldProdCode] , [SaleQty], [SaleAmt] )
VALUES ( 2004, 2, 'Product B', 8, 100 )
GO

INSERT INTO [ActualSales] (
[SaleYear], [SaleMonth], [SoldProdCode] , [SaleQty], [SaleAmt] )
VALUES ( 2004, 4, 'Product A', 4, 800 )
GO

INSERT INTO [ActualSales] (
[SaleYear], [SaleMonth], [SoldProdCode] , [SaleQty], [SaleAmt] )
VALUES ( 2004, 3, 'Product B', 2, 150 )
GO

SELECT * FROM [PlannedSales]
SELECT * FROM [ActualSales]

Although there are 5 rows in each table 4 match on Year, Month and Product
code therefore with a query like:

SELECT
ISNULL(P.PlanYear,A.[SaleYear]) AS Year,
ISNULL(P.PlanMonth,A.[SaleMonth]) AS Month,
ISNULL(P.PlanProdCode,A.[SoldProdCode]) AS ProductCode,
ISNULL(P.[PlanQty],0) AS PlanQty,
ISNULL(P.[PlanAmt],0) AS PlanAmt,
ISNULL(A.[SaleQty],0) AS SaleQty,
ISNULL(A.[SaleAmt],0) AS SaleAmt
FROM [PlannedSales] p
FULL OUTER JOIN [ActualSales] a ON
A.[SaleYear] = P.[PlanYear]
AND A.[SaleMonth] = P.[PlanMonth]
AND A.[SoldProdCode] = P.[PlanProdCode]
ORDER BY
ISNULL(P.PlanYear,A.[SaleYear]),
ISNULL(P.PlanMonth,A.[SaleMonth]),
ISNULL(P.PlanProdCode,A.[SoldProdCode])

Returns 6 rows.

Year Month ProductCode PlanQty PlanAmt
SaleQty SaleAmt
------ ------ -------------------- ------------------------------ ----------
----------- ------------------------------ ---------------------
2004 1 Product A 1.00000000 100.0000
2.00000000 200.0000
2004 2 Product A 2.00000000 200.0000
..00000000 .0000
2004 2 Product B 6.00000000 50.0000
8.00000000 100.0000
2004 3 Product A 3.00000000 300.0000
3.00000000 300.0000
2004 3 Product B 2.00000000 150.0000
2.00000000 150.0000
2004 4 Product A .00000000 .0000
4.00000000 800.0000

(6 row(s) affected)

John


"Learner" <wantnospam (AT) email (DOT) com> wrote

Quote:
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.



Reply With Quote
  #4  
Old   
Learner
 
Posts: n/a

Default Re: Pleasehelp - Having problem getting correct results linking tables (JOIN related) - 05-15-2004 , 04:14 PM



Hi David and John,

Thanks for the help. Will give it a try and if still problems then I
will post the insert statements too.

Till then a question to David:

Quote:
| 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

IS " a reserved word/function??? This is the first time I'm coming
across this

--
Thanks.


Reply With Quote
  #5  
Old   
John Bell
 
Posts: n/a

Default Re: Pleasehelp - Having problem getting correct results linking tables (JOIN related) - 05-15-2004 , 04:23 PM




Quote:
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...


John




Reply With Quote
  #6  
Old   
Learner
 
Posts: n/a

Default Re: Pleasehelp - Having problem getting correct results linking tables (JOIN related) - 05-16-2004 , 01:24 PM



Quote:
|
| > 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...

Hi John,

It was a typo. Sorry.

Quote:
| 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

My re-phrased question:
Is 'COALESCE' a reserved word? Have never come across this before.

Thanks.


Reply With Quote
  #7  
Old   
Learner
 
Posts: n/a

Default Re: Pleasehelp - Having problem getting correct results linking tables (JOIN related) - 05-16-2004 , 01:24 PM



Hi David and John,

Many thanks...got it to work and getting the results. However I have to
say that this was one nasty join that I ran into


Many thanks for your help.

Regards.

Reply With Quote
  #8  
Old   
David Portas
 
Posts: n/a

Default Re: Pleasehelp - Having problem getting correct results linking tables (JOIN related) - 05-16-2004 , 02:46 PM



Yes. COALESCE is a Standard SQL function. It returns the first non-NULL
value among its arguments.

John used ISNULL in his query. ISNULL is a Microsoft proprietary function
which when used with two arguments of the same datatype gives the same
result as COALESCE.

--
David Portas
SQL Server MVP
--



Reply With Quote
Reply




Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.