Col1仅包含X和Y.
Col1 Col2 X abc Y pqr X pqr X mnq Y cxr
我想这样做:@H_502_5@
X Y Col2 Yes Yes pqr Yes No abc Yes No mnq No Yes cxr
解决方法
使用
SQL PIVOT operator的解决方案:
SELECT Col2,case when X=0 then 'No' else 'Yes' end as X,case when Y=0 then 'No' else 'Yes' end as Y FROM MyTable PIVOT ( count(Col1) FOR Col1 IN ([X],[Y]) ) AS PivotTable;
运行样本:http://www.sqlfiddle.com/#!3/5856d/14@H_502_5@