[英]MS Access 2013 Concatenate Rows query
I'm looking for something like FOR XML PATH ('') for MS Access. 我正在寻找用于MS Access的FOR XML PATH('')之类的东西。
Since I can only read data but not modify the access file/database, everything beyond querying is not an option for me. 由于我只能读取数据,而不能修改访问文件/数据库,因此查询之外的所有操作对我来说都是不可行的。
A little detail: 一点细节:
My target is to have some query like below but usable for MS Access 2013: 我的目标是要进行如下查询,但可用于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]
If the results of ItemValue selection will be like 如果ItemValue选择的结果会像
SELECT ItemValues.Value
FROM Itemvalues
WHERE Itemvalues.ItemID = [value]
AND someOtherConditions
Output: 输出:
itemval1
property2val
1234foo
And the result of Item Selection will be like 项目选择的结果将像
SELECT
Items.Name, Items.Description
FROM
Items
WHERE
Items.ID = [value]
Output: 输出:
Testitem | This is a test item
I want to have a result row like 我想要像这样的结果行
Testitem | This is a text test item | itemval1;property2val;1234foo;
Thanks your for help 谢谢你的帮助
PS: I've seen some other posts about this topic but since those are either many years old or not fit for my situation I'm trying my luck. PS:我已经看过其他有关此主题的文章,但是由于这些文章已有多年历史,或者不适合我的情况,所以我正在尝试自己的运气。
Consider the following query with subquery and derived table nested in an aggregate query. 考虑以下查询,其中子查询和派生表嵌套在聚合查询中。 Inner query counts number of ItemValues
per ItemID
and then outer query conditionally aggregates by [Name]
and Description
using calculated count to output a concatenation string of Item's Values. 内部查询对每个ItemID
的ItemValues
数量进行计数,然后外部查询使用计算出的计数按[Name]
和Description
有条件地进行聚合,以输出项目值的串联字符串。
Unfortunately, you will need to explicitly add Max(IIF(...)
query statements for each value to be concatenated as below only uses three from your example. See ellipsis. 不幸的是,您将需要为要串联的每个值显式添加Max(IIF(...)
查询语句,如下所示,仅使用示例中的三个。
Please note: this solution assumes you have a primary, autonumber ID
field in [ItemValues]
table: 请注意:此解决方案假定您在[ItemValues]
表中具有一个主要的自动编号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;
OUTPUT OUTPUT
Name Description ItemValues
Testitem This is a test item itemval1;property2val;1234foo
By the way, your Items
and ItemValues
table seems to resemble the Entity-Attribute Value (EAV) model where multiple data items and types are stored in a single column. 顺便说一下,您的Items
和ItemValues
表似乎类似于Entity-Attribute Value(EAV)模型,其中多个数据项和类型存储在单个列中。 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 . 请参阅关于此主题的SO帖子: 此处和此处 。 Consider a revised table schema to avoid complex queries down the road. 考虑修改后的表架构,以避免将来出现复杂的查询。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.