有没有办法在这些列中插入COUNT(*)的一些值。解释。我有一张桌子
myObject1
其中值是类型(A,B,C)在数据集中出现的次数(这就是我想要COUNT的原因) 我希望在决赛桌上有类似的东西
+--------+----------+
| url | type |
+--------+----------+
| URl1 | A |
+--------+----------+
| URL1 | B |
+--------+----------+
| URL2 | C |
+--------+----------+
| URL2 | C |
+--------+----------+
直到现在我这样做了
+--------+------+-----+-----+
| url | A | B | C |
+--------+------+-----+-----+
| URL1 | 3 | 1 | 0 |
+--------+------+-----+-----+
| URL2 | 2 | 0 | 1 |
+--------+------+-----+-----+
我得到了这个(随机例子)
SELECT
url, type, COUNT(*)
FROM
table
GROUP BY url,type
我希望插入上面表格中的值
答案 0 :(得分:0)
你可以使用case when和group by
select url,
sum(case when type ='A' then 1 else 0 end) as A,
sum(case when type ='B' then 1 else 0 end) as B,
sum(case when type ='C' then 1 else 0 end) as C
FROM my_table
group by url ;