Rows to Dynamic columns in Access

隐身守侯 提交于 2019-12-25 03:27:36

问题


I need a setup in Access where some rows in a table are converted to columns...for example, lets say I have this table:

Team    Employee    DaysWorked
Sales    John         23
Sales    Mark         3
Sales    James        5

And then through the use of a query/form/something else, I would like the following display:

Team   John   Mark   James
Sales   23     3       5

This conversion of rows to columns would have to be dynamic as a team could have any number of Employees and the Employees could change etc. Could anyone please guide me on the best way to achieve this?


回答1:


You want to create a CrossTab query. Here's the SQL that you can use.

TRANSFORM SUM(YourTable.DaysWorked) AS DaysWorked
SELECT YourTable.Team
FROM YourTable
GROUP BY YourTable.Team
PIVOT YourTable.Employee

Of course the output is slightly different in that the columns are in alphabetical order.

Team   James  John  Mark
Sales  5      23    3

For more detail see Make summary data easier to read by using a crosstab query at office.microsoft.com



来源:https://stackoverflow.com/questions/8523520/rows-to-dynamic-columns-in-access

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!