[英]Pivoting data in MS Access
I have a query that I've created to pull student IDs and meal items they have taken over a month long period. 我有一个查询,我已创建它来拉取他们已经花了一个月的学生ID和餐食。 I would like to count the numbers of each item (Breakfast, Lunch, Snack) taken by a student over the course of the month.
我想计算一个学生在这个月的每个项目(早餐,午餐,小吃)的数量。
It appears there's too much data for access to handle in a Pivot Table report, so I was hoping there was a SQL query I could run instead. 看来,在数据透视表报表中访问句柄的数据太多,所以我希望有一个我可以运行的SQL查询。
Here's the current query I've created: 这是我创建的当前查询:
SELECT April2013.SID, MenuItems.MealType AS Apr2013Meal
FROM April2013 LEFT JOIN MenuItems ON MenuItems.Item=April2013.Item;
Current output: 当前输出:
+-----+-----------+
| SID | Meal |
+-----+-----------+
| 001 | Lunch |
| 002 | Lunch |
| 003 | Breakfast |
| 004 | Snack |
| 005 | Lunch |
| 006 | Lunch |
| 001 | Breakfast |
| 003 | Snack |
| 004 | Breakfast |
+-----+-----------+
Here's how I'd like it to look: 这是我希望它看起来的样子:
+-----+-----------+-------+---------+
| SID | Breakfast | Lunch | Snack |
+-----+-----------+-------+---------+
| 001 | 3 | 10 | 1 |
| 002 | 4 | 8 | 10 |
| 003 | 18 | 2 | 7 |
| 004 | 6 | 7 | 2 |
+-----+-----------+-------+---------+
You can pivot the data using TRANSFORM: 您可以使用TRANSFORM来转动数据:
TRANSFORM COUNT(MenuItems.MealType)
SELECT April2013.SID, MenuItems.MealType
FROM April2013
LEFT JOIN MenuItems
ON MenuItems.Item=April2013.Item
GROUP BY April2013.SID
PIVOT MenuItems.MealType;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.