在MS ACCESS中的同一个表上唯一地共同创建列

时间:2015-07-21 16:23:10

标签: sql ms-access

原始表:

=======================================================================================
|   A   |     B    |       C        |     D     |     E     |      F      |     G     |
=======================================================================================
| 65432  | AFD452E |   sometext1    |   IA1     |   10,6    |    8,5      |   22,0    |
| 65432  | AFD452E |   sometext1    |   IA2     |           |    11,4     |   16,5    |
| 65432  | AFD452E |   sometext1    |   IA3     |           |    3,5      |    5,3    |
| 65989  | 74652E  |   sometext2    |   IA1     |   3,3     |             |     8     |
| 65989  | 74652E  |   sometext2    |   IA3     |   23      |     1,1     |    6,9    |
| 27890  | P8965A  |   sometext3    |   IA3     |   12,7    |     13,8    |   33,2    |
=======================================================================================

结果表应为:

=======================================================================================
|   A   |     B    |       C        |     NEWA1     |     NEWA2     |      NEWA3      |
=======================================================================================
| 65432  | AFD452E |   sometext1    | 10,6-8,5-22,0-| -11,4-16,5-   |                 |
| 65989  | 74652E  |   sometext2    | 3,3--8-       |               |  23-1,1-6,9-    |
| 27890  | P8965A  |   sometext3    |               |               | 12,7-13,8-33,2- |
=======================================================================================

这是我到目前为止所提出的,但它没有做到这一点:

    SELECT DISTINCT [A], [B], [C],
    IIF( [D]  LIKE  "IA1",  IIF(ISNULL([E]), '-', [E] + '-') + IIF(ISNULL([F]), '-', [F] + '-')  + IIF(ISNULL([G]), '-', [G] + '-')   , ‘’) AS NEWIA1,  
    IIF( [D]  LIKE  "IA2",  IIF(ISNULL([E]), '-', [E] + '-') + IIF(ISNULL([F]), '-', [F] + '-')  + IIF(ISNULL([G]), '-', [G] + '-')   , ‘’) AS NEWIA2, 
    IIF( [D]  LIKE  "IA3",  IIF(ISNULL([E]), '-', [E] + '-') + IIF(ISNULL([F]), '-', [F] + '-')  + IIF(ISNULL([G]), '-', [G] + '-')   , ‘’) AS NEWIA3
    FROM TABLE;

这就是我提出的问题:

=======================================================================================
|   A   |     B    |       C        |     NEWA1     |     NEWA2     |      NEWA3      |
=======================================================================================
| 65432  | AFD452E |   sometext1    | 10,6-8,5-22,0-|               |                 |
| 65432  | AFD452E |   sometext1    |               |  -11,4-16,5-  |                 |
| 65432  | AFD452E |   sometext1    |               |               |   -11,4-16,5-   |
| 65989  | 74652E  |   sometext2    | 3,3--8-       |               |                 |
| 65989  | 74652E  |   sometext2    |               |               |   23-1,1-6,9-   |
| 27890  | P8965A  |   sometext3    |               |               | 12,7-13,8-33,2- |
=======================================================================================

1 个答案:

答案 0 :(得分:0)

尝试类似:

select A, B, C,
max(iif([D] like "1A1" .. rest of code)) as newa1,
e ...
from table
group by A, B. c;

设置好行后,可以将它们包装在子查询中并修剪多余的连字符。