I am trying to understand how to use PIVOT in SQL. Ideally I'd like a query with dynamic rows and dynamic columns. I would post a code sample of what I've got so far, but I'm afraid I don't have anything working.
I have 2 primary entities and an M:N map such as this.
DECLARE @people TABLE(id INT NOT NULL PRIMARY KEY IDENTITY(1,1),name VARCHAR(MAX) NOT NULL)
INSERT INTO @people (name) VALUES ('Joe'),('Sally'),('Mary')
DECLARE @group TABLE(id INT NOT NULL PRIMARY KEY IDENTITY(1,1),name VARCHAR(MAX) NOT NULL)
INSERT INTO @group (name) VALUES ('Cool Kids'),('Band Camp'),('Chess Club')
DECLARE @map TABLE(peopleId INT, groupId INT)
INSERT INTO @map (peopleId,groupId) VALUES (1,1),(1,3),(2,3),(3,2)
Ideally I'd like a result that puts one entity as rows, another as columns, and some piece of data (a simple boolean or sum) at the intersection.
Cool Kids Band Camp Chess Club
Joe TRUE FALSE TRUE
Sally FALSE FALSE TRUE
Mary FALSE TRUE TRUE
Most examples I've seen seem to suggest you require knowing which @groups you are querying for in advance, which I don't.