如何使用日期和周数连接两个表?

时间:2017-11-03 18:54:32

标签: sql sql-server select join cross-join

如何连接这两个表以根据部门名称和周数连接数据。因此,如果周数不匹配,则显示其他人的空值。

http://sqlfiddle.com/#!6/3bbd3/1

1 个答案:

答案 0 :(得分:1)

完整的外连接应该可以解决问题:

SELECT          d.id, 
                d.name, 
                f.id, 
                DATEPART(WEEK, setupdate) as WeekNumber  
FROM            departments d
FULL OUTER JOIN forecast f ON d.name = f.name AND
                DATEPART(WEEK, setupdate) = DATEPART(WEEK, forecast)