Question

I need help with a composite query in MS Access 2010.

I've got one table with this structure:

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

where name and category are the primary key of ChoirOrder. The category field can be A, B or Y.

I need to extract data in the following format:

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
);

(every choir must appear in one and only one row, and name must be the primary key).

The problem is that I want to obtain NULL in the cat_x_day, cat_x_h_start, cat_x_h_end and cat_x_h_order fields (where x can be A, B or Y) if in the original ChoirOrder table a choir does not have any rows with that category. For example, if the data in ChoirOrder is:

 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

the result should be:

 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      |

(since the choir C1 does not have a row in ChoirOrder where the category is Y, the fields CAT_Y_DAY, CAT_Y_H_START, CAT_Y_H_END and CAT_Y_ORDER are set to NULL; likewise for the other choirs).

I prepared a two-tables full outer join with the following code; it works by extracting ALL choirs in the A category and ALL choirs that have only a row for the B category (Access 2010 does not support full outer joins, so I use a 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;

However, this query will extract only part of the intended result. I need to combine it or fully rewrite it to include the Y category as well.

I thought I could include

  NULL  AS [cat_y_day], 
  NULL  AS [cat_y_h_start], 
  NULL  AS [cat_y_h_end], 
  NULL  AS [cat_y_order]

in the query, but I cannot make it work. Any ideas?

Was it helpful?

Solution

The following crosstab query seems to give the desired result:

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'
    )

It returns:

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          
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top