MS Access等效于T-SQL中的FOR XML PATH以列出相关值

时间:2014-01-27 12:20:02

标签: sql ms-access

我在访问中有如下表:

MatterNO    Description     Amount
FL069509    SMS             R 50.00
FL069509    Call            R 52.00
FL069509    Summons         R 52.00
FL069509    Email           R 5 420.00
FL069509    Letter          R 0.12
NA076353"   SMs             R 123.10
NA076353"   call            R 0.53
NA076353"   summons         R 53.00
NA076353"   email           R 453.00
NA076353"   letter          R 4 530.00
NA076359    Call            R 50.00

我想对MatterNO进行分组并总结Amounts,但是在单独的列id旁边的数量就像显示构成该数量的所有描述的列表一样。

例如:

MatterNO    Amount    CSV
FL069509    R5574.12  SMS, Call, Summons, Email, Letter

我在SQL中知道一个名为FOR XML PATH

的函数

如下所示:

SELECT t.MatterNo,SUM(Amount), STUFF(
(SELECT ',' + s.Descriptions
FROM Table1 s
WHERE s.MatterNo = t.MatterNo
FOR XML PATH('')),1,1,'') AS CSV
FROM Table1 AS t
GROUP BY t.MatterNo

是否可以在访问中执行此操作?

修改

我在下面的评论中用ConcatRelated()

添加了这个
SELECT DISTINCT table1.MatterNO, Sum(table1.Amount) AS SumOfAmount,
ConcatRelated("Description","table1","MatterNo = '" & [MatterNo] & "'") AS Descriptions
FROM table1
GROUP BY table1.MatterNO, table1.Description;

我得到了我需要的东西。

但是有一个问题,就是没有添加金额,我该如何解决?

1 个答案:

答案 0 :(得分:0)

请改为尝试:

SELECT
    MatterNO,
    SumOfAmount,
    ConcatRelated("Description","table1","MatterNo = '" & [MatterNo] & "'") AS Descriptions
FROM
    (
        SELECT
            MatterNO,
            SUM(Amount) AS SumOfAmount
        FROM table1
        GROUP BY MatterNO
    ) AS subquery