4

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.

4

2 回答 2

5

如果要这样做,则必须动态创建查询字符串,然后将其传递给sp_execute存储过程。AFAIK,如果不动态构造查询字符串,就没有办法做到这一点。

于 2009-04-01T16:13:45.250 回答
0

如果您有方便的报告工具,请使用当前查询并制作“martix”、“交叉表”或“数据透视表”报告。

于 2009-04-01T17:15:30.640 回答