I have a table of equipment, a table of customers and a sparse table of equipment and customers. I want to produce a query that returns a 1 if the equipment/Customer table record exists and a 0 if it doesn't for each customer. There are only a small number of customers in this problem, so what I want back should look like this:
EquipmentID Cust1 Cust2 Cust3
----------- ----- ----- -----
1234 1 0 1
1357 0 1 0
2234 1 0 0
I can use a cross join
to get a master list of possible records, but that returns the information in rows. I want to see it in columns, but the PIVOT
keyword requires that I name the columns first. What I need is for the column names (Cust1
, Cust2
...) to be dynamic.