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
 Help in query, pls help

Author  Topic 

sqlserverlearner
Starting Member

21 Posts

Posted - 2009-01-13 : 11:00:44
I have 2 tables which need to be right joined to fill in extra rows based on another table. Pls anyone help.

tabA
Monthid LocId Hrs
1 10 12
2 10 15
2 12 10
3 12 9

tabB
Monthid Name
1 Jan
2 Feb
3 Mar
4 Apr
5 May


Result

10 Jan 12
10 Feb 15
10 Mar 0 inserted
10 Apr 0 inserted
10 May 0 inserted
12 Jan 0 inserted
12 Feb 10
12 Mar 9
12 Apr 0 inserted
12 May 0 inserted

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-13 : 11:20:16
[code]
SELECT m.LocId,m.MonthName,COALESCE(a.Hrs,0)
FROM
(
SELECT t.LocId,b.Monthid,b.MonthName
FROM
(
select distinct LocId FROM tabA
)t
CROSS JOIN tabB b
)m
LEFT JOIN tabA a
ON a.Monthid=m.Monthid
AND a.LocId=m.LocId
[/code]
Go to Top of Page

sqlserverlearner
Starting Member

21 Posts

Posted - 2009-01-13 : 12:38:02
Thank You very much. I modified as per my requirement and got exactly what I wanted. I appreciate all the help.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-13 : 12:47:17
welcome
Go to Top of Page
   

- Advertisement -