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
 Join Help

Author  Topic 

cableca
Starting Member

6 Posts

Posted - 2008-03-04 : 14:17:01
I have 2 tables. table A has all the information I need. table b has all the months I want to show. How can I join the tables on the month field and have all the months from table b show even if the months are not in table A? I have my query below.

SELECT b.f_month,disc_check_in_month,region,product_group,count(product_group) as cnt
FROM collections.v_nonpays a
left join collections.filler_months b
on (a.disc_check_in_month=b.f_month)
where disc_check_in_month >='2007-01-01'

Thanks!!

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-03-04 : 14:20:03
SELECT b.f_month,
a.disc_check_in_month,
a.region,
a.product_group,
count(a.product_group) as cnt
FROM collections.filler_months as b
left join collections.v_nonpays as a on a.disc_check_in_month = b.f_month
and a.disc_check_in_month >= '2007-01-01'
group by b.f_month,
a.disc_check_in_month,
a.region,
a.product_group


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

cableca
Starting Member

6 Posts

Posted - 2008-03-04 : 14:49:12
Thanks for your quick response. I tried the query you gave me but now the counts in the 'cnt' column are much higher. any thoughts as to why the counts are higher?
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-03-04 : 15:58:30
Can't tell without knowing table layouts.
Also you need to post proper sample data and expected output.
BTW read this, it has everything you need to know about posting proper questions...

http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page
   

- Advertisement -