Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
547 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
Written by Andrew Gould
You can download the script to create the database used in this article here.
Our two-day advanced SQL course usually has time to cover this topic. Failing this, there are loads more SQL training resources here.
The standard way to group and aggregate data in SQL Server is to use the GROUP BY clause.
SELECT
CountryName
,COUNT(FilmID) AS [Count of Films]
FROM
tblFilm AS f INNER JOIN
tblCountry AS c
ON c.CountryID=f.FilmCountryID
GROUP BY
CountryName
Executing the query shown above generates the following results:
The GROUP BY clause generates results grouped in rows.
The row groups generated by this type of query work well enough, but what if you wanted the data grouped by columns instead? What if you wanted both row groups and column groups in the same query?
You might want to organise you groups in columns rather than rows.
You may want to create both row and column groups to create a crosstab-style report.
You can achieve results such as the ones shown above using the PIVOT operator and the rest of this article explains how it works.
You can learn more about this topic on the following Wise Owl courses:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.