[英]Get frequency of data from two columns via one SQL query
I have the table 我有桌子
_______________ | from | to | | 1 | 2 | | 2 | 2 | | 1 | 2 | | 3 | 2 |
Where from
and to
are countries. 当
from
和to
是国家。
I need to get top 10 countries met in from
and to
. 我需要获得前10名的国家遇到了
from
和to
。
The result would be 结果将是
________________ |country| count | | 2 | 5 | | 1 | 2 | | 3 | 1 |
I reached what I need in two queries, but I am almost sure there is possibility to manage it with one. 我通过两个查询达到了所需的条件,但是我几乎可以肯定可以通过一个查询进行管理。
Thanks in advance. 提前致谢。
You want to use a UNION ALL
and then GROUP BY
using the union of from and to: 您要使用
UNION ALL
,然后使用GROUP BY
使用from和to的并集:
SELECT country, COUNT(*) AS count FROM
(SELECT from AS country FROM table_name
UNION ALL
SELECT to AS country FROM table_name)
GROUP BY country
ORDER BY count DESC
LIMIT 10
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.