如何将字段汇总到一个字段中?

时间:2014-01-07 05:33:37

标签: ms-access

我是新手,有一个简单的问题。 在Microsoft Access中,我有几个正面或负面答案的字段。 我可以将这些字段汇总到一个字段中,该字段包含查询中具有肯定答案的字段吗?

实施例

Field1 : positive
Field2 : negative
Field3 : negative
Field4 : positive

Newfield : Field1, Field4

1 个答案:

答案 0 :(得分:1)

这是以下查询的输出,idField1Field4存储在名为 YourTable 的表格中。

id Field1   Field2   Field3   Field4   NewField
-- -------- -------- -------- -------- --------------
 1 positive negative negative positive Field1, Field4
 2 negative positive positive negative Field2, Field3
 3 negative negative negative positive Field4
 4 positive negative negative negative Field1
SELECT
    sub.id,
    sub.Field1,
    sub.Field2,
    sub.Field3,
    sub.Field4,
    IIf
        (
            Right(sub.raw_string,2)=', ',
            Left(sub.raw_string, Len(sub.raw_string) -2),
            sub.raw_string
        ) AS NewField
FROM
    (
        SELECT
            y.id,
            y.Field1,
            y.Field2,
            y.Field3,
            y.Field4,
              (IIf(y.Field1='positive','Field1',Null) + ', ')
            & (IIf(y.Field2='positive','Field2',Null) + ', ')
            & (IIf(y.Field3='positive','Field3',Null) + ', ')
            &  IIf(y.Field4='positive','Field4',Null)
                AS raw_string
        FROM YourTable AS y
    ) AS sub;