2014-03-04 78 views
4

我需要在MS Access的複合查詢2010年SQL Access 2010中 - 全外連接三個表

我有一個表,這個結構幫助:

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

其中namecategoryChoirOrder的主要關鍵。 category字段可以是A,BY

我需要以下列格式來提取數據:

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必須是主密鑰)。

的問題是,我想獲得在cat_x_daycat_x_h_startcat_x_h_endcat_x_h_order領域NULL(其中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一排,其中類別是Y,字段CAT_Y_DAYCAT_Y_H_START,CAT_Y_H_ENDCAT_Y_ORDER設置爲NULL;同樣對於其他合唱團)。

我用下面的代碼準備了一個兩表全外連接;它的工作原理是在A類別提取出所有的合唱團和有隻爲B類別行的所有合唱團(Access 2010中不支持全外連接,所以我用一個LEFT JOIN 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 
    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] 
查詢

,但我不能使它發揮作用。有任何想法嗎?

回答

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