选择不同的,在行尾显示计数

发布于 2025-01-03 23:13:23 字数 543 浏览 1 评论 0原文

我有这个表/数据结构:

ID    Area       Postcode
------------------------
1     "Area 1"   "EN1 1NE"
2     "Area 2"   "AB2 3BA"
3     "Area 1"   "EN1 1NE"
4     "Area 3"   "XY4 5ZA"
5     "Area 4"   "MN6 5OP"

第 1 行和第 3 行有重复的邮政编码和区域。我需要一个输出这样的计数的查询:

Area       Postcode    Count
----------------------------
"Area 1"   "EN1 1NE"   2
"Area 2"   "AB2 3BA"   1
"Area 3"   "XY4 5ZA"   1
"Area 4"   "MN6 5OP"   1

我已经搜索并玩过 DISTINCT 和/或 COUNT 和/或子查询,但在这里真的迷失了! #weak-sql-fu

谢谢! 本

I have this table/data structure:

ID    Area       Postcode
------------------------
1     "Area 1"   "EN1 1NE"
2     "Area 2"   "AB2 3BA"
3     "Area 1"   "EN1 1NE"
4     "Area 3"   "XY4 5ZA"
5     "Area 4"   "MN6 5OP"

Rows 1 and 3 have duplicate Postcodes and Areas. I need a query that outputs counts like this this:

Area       Postcode    Count
----------------------------
"Area 1"   "EN1 1NE"   2
"Area 2"   "AB2 3BA"   1
"Area 3"   "XY4 5ZA"   1
"Area 4"   "MN6 5OP"   1

I've searched and played about with DISTINCT and/or COUNT and/or subqueries but getting really lost here! #weak-sql-fu

Thanks!
Ben

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白况 2025-01-10 23:13:23

您可以将 count(*) 与 group by 一起使用,如下所示:

select Area, Postcode, count(*) from TABLE group by Area, Postcode;

You can use count(*) with group by like this:

select Area, Postcode, count(*) from TABLE group by Area, Postcode;
橙味迷妹 2025-01-10 23:13:23

必须在 ALIAS COUNT 中使用反引号来转义 RESERVED WORD。

SELECT Area, 
       Postcode, 
       COUNT(ID) as `Count` 
FROM   tableName 
Group BY Area, Postcode
ORDER BY Area;

have to use backtick within the ALIAS COUNT to escape the RESERVED WORD.

SELECT Area, 
       Postcode, 
       COUNT(ID) as `Count` 
FROM   tableName 
Group BY Area, Postcode
ORDER BY Area;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文