Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to write join statement for this

Author  Topic 

anchoredwisdom
Starting Member

22 Posts

Posted - 2012-11-09 : 11:58:51
Hi Gurus,

I have a table with four columns.
ProductID, Year, Product , amount.

only 4 years , 4 products , 4 Ids , 4 amounts
say years 2009,2010,2011,2012,
Products A,B,C,D
ProductID,1,2,3,4 AND respective amounts,
now i just want sum (amounts ) group by these three attributes
But for 2010 Product C did not do any business hence there is no record for year 2010 against C id 3

I have display null against that missing year,id,product



( I tried to put my scenario in a generic way.)


Best Regards
S

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2012-11-09 : 15:05:29
>>( I tried to put my scenario in a generic way.)
Then here's a generic answer:

You need a table of all your groupings (product,year). say ProdYear_tbl
Then LEFT OUTER JOIN to your data table correlating on the grouping columns. SELECT the grouping values from the PRodYear table and sum the amount from your data table. That way you have a row for all possibilities even if the amount is null.

Be One with the Optimizer
TG
Go to Top of Page
   

- Advertisement -