3个表上的完全外连接

时间:2015-11-25 06:36:25

标签: sql-server sql-server-2008 full-outer-join

我试图从3个表中获取所有组合的完整结果集,但它不起作用,因为我无法弄清楚如何告诉SQL-Server 2008我想要什么。

我已将其简化为以下类似问题...我有3个表:

table `date_ranges`:
  --------------------------------------------------------
| start_day                  | end_day                     |
| -------------------------------------------------------- |
| November, 01 2015 14:37:00 | November, 02 2015 00:00:00  |
| November, 02 2015 00:00:00 | November, 03 2015 00:00:00  |
| November, 03 2015 00:00:00 | November, 04 2015 00:00:00  |
| November, 04 2015 00:00:00 | November, 04 2015 02:00:00  |
  --------------------------------------------------------

table `sites`:
  ----
| site |
| ---- |
| 1    |
| 2    |
| 3    |
| 4    |
  ---- 

table `all_data`:
  --------------------------------------
| data_date                  | data_site |
| ---------------------------|---------- |
| November, 02 2015 15:35:00 | 1         |
  --------------------------------------

我希望得到以下结果:

  -------------------------------------------------------------------------------------------------------
| data_date                  | data_site | start_day                  | end_day                    | site |
| ------------------------------------------------------------------------------------------------------- |
| null                       | null      | November, 01 2015 14:37:00 | November, 02 2015 00:00:00 | 1    |
| November, 02 2015 15:35:00 | 1         | November, 02 2015 00:00:00 | November, 03 2015 00:00:00 | 1    |
| null                       | null      | November, 03 2015 00:00:00 | November, 04 2015 00:00:00 | 1    |
| null                       | null      | November, 04 2015 00:00:00 | November, 04 2015 02:00:00 | 1    |
| null                       | null      | November, 01 2015 14:37:00 | November, 02 2015 00:00:00 | 2    |
| null                       | null      | November, 02 2015 00:00:00 | November, 03 2015 00:00:00 | 2    |
| null                       | null      | November, 03 2015 00:00:00 | November, 04 2015 00:00:00 | 2    |
| null                       | null      | November, 04 2015 00:00:00 | November, 04 2015 02:00:00 | 2    |
| null                       | null      | November, 01 2015 14:37:00 | November, 02 2015 00:00:00 | 3    |
| null                       | null      | November, 02 2015 00:00:00 | November, 03 2015 00:00:00 | 3    |
| null                       | null      | November, 03 2015 00:00:00 | November, 04 2015 00:00:00 | 3    |
| null                       | null      | November, 04 2015 00:00:00 | November, 04 2015 02:00:00 | 3    |
| null                       | null      | November, 01 2015 14:37:00 | November, 02 2015 00:00:00 | 4    |
| null                       | null      | November, 02 2015 00:00:00 | November, 03 2015 00:00:00 | 4    |
| null                       | null      | November, 03 2015 00:00:00 | November, 04 2015 00:00:00 | 4    |
| null                       | null      | November, 04 2015 00:00:00 | November, 04 2015 02:00:00 | 4    |
  ------------------------------------------------------------------------------------------------------- 

但我只能弄清楚如何获得以下结果(see fiddle here):

  -------------------------------------------------------------------------------------------------------
| data_date                  | data_site | start_day                  | end_day                    | site |
| ------------------------------------------------------------------------------------------------------- |
| null                       | null      | November, 01 2015 14:37:00 | November, 02 2015 00:00:00 | null |
| November, 02 2015 15:35:00 | 1         | November, 02 2015 00:00:00 | November, 03 2015 00:00:00 | 1    |
| null                       | null      | November, 03 2015 00:00:00 | November, 04 2015 00:00:00 | null |
| null                       | null      | November, 04 2015 00:00:00 | November, 04 2015 02:00:00 | null |
| null                       | null      | null                       | null                       | 2    |
| null                       | null      | null                       | null                       | 3    |
| null                       | null      | null                       | null                       | 4    |
  ------------------------------------------------------------------------------------------------------- 

使用以下错误查询:

select * from all_data d
full outer join date_ranges r on (r.start_day <= d.data_date and d.data_date < r.end_day)
full outer join sites s on s.site = d.data_site

1 个答案:

答案 0 :(得分:5)

CROSS JOINLEFT JOIN的组合可以帮到你。

像这样......

See Working link

SELECT * 
FROM DATE_RANGES R
    CROSS JOIN SITES S 
    LEFT OUTER JOIN ALL_DATA D ON (R.START_DAY <= D.DATA_DATE AND D.DATA_DATE < R.END_DAY)
                                        AND S.SITE = D.DATA_SITE