Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PostgreSQL:左连接,临时表添加额外行_Postgresql_Left Join_Temporary - Fatal编程技术网

PostgreSQL:左连接,临时表添加额外行

PostgreSQL:左连接,临时表添加额外行,postgresql,left-join,temporary,Postgresql,Left Join,Temporary,我有一个观点,即存储水管参考资料以及水系统操作的详细信息。我需要从该视图中提取过去12个月内注册了不止一次操作的水管。以下是我的做法: 以下是视图结构和数据示例: CREATE TABLE schema.pipe ( id INTEGER, code VARCHAR, ope_date DATE, ope_type VARCHAR(2), system VARCHAR(2)); INSERT INTO schema.pipe (code, ope_date, ope_t

我有一个观点,即存储水管参考资料以及水系统操作的详细信息。我需要从该视图中提取过去12个月内注册了不止一次操作的水管。以下是我的做法:

以下是视图结构和数据示例:

CREATE TABLE schema.pipe (
  id INTEGER,
  code VARCHAR, 
  ope_date DATE, 
  ope_type VARCHAR(2),
  system VARCHAR(2));

INSERT INTO schema.pipe (code, ope_date, ope_type, system) VALUES
 ('0001', '2014-11-11', '01', 'EU'),
 ('0001', '2014-11-11', '03', 'EU'),
 ('0002', '2014-12-03', '03', 'EP'),
 ('0002', '2014-01-03', '03', 'EP'),
 ('0003', '2014-08-11', '01', 'EP'),
 ('0003', '2014-03-03', '03', 'EP'),
 ('0003', '2012-02-27', '03', 'EP'),
 ('0004', '2014-08-11', '01', 'UN'),
 ('0004', '2013-12-30', '03', 'UN'),
 ('0004', '2013-06-01', '03', 'UN'),
 ('0004', '2012-07-31', '03', 'UN'),
 ('0005', '2013-10-01', '03', 'EU'),
 ('0005', '2012-11-01', '03', 'EU'),
 ('0006', '2014-04-01', '01', 'UN'),
 ('0006', '2014-05-15', '01', 'UN');
代码是管道参考 操作日期是操作日期 ope_类型是操作类型 system是系统类型 以下是我正在使用的查询:

SELECT code, ope_date FROM schema.pipe 
WHERE (NOW()::DATE - ope_date) < 365 
GROUP BY code, ope_date 
HAVING count(*) = 1 ;
现在,我需要用这个选项返回其他列。因此,我使用:

WITH temptable AS (
    SELECT code, ope_date FROM schema.pipe WHERE (NOW()::DATE - ope_date) < 365 GROUP BY code, ope_date HAVING count(*) = 1)

SELECT DISTINCT a.code, a.ope_date, b.ope_type, b.system FROM temptable a LEFT JOIN schema.pipe b on a.code = b.code ;
所以我的问题来了:我怎样才能得到与我的选择相匹配的行呢

非常感谢

编辑:

我需要的是:

  code   | ope_date    | ope_type  | system
---------+-------------+-----------+---------  
  0002   | 2014-12-03  | 03        | EP
  0002   | 2014-01-03  | 03        | EP
  0003   | 2014-08-11  | 01        | EP
  0003   | 2014-03-03  | 03        | EP
  0004   | 2013-12-30  | 03        | UN
  0004   | 2014-08-11  | 01        | UN
  0006   | 2014-04-01  | 01        | UN
  0006   | 2014-05-15  | 01        | UN

我找到了一个解决方案,它包括使用code和ope_date列来连接表,而不仅仅是代码:


对此解决方案有何评论?

它们符合您的选择。您有两行,不同的操作类型,代码0003和代码0004,而代码0002和代码0006只有一行。我刚刚编辑了底部的文章。我需要的是第一次查询的结果,包括8行和匹配的列。例如,为什么我在12行查询结果中得到0003 | 2014-03-03 | 01 | EP?这条线不在管道表中…谢谢,非常好的站点。。。我已经发布了一个答案和一个解决方案。它更可读从。。。参加在…上和可以在多个列上联接。
  code  |  ope_date    |  ope_type  |  system
 -------+--------------+------------+---------
  0002  |  2014-01-03  |  03        |  EP
  0002  |  2014-12-03  |  03        |  EP
  0003  |  2014-03-03  |  01        |  EP
  0003  |  2014-03-03  |  03        |  EP
  0003  |  2014-08-11  |  01        |  EP
  0003  |  2014-08-11  |  03        |  EP
  0004  |  2013-12-30  |  01        |  UN
  0004  |  2013-12-30  |  03        |  UN
  0004  |  2014-08-11  |  01        |  UN
  0004  |  2014-08-11  |  03        |  UN
  0006  |  2014-04-01  |  01        |  UN
  0006  |  2014-05-15  |  01        |  UN
  code   | ope_date    | ope_type  | system
---------+-------------+-----------+---------  
  0002   | 2014-12-03  | 03        | EP
  0002   | 2014-01-03  | 03        | EP
  0003   | 2014-08-11  | 01        | EP
  0003   | 2014-03-03  | 03        | EP
  0004   | 2013-12-30  | 03        | UN
  0004   | 2014-08-11  | 01        | UN
  0006   | 2014-04-01  | 01        | UN
  0006   | 2014-05-15  | 01        | UN
WITH temptable AS (
    SELECT code, ope_date FROM schema.pipe WHERE (NOW()::DATE - ope_date) < 365 GROUP BY code, ope_date HAVING count(*) = 1)

SELECT DISTINCT a.code, a.ope_date, b.ope_type, b.system FROM temptable a, schema.pipe b WHERE a.code = b.code AND a.ope_date = b.ope_date;