如何在Access查询中将多行的文本值组合到一个单元格中[重复]
Posted
技术标签:
【中文标题】如何在Access查询中将多行的文本值组合到一个单元格中[重复]【英文标题】:How to combine text values from multiple rows into a single cell in Access query [duplicate] 【发布时间】:2016-11-29 08:59:22 【问题描述】:我有一个表格,用于在一年中每个月为员工工资增加折扣或激励。此表包含一个字段,使用查找只能包含“折扣”或“动机”字样。
每个月我都可以添加多个折扣或激励措施,所以当我想在月底支付工资时,我希望将这些数据为每个员工分组在一行中。
这是包含当月折扣和激励的表格。表名是 Table1:
想要的查询结果:
【问题讨论】:
看看这里:***.com/questions/19478272/… 【参考方案1】:除了一个例外,以下查询提供了您想要的结果。例外情况是 Total_Discount
和 Total_Motivation
列未格式化为货币(这可能是不可取的,因为格式化的列将是字符串而不是数字)。
SELECT t.The_Year
, t.The_Month
, t.Emp_Num
, Nz(DSum("Money_Amount","Table1","Discount_Motivation = 'Discount' AND The_Year = " & t.The_Year & " AND The_Month = " & t.The_Month & " AND Emp_Num = " & t.Emp_Num), 0) AS Total_Discount
, ConcatRelated("Reason","Table1","Discount_Motivation = 'Discount' AND The_Year = " & t.The_Year & " AND The_Month = " & t.The_Month & " AND Emp_Num = " & t.Emp_Num) AS Discount_Reasons
, Nz(DSum("Money_Amount","Table1","Discount_Motivation = 'Motivation' AND The_Year = " & t.The_Year & " AND The_Month = " & t.The_Month & " AND Emp_Num = " & t.Emp_Num), 0) AS Total_Motivation
, ConcatRelated("Reason","Table1","Discount_Motivation = 'Motivation' AND The_Year = " & t.The_Year & " AND The_Month = " & t.The_Month & " AND Emp_Num = " & t.Emp_Num) AS Motivation_Reasons
FROM Table1 t
GROUP BY t.The_Year, t.Emp_Num, t.The_Month
ORDER BY t.The_Year, t.Emp_Num, t.The_Month
;
但是,如果确实需要这种格式,您可以简单地将这些列的行包装在 Format
函数中,并提供“货币”作为第二个参数:
SELECT t.The_Year
, t.The_Month
, t.Emp_Num
, Format(Nz(DSum("Money_Amount","Table1","Discount_Motivation = 'Discount' AND The_Year = " & t.The_Year & " AND The_Month = " & t.The_Month & " AND Emp_Num = " & t.Emp_Num), 0), "Currency") AS Total_Discount
, ConcatRelated("Reason","Table1","Discount_Motivation = 'Discount' AND The_Year = " & t.The_Year & " AND The_Month = " & t.The_Month & " AND Emp_Num = " & t.Emp_Num) AS Discount_Reasons
, Format(Nz(DSum("Money_Amount","Table1","Discount_Motivation = 'Motivation' AND The_Year = " & t.The_Year & " AND The_Month = " & t.The_Month & " AND Emp_Num = " & t.Emp_Num), 0), "Currency") AS Total_Motivation
, ConcatRelated("Reason","Table1","Discount_Motivation = 'Motivation' AND The_Year = " & t.The_Year & " AND The_Month = " & t.The_Month & " AND Emp_Num = " & t.Emp_Num) AS Motivation_Reasons
FROM Table1 t
GROUP BY t.The_Year, t.Emp_Num, t.The_Month
ORDER BY t.The_Year, t.Emp_Num, t.The_Month
;
【讨论】:
太好了,真的非常感谢你,你是最棒的:)以上是关于如何在Access查询中将多行的文本值组合到一个单元格中[重复]的主要内容,如果未能解决你的问题,请参考以下文章
如何在 MS Access 的追加查询中将数据类型从文本转换为是/否?