sql - PostgreSQL - 根据 3 列选择重复数据

标签 sql postgresql

这是表格。

CREATE TABLE cameras
(
  id integer NOT NULL DEFAULT nextval('sq_streams'::regclass),
  created_at timestamp with time zone NOT NULL DEFAULT now(),
  updated_at timestamp with time zone NOT NULL DEFAULT now(),
  exid text NOT NULL,
  owner_id integer NOT NULL,
  is_public boolean NOT NULL,
  config json NOT NULL,
  name text NOT NULL,
  last_polled_at timestamp with time zone DEFAULT now(),
  is_online boolean,
  timezone text,
  last_online_at timestamp with time zone DEFAULT now(),
  location geography(Point,4326),
  mac_address macaddr,
  model_id integer,
  discoverable boolean NOT NULL DEFAULT false,
  preview bytea,
  thumbnail_url text,
  CONSTRAINT pk_streams PRIMARY KEY (id)
)

我有 5000 条记录。我想要得到的是以下结果: 有 57 个摄像头,它们各不相同,但有 3 个相同,它们是

'external_http_port', config->> 'external_host', config-> 'snapshots'->> 'jpg'

我想创建一个查询,将结果显示为

external_http_port | external_host | config-> 'snapshots'->> 'jpg' | Count
"93.87.73.118"     | "1024"        |      "/oneshotimage.jpg"      | 57
"180.180.180.48"   |  "81"         |"/onvif/media_service/snapshot"|20

最佳答案

这个呢?

SELECT config ->>'external_http_port' AS external_http_port, 
       config->> 'external_host' AS external_host, 
       config-> 'snapshots'->> 'jpg' AS snapshots,
       count(*) 
FROM cameras  
GROUP BY config->>'external_http_port', config->> 'external_host', config-> 'snapshots'->> 'jpg' 
HAVING count(*) > 1;

关于sql - PostgreSQL - 根据 3 列选择重复数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34352177/

相关文章:

postgresql - 强制 PostgreSQL 在函数中使用不同的模式

mysql - 如何在 SQL 中逐行创建部分余额

sql - 对 mysql 查询使用反引号/反引号

postgresql - 使用 Docker 和 Jenkins 管理用于测试的资源(数据库、elasticsearch、redis 等)

postgresql - ubuntu 14.10 和 postgres 9.4 如何运行 pg_controldata

postgresql - PSQL 连接别名

sql - 如何引用下一行的数据?

sql - SparkSQL : conditional sum using two columns

mysql - 创建唯一索引多列,其中一列的值为零

mysql - 在mysql中获取随机数组值