SQL Access 2010 - 三个表上的完全外连接

时间:2014-03-04 10:40:28

标签: sql ms-access-2010 multiple-tables full-outer-join

我需要MS Access 2010中的复合查询帮助。

我有一个这种结构的表:

ChoirOrder(name, category, day, h_start, h_end, ord);

其中namecategoryChoirOrder的主键。 category字段可以是ABY

我需要以下列格式提取数据:

FullChoirExecution (
  name, 
  cat_a_day, 
  cat_a_h_start, 
  cat_a_h_end, 
  cat_a_order,
  cat_b_day, 
  cat_b_h_start, 
  cat_b_h_end, 
  cat_b_order,
  cat_y_day, 
  cat_y_h_start, 
  cat_y_h_end, 
  cat_y_order
);

(每个合唱团必须出现在一行且只有一行,而name必须是主键。)

问题是,我想在NULLcat_x_daycat_x_h_startcat_x_h_end字段中获取cat_x_h_order(其中x可以是ABY)如果在原始ChoirOrder表格中,合唱团没有任何具有该类别的行。例如,如果ChoirOrder中的数据是:

 NAME | CAT |    DAY     | H_START | H_END | ORD 
  C1  |  A  | 04/03/2014 |  09:00  | 10:00 |  1
  C2  |  A  | 04/03/2014 |  10:00  | 11:00 |  2
  C3  |  A  | 04/03/2014 |  11:00  | 12:00 |  3
  C4  |  A  | 04/03/2014 |  12:00  | 13:00 |  4
  C1  |  B  | 05/03/2014 |  14:00  | 15:00 |  1
  C2  |  B  | 05/03/2014 |  15:00  | 16:00 |  2
  C5  |  B  | 05/03/2014 |  16:00  | 17:00 |  3
  C3  |  Y  | 06/03/2014 |  09:00  | 10:00 |  1
  C5  |  Y  | 06/03/2014 |  10:00  | 11:00 |  2
  C6  |  Y  | 06/03/2014 |  11:00  | 12:00 |  3
  C4  |  Y  | 06/03/2014 |  12:00  | 13:00 |  4

结果应该是:

 NAME | CAT_A_DAY  | CAT_A_H_START | CAT_A_H_END | CAT_A_ORDER | CAT_B_DAY  | CAT_B_H_START | CAT_B_H_END | CAT_B_ORDER | CAT_Y_DAY  | CAT_Y_H_START | CAT_Y_H_END | CAT_Y_ORDER |
  C1  | 04/03/2014 |     09:00     |    10:00    |      1      | 05/03/2014 |     14:00     |    15:00    |      1      |            |               |             |             |
  C2  | 04/03/2014 |     10:00     |    11:00    |      2      | 05/03/2014 |     15:00     |    16:00    |      2      |            |               |             |             |
  C3  | 04/03/2014 |     11:00     |    12:00    |      3      |            |               |             |             | 06/03/2014 |     09:00     |    10:00    |      1      |
  C4  | 04/03/2014 |     12:00     |    13:00    |      4      |            |               |             |             | 06/03/2014 |     12:00     |    13:00    |      4      |
  C5  |            |               |             |             | 05/03/2014 |     16:00     |    17:00    |      3      | 06/03/2014 |     10:00     |    11:00    |      2      |
  C6  |            |               |             |             |            |               |             |             | 06/03/2014 |     11:00     |    12:00    |      3      |

(由于合唱团C1在类别为ChoirOrder的{​​{1}}中没有行,因此字段为YCAT_Y_DAY,{{1 }}和CAT_Y_H_START设置为CAT_Y_H_END;同样适用于其他合唱团。)

我用以下代码准备了一个两表全外连接;它的工作原理是提取CAT_Y_ORDER类别中的所有合唱团以及只有NULL类别的行的所有合唱团(Access 2010不支持完整的外部联接,因此我使用LEFT加入UNION RIGHT联接) :

A

但是,此查询将仅提取预期结果的一部分。我需要将它组合起来或完全重写它以包含B类别。

我以为我可以加入

SELECT 
  B.[name]      AS [name], 
  A.[day]       AS [cat_a_day], 
  A.[h_start]   AS [cat_a_h_start], 
  A.[h_end]     AS [cat_a_h_end], 
  A.[ord]       AS [cat_a_order],
  B.[day]       AS [cat_b_day], 
  B.[h_start]   AS [cat_b_h_start], 
  B.[h_end]     AS [cat_b_h_end], 
  B.[ord]       AS [cat_b_order]

FROM 
  [ChoirOrder] AS A
  LEFT OUTER JOIN 
    [ChoirOrder] AS B 
  ON 
        (A.[name] = B.[name])
    AND (A.[category]='A' AND B.[category]='B')

UNION 

SELECT 
  B.[name]      AS [name], 
  A.[day]       AS [cat_a_day], 
  A.[h_start]   AS [cat_a_h_start], 
  A.[h_end]     AS [cat_a_h_end], 
  A.[ord]       AS [cat_a_order],
  B.[day]       AS [cat_b_day], 
  B.[h_start]   AS [cat_b_h_start], 
  B.[h_end]     AS [cat_b_h_end], 
  B.[ord]       AS [cat_b_order]

FROM 
  [ChoirOrder] AS A
  RIGHT OUTER JOIN 
    [ChoirOrder] AS B 
  ON 
        (A.[name] = B.[name])
    AND (A.[category]='A' AND B.[category]='B')

WHERE A.[category] IS NULL;

在查询中,但我无法使其工作。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

以下交叉表查询似乎给出了所需的结果:

TRANSFORM First(field_value) AS v
SELECT [name]
FROM
    (
        SELECT
            [name],
            'cat_' & LCase([cat]) & '_day' AS field_name,
            [day] AS field_value
        FROM ChoirOrder
    UNION ALL
        SELECT
            [name],
            'cat_' & LCase([cat]) & '_h_start' AS field_name,
            [h_start] AS field_value
        FROM ChoirOrder
    UNION ALL
        SELECT
            [name],
            'cat_' & LCase([cat]) & '_h_end' AS field_name,
            [h_end] AS field_value
        FROM ChoirOrder
    UNION ALL
        SELECT
            [name],
            'cat_' & LCase([cat]) & '_order' AS field_name,
            [ord] AS field_value
        FROM ChoirOrder
    ) AS u
GROUP BY [name]
PIVOT field_name IN
    (
        'cat_a_day', 'cat_a_h_start', 'cat_a_h_end', 'cat_a_order',
        'cat_b_day', 'cat_b_h_start', 'cat_b_h_end', 'cat_b_order',
        'cat_y_day', 'cat_y_h_start', 'cat_y_h_end', 'cat_y_order'
    )

它返回:

name  cat_a_day   cat_a_h_start  cat_a_h_end  cat_a_order  cat_b_day   cat_b_h_start  cat_b_h_end  cat_b_order  cat_y_day   cat_y_h_start  cat_y_h_end  cat_y_order
----  ----------  -------------  -----------  -----------  ----------  -------------  -----------  -----------  ----------  -------------  -----------  -----------
C1    2014-03-04  09:00          10:00        1            2014-03-05  14:00          15:00        1                                                               
C2    2014-03-04  10:00          11:00        2            2014-03-05  15:00          16:00        2                                                               
C3    2014-03-04  11:00          12:00        3                                                                 2014-03-06  09:00          10:00        1          
C4    2014-03-04  12:00          13:00        4                                                                 2014-03-06  12:00          13:00        4          
C5                                                         2014-03-05  16:00          17:00        3            2014-03-06  10:00          11:00        2          
C6                                                                                                              2014-03-06  11:00          12:00        3