同时从每一列中获取每个值的计数
Get a count of each value from every column at the same time
我试图看到类似的问题,但 none 正在以有效的方式帮助解决这个问题。
我有一个 table 列,如下所示:
我想计算每一列中值的出现次数,但对于 table 的所有列,而不是只有一个。
我想得到这样的东西:
p7 | p7_count | p9 | p9_count
B | 1 | A | 2
A | 1 | E | 1
C | 1
但我只能对每个查询使用一个查询来获取它,例如:
SELECT p9, count(*) AS p9_Count
FROM respostas
GROUP by p9
ORDER BY p9_Count DESC
但是我得到的结果是:
有没有一种方法可以对所有列执行此操作,而不必分别对每个列执行此操作并分别获得结果?
您可以使用 union all
来执行此操作。有点不清楚你到底想要什么。也许这很接近:
select p, max(p7cnt) as p7cnt, max(p8cnt) as p8cnt, max(p9cnt) as p9cnt
from ((select p7 as p, count(*) as p7cnt, 0 as p8cnt, 0 as p9cnt
from respostas
group by p7
) union all
(select p8, 0 as p7cnt, count(*) as p8cnt, 0 as p9cnt
from respostas
group by p8
) union all
(select p9, 0 as p7cnt, 0 as p8cnt, count(*) as p9cnt
from respostas
group by p9
)
) ppp
group by p;
我认为这就是您想象的那种事情。它变得有点混乱,但您可以通过添加到合并来扩展它。为了让它与 row_number 函数一起工作(对于 MySQL),我将其转换为使用子查询。当行数变大时,这甚至不会有效,因为 SQL 不是这项工作的正确工具。
select
p1, p1_count, p2, p2_count, p3, p3_count
from
(
select
p1, p1_count,
(
select count(*) from
(SELECT p1, count(*) AS p1_Count FROM respostas GROUP by p1) as t2
where
t2.p1_Count <= t1.p1_Count
or (t2.p1_Count = t1.p1_Count and t2.p1 <= t1.p1)
) as rownum
from (SELECT p1, count(*) AS p1_Count FROM respostas GROUP by p1) as t1
) as tt1
full outer join
(
select
p2, p2_count,
(
select count(*) from
(SELECT p2, count(*) AS p2_Count FROM respostas GROUP by p2) as t2
where
t2.p2_Count <= t1.p2_Count
or (t2.p2_Count = t1.p2_Count and t2.p2 <= t1.p2)
) as rownum
from (SELECT p2, count(*) AS p2_Count FROM respostas GROUP by p2) as t2
) as tt2
on tt2.rownum = tt1.rownum
full outer join
(
select
p3, p3_count,
(
select count(*) from
(SELECT p3, count(*) AS p3_Count FROM respostas GROUP by p3) as t2
where
t2.p3_Count <= t1.p3_Count
or (t2.p3_Count = t1.p3_Count and t2.p3 <= t1.p3)
) rownum
from (SELECT p3, count(*) AS p3_Count FROM respostas GROUP by p2) as t3
) as tt3
on tt3.rownum = coalesce(tt1.rownum, tt2.rownum)
order by
coalesce(tt1.rownum, tt2.rownum, tt3.rownum)
我试图看到类似的问题,但 none 正在以有效的方式帮助解决这个问题。
我有一个 table 列,如下所示:
我想计算每一列中值的出现次数,但对于 table 的所有列,而不是只有一个。
我想得到这样的东西:
p7 | p7_count | p9 | p9_count
B | 1 | A | 2
A | 1 | E | 1
C | 1
但我只能对每个查询使用一个查询来获取它,例如:
SELECT p9, count(*) AS p9_Count
FROM respostas
GROUP by p9
ORDER BY p9_Count DESC
但是我得到的结果是:
有没有一种方法可以对所有列执行此操作,而不必分别对每个列执行此操作并分别获得结果?
您可以使用 union all
来执行此操作。有点不清楚你到底想要什么。也许这很接近:
select p, max(p7cnt) as p7cnt, max(p8cnt) as p8cnt, max(p9cnt) as p9cnt
from ((select p7 as p, count(*) as p7cnt, 0 as p8cnt, 0 as p9cnt
from respostas
group by p7
) union all
(select p8, 0 as p7cnt, count(*) as p8cnt, 0 as p9cnt
from respostas
group by p8
) union all
(select p9, 0 as p7cnt, 0 as p8cnt, count(*) as p9cnt
from respostas
group by p9
)
) ppp
group by p;
我认为这就是您想象的那种事情。它变得有点混乱,但您可以通过添加到合并来扩展它。为了让它与 row_number 函数一起工作(对于 MySQL),我将其转换为使用子查询。当行数变大时,这甚至不会有效,因为 SQL 不是这项工作的正确工具。
select
p1, p1_count, p2, p2_count, p3, p3_count
from
(
select
p1, p1_count,
(
select count(*) from
(SELECT p1, count(*) AS p1_Count FROM respostas GROUP by p1) as t2
where
t2.p1_Count <= t1.p1_Count
or (t2.p1_Count = t1.p1_Count and t2.p1 <= t1.p1)
) as rownum
from (SELECT p1, count(*) AS p1_Count FROM respostas GROUP by p1) as t1
) as tt1
full outer join
(
select
p2, p2_count,
(
select count(*) from
(SELECT p2, count(*) AS p2_Count FROM respostas GROUP by p2) as t2
where
t2.p2_Count <= t1.p2_Count
or (t2.p2_Count = t1.p2_Count and t2.p2 <= t1.p2)
) as rownum
from (SELECT p2, count(*) AS p2_Count FROM respostas GROUP by p2) as t2
) as tt2
on tt2.rownum = tt1.rownum
full outer join
(
select
p3, p3_count,
(
select count(*) from
(SELECT p3, count(*) AS p3_Count FROM respostas GROUP by p3) as t2
where
t2.p3_Count <= t1.p3_Count
or (t2.p3_Count = t1.p3_Count and t2.p3 <= t1.p3)
) rownum
from (SELECT p3, count(*) AS p3_Count FROM respostas GROUP by p2) as t3
) as tt3
on tt3.rownum = coalesce(tt1.rownum, tt2.rownum)
order by
coalesce(tt1.rownum, tt2.rownum, tt3.rownum)