Col1仅包含X和Y.
Col1 Col2
X abc
Y pqr
X pqr
X mnq
Y cxr
Run Code Online (Sandbox Code Playgroud)
我想这样做:
X Y Col2
Yes Yes pqr
Yes No abc
Yes No mnq
No Yes cxr
Run Code Online (Sandbox Code Playgroud)
我应该写什么SQL查询?
Cri*_*scu 12
使用SQL PIVOT运算符的解决方案:
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;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17686 次 |
| 最近记录: |