Home > Database > Mysql Tutorial > How Can I Pivot Data in MS Access Using SQL to Count Meal Types Consumed by Students?

How Can I Pivot Data in MS Access Using SQL to Count Meal Types Consumed by Students?

DDD
Release: 2025-01-03 21:35:42
Original
545 people have browsed it

How Can I Pivot Data in MS Access Using SQL to Count Meal Types Consumed by Students?

Pivoting Data in MS Access Using SQL

To pivot data in MS Access, one can utilize the TRANSFORM function to count occurrences of distinct values across multiple columns. This approach is particularly useful when dealing with large datasets that may exceed the capacity of pivot tables.

Consider the following scenario: You have a query that retrieves student IDs (SIDs) and meal items (MealType) they consumed over a month. To count the number of times each student consumed specific meal types (Breakfast, Lunch, Snack), you can employ the following SQL query:

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; 
Copy after login

This query will pivot the data, creating a new table with columns for each MealType (Breakfast, Lunch, Snack) and displaying the count of each meal type for each student. The resulting table will be structured as follows:

+-----+-----------+-------+---------+  
| SID | Breakfast | Lunch | Snack   |  
+-----+-----------+-------+---------+  
| 001 |         3 |    10 |     1   |  
| 002 |         4 |     8 |    10   |  
| 003 |        18 |     2 |     7   |  
| 004 |         6 |     7 |     2   |  
+-----+-----------+-------+---------+  
Copy after login

This pivoted table provides a concise summary of the meal consumption patterns for each student over the specified time period.

The above is the detailed content of How Can I Pivot Data in MS Access Using SQL to Count Meal Types Consumed by Students?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template