我有一张桌子
id value 1 a 2 a 3 b 4 b 5 b 6 c
我的身份证明是主要的。 我总共有2 a,3 b和1 c。所以我想计算每个匹配的主要ID的总重复值
我想要这种格式
id value_count 1 2 2 2 3 3 4 3 5 3 6 1
答案 0 :(得分:2)
尝试此查询:
SELECT a.id, b.valueCnt
FROM tableA a
INNER JOIN (SELECT a.value, COUNT(a.value) valueCnt
FROM tableA a GROUP BY a.value) AS B ON a.value = b.value;
<强>输出强>
| ID | VALUECNT |
|----|----------|
| 1 | 2 |
| 2 | 2 |
| 3 | 3 |
| 4 | 3 |
| 5 | 3 |
| 6 | 1 |
答案 1 :(得分:1)
试试这个
select id, value_count from tablename as a1
join (select count(*) as value_count, value from tablename group by value) as a2
on a1.value= a2.value
答案 2 :(得分:1)
我建议您使用没有任何连接的子选择:
SELECT
a.id,(SELECT COUNT(*) FROM tableA WHERE value = a.value) as valueCnt
FROM tableA a
答案 3 :(得分:0)
您需要使用子查询。
SELECT table.id , x.value_count
FROM table
INNER JOIN
(SELECT t1.value, count(t1.id) as value_count
FROM table t1
Group by t1.value
) x on x.value = table.value