【问题标题】:How to compare two rows of same entry in PostgreSQL?如何比较 PostgreSQL 中相同条目的两行?
【发布时间】:2014-02-19 05:24:30
【问题描述】:

下面是我针对 Postgres 数据库运行的查询。

select distinct(col1),col2,col3,col4 
    from tableNot 
    where col5 = 100 and col6 = '78' 
    order by col4 DESC  limit 100;

下面是我在控制台上得到的输出-

col1            col2       col3      col4 

entry.one       1.2.3       18       subject
entry.one       1.2.8       18       newSubject
entry.two       3.4.9       20       lifePerfect
entry.two       3.4.5       17       helloPartner
  • 现在,如果您看到我上面的输出,entry.oneentry.two 将出现两次,所以我将比较 col2 的值与 entry.one 的值,以较高者为准,我将保留该行。因此,对于entry.one,我会将1.2.31.2.8 进行比较,而1.2.8 对于entry.one 更高,所以我只会为entry.one 保留这一行。
  • entry.two 类似,我会将3.4.93.4.5 进行比较,3.4.9 对于entry.two 更高,所以我将只为entry.two 保留这一行。

下面是我想在控制台上看到的输出 -

col1            col2       col3      col4 

entry.one       1.2.8       18       newSubject
entry.two       3.4.9       20       lifePerfect    

这可以在 SQL 中实现吗?

P.S 任何 Fiddle 示例都会很棒。

【问题讨论】:

    标签: sql postgresql comparison aggregate-functions


    【解决方案1】:

    您可能正在寻找 distinct on,它是 distinct 运算符的 Postgres 特定扩展。

    select distinct on (col1)  col1, col2, col3, col4 
    from tableNot 
    where col5 = 100 
      and col6 = '78' 
    order by col1, col4 DESC  
    limit 100;
    

    http://sqlfiddle.com/#!15/04698/1


    请注意,distinct 不是函数。 select distinct (col1), col2, col3select distinct col1, col2, col3 完全一样。

    (标准 SQL)distinct 运算符始终对选择列表的 所有 列进行操作,而不仅仅是一个。

    select distinct (col1), col2select distinct col1, col2 的区别与select (col1), col2select col1, col2 的区别相同。

    【讨论】:

      【解决方案2】:

      试试这个查询:

      SELECT * FROM
      (select distinct(col1),col2,col3,col4 from tableNot 
      where col5 = 100 and col6 = '78' order by col4 DESC  limit 100)t1
      WHERE t1.col2 = (SELECT MAX(t2.col2) FROM (select distinct(col1),col2,col3,col4 
      from tableNot 
      where col5 = 100 and col6 = '78' order by col4 DESC  limit 100)t2 
      WHERE t1.col1 = t2.col1 group by t2.col1)
      

      SQL Fiddle

      【讨论】:

      • 感谢 Hamidreza,运行您的查询后出现以下错误:ERROR: argument of AND must be type boolean, not type integer LINE 2: ...t1.col6 = '91' order by t1.col4 DESC limit 100 ^ ********** Error ********** ERROR: argument of AND must be type boolean, not type integer SQL state: 42804 Character: 181
      • 不客气,我的朋友。我的查询只是给你一些线索。因为我看不到你的真实桌子。在sql fiddle 中查看我的查询,这对你很有帮助找到结果。
      • 我的朋友编辑了我的查询,它基于您的查询,如果您的查询返回值,我的查询也将返回值。
      • 在查询的最后,你有group by t2.col) 这里 col 是什么意思?是 col2 还是 col3?
      • 你是对的,我的朋友,这是我的错,我现在编辑它是group by col1
      【解决方案3】:

      您可以尝试另一个简单的查询:

      SELECT DISTINCT t1.col1, t1.col2, t1.col3, t1.col4 
      FROM tableNot t1 
      INNER JOIN (SELECT MAX(col2) col2 
      FROM tableNot GROUP BY col1) t2  ON t1.col2=t2.col2
      WHERE col5 = 100 AND col6 = '78' ORDER BY col4 DESC  LIMIT 100;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多