MS Access 2013 连接行查询

MS Access 2013 Concatenate Rows query

我正在寻找类似于 FOR XML PATH ('') 的 MS Access。

由于我只能读取数据但不能修改 访问权限file/database,所以除了查询之外的所有内容对我来说都不是一个选项。

一点细节:

我的目标是进行如下查询,但可用于 MS Access 2013:

SELECT 
    Items.Name, Items.Description , 
    (SELECT ItemValues.Value + ';'
     FROM Itemvalues 
     WHERE Itemvalues.ItemID = [value] 
       AND someOtherConditions
     FOR XML PATH ('') ) AS Values 
FROM 
    Items 
WHERE 
    Items.ID = [value]

如果ItemValue选择的结果会像

SELECT ItemValues.Value
FROM Itemvalues 
WHERE Itemvalues.ItemID = [value] 
  AND someOtherConditions

输出:

     itemval1
     property2val
     1234foo

物品选择的结果会是这样

SELECT 
    Items.Name, Items.Description 
FROM 
    Items 
WHERE 
    Items.ID = [value]

输出:

 Testitem | This is a test item

我想要一个像

这样的结果行
 Testitem | This is a text test item | itemval1;property2val;1234foo;

感谢您的帮助

PS:我看过一些关于这个主题的其他帖子,但由于这些帖子要么已有多年历史,要么不适合我的情况,我正在试试运气。

考虑以下查询,子查询和派生 table 嵌套在聚合查询中。内部查询计算每个 ItemIDItemValues 的数量,然后外部查询使用计算的计数按 [Name]Description 有条件地聚合以输出项目值的串联字符串。

不幸的是,您需要为要连接的每个值显式添加 Max(IIF(...) 查询语句,因为下面仅使用示例中的三个。见省略号。

请注意:此解决方案假定您在 [ItemValues] table:

中有一个主要的自动编号 ID 字段
SELECT Items.Name, Items.Description,
       Max(IIF(ItemCount=1, t1.Value, NULL)) & ';' &  
       Max(IIF(ItemCount=2, t1.Value, NULL)) & ';' & 
       Max(IIF(ItemCount=3, t1.Value, NULL)) 
       ... As ItemValues       
FROM(
     SELECT Items.Name, Items.Description, t1.Value,
            (SELECT Count(*) FROM ItemValues t2 
             WHERE t1.ItemID = t2.ItemID AND t1.ID >=t2.ID) As ItemCount
     FROM Items INNER JOIN ItemValues t1 ON Items.ID = t1.ItemID
) As derivedTable
GROUP BY Items.Name, Items.Description;

输出

Name        Description            ItemValues
Testitem    This is a test item    itemval1;property2val;1234foo

顺便说一下,您的 ItemsItemValues table 似乎类似于 Entity-Attribute Value (EAV) model where multiple data items and types are stored in a single column. Though it makes for efficient storage at an extreme type of normalization, this model is generally not recommended in database design. See SO posts on the subject: here and here。考虑修改 table 架构以避免复杂的查询。