【问题标题】:Select Count (distinct col) query to show number of rows and column in result - postgresql选择 Count (distinct col) 查询以显示结果中的行数和列数 - postgresql
【发布时间】:2019-09-24 16:10:20
【问题描述】:

我有一个 PostgreSQL 表,其中包含几列 col1、col2、col3、col4。我想计算 col3 中有多少行唯一值。结果也需要显示行数和值。我如何形成这样的查询。我正在使用 PgAdmin4。

col1. col2.  col3.   col4.
  x1   y1     123   xx-xx-xxxx   
  x2   y2     123   xx-xx-xxxx
  x3   y3     123   xx-xx-xxxx
  x4   y4     111   xx-xx-xxxx
  x5   y5     111   xx-xx-xxxx

我尝试使用select count( distinct col3) from table_ where col3_ts >'2019-09-17' 但它仅计算/显示所有不同行的数量,即仅像 8999 这样的数字。

示例结果如下:

    #.  col3.    # of rows.
   -----------------------
    1.  123        3 
    2.  111        2
   -----------------------

【问题讨论】:

  • 请分享一些例子。
  • @jjanes:我尝试添加示例。我希望它会有所帮助。
  • 你不需要不同的计数,你只需要按 count() 分组。

标签: postgresql


【解决方案1】:

这是经典的 GROUP BY 用例:

select col3, count(*) from table_ where col3_ts >'2019-09-17' GROUP BY col3.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-30
    • 1970-01-01
    • 2016-04-04
    • 2011-03-17
    • 2020-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多