简体   繁体   English

访问查询中的串联值

[英]Access Concatenating Values in a Query

I have a query, [Query1], with employee names, projects, days, months, and years. 我有一个查询[Query1],其中包含员工姓名,项目,天,月和年。

In another query, [Query2], I take all the values and put them into a cross table. 在另一个查询[Query2]中,我将所有值都放入一个交叉表中。 My rows are "Year, Month, Employee." 我的行是“年,月,员工”。 My column is "Day." 我的专栏是“日”。 My values are Projects. 我的价值观是项目。

The problem is that for one date, there may be more than one project assigned to an employee. 问题在于,有一天,可能有多个项目分配给一名员工。

When I attempt to put the projects as values into a table using IIf(Count(*)>0,[Project],"") , I get an error because there may be more than one possible value for the project, and access doesn't know which one to choose. 当我尝试使用IIf(Count(*)>0,[Project],"")将项目作为值放入表中时,出现错误,因为该项目可能有多个可能的值,并且访问没有不知道该选择哪一个。

I need a way to Concatenate the values if there is more than one Project. 如果有多个项目,我需要一种连接值的方法。

Ex: 例如:

[Query1]
Bill |  CC555  |  28  |  03  |  2014
Jim  |  CC999  |  29  |  03  |  2014
Jim  |  CC555  |  29  |  03  |  2014
John |  CC555  |  29  |  03  |  2014

[Query2]
Year  | Month | Employee | 1 | 2 | 3 | ... | 27 |  28   |      29       | 30 | 31 
2014  |  03   |   Bill   | - | - | - | ... | -  | CC555 |      -        | -  | -
2014  |  03   |   Jim    | - | - | - | ... | -  |   -   | CC555 + CC999 | -  | -
2014  |  03   |   John   | - | - | - | ... | -  |   -   |     CC555     | -  | -

Aside: [Query1] is dynamic and could have duplicate dates deleted or added to it, so [Query2] values must change accordingly. 另外:[Query1]是动态的,可能已删除或添加了重复的日期,因此[Query2]值必须相应地更改。

one simple example,you have to make it dynamic,in real scenrio no need of table variable or CTE if using dynamic sql.i think no need of dynamic,just hard code from 1 to 31 一个简单的示例,您必须使其动态,在实际的场景中,如果使用动态sql,则不需要表变量或CTE。我认为不需要动态,只需将硬代码从1到31

;With CTE as
(
select   'Bill' Employee ,'CC555' codes,28 dd,03 mm ,2014 yrs union all
select  'Jim ','CC999',  29 ,  03 ,  2014  union all
select  'Jim ','CC555',  29 ,  03 ,  2014  union all
select  'John','CC555',  29 ,  03 , 2014
)
select yrs,mm,Employee,isnull([28],'-')[28],[29],[30] from
(select Employee,dd,mm,yrs 
,stuff((select ','+codes from cte b where b.Employee=a.Employee for xml path('')),1,1,'')codes
from cte a ) src
pivot (min(codes) for dd in([28],[29],[30])) pvt

通过使用此处allenbrowne.com/func-concat.html的功能,并按照http://www.access-programmers.co.uk/forums/showthread.php?t=234291的示例,我能够解决这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM