我有一个看起来像这样的数据集:
+----+-------------+
| ID | StoreVisit |
+----+-------------+
| 1 | Home Depot |
| 2 | Lowes |
| 3 | Home Depot |
| 2 | ACE |
| 2 | Lowes |
| 1 | Home Depot |
| 4 | ACE |
| 5 | ACE |
| 4 | Lowes |
+----+-------------+
我是 SQL 的新手,我知道我可以全选,然后使用 Excel(数据透视表/函数/特殊粘贴)或 R (tidyr) 进行转置。但是,如果我有很多数据,这样效率不高。下面的查询是否正确?如果是这样,如果有数千种类型的商店而无需在查询中逐一键入,我如何定义 StoreVisit 的所有值?
select * from Stores
pivot (COUNT(StoreVisit) for StoreVisit in ([ACE],[Lowes],[Home Depot])) as StoreCounts
+----+-------+-----------+-----+
| ID | Lowes | HomeDepot | ACE |
+----+-------+-----------+-----+
| 1 | 0 | 2 | 0 |
| 2 | 2 | 0 | 0 |
| 3 | 0 | 1 | 0 |
| 4 | 1 | 0 | 1 |
| 5 | 0 | 0 | 1 |
+----+-------+-----------+-----+
请原谅这篇文章的格式!非常抱歉。
最佳答案
使用条件聚合:
select id,
sum(storevisit = 'Lowes') as lowes,
sum(storevisit = 'HomeDepot') as HomeDepot,
sum(storevisit = 'Ace') as ace
from t
group by id;
关于mysql - 我知道 PIVOT 命令可以转换数据集,这是正确的方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56535524/