我需要有关 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 可以是 xAB )中获取 Y 如果在原始 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      |

(由于合唱团 C1ChoirOrder 中没有类别为 Y 的行,因此字段 CAT_Y_DAYCAT_Y_H_STARTCAT_Y_H_ENDCAT_Y_ORDER 设置为 NULL ;其他合唱团也是如此)。

我用以下代码准备了一个两表全外连接;它的工作原理是提取 A 类别中的所有合唱团和 B 类别中只有一行的所有合唱团(Access 2010 不支持完全外连接,所以我使用 LEFT join UNION RIGHT join):
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;

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

我以为我可以包括
  NULL  AS [cat_y_day],
  NULL  AS [cat_y_h_start],
  NULL  AS [cat_y_h_end],
  NULL  AS [cat_y_order]

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

最佳答案

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

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

关于SQL Access 2010 - 三个表上的完全外连接,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22169605/

10-10 19:38