如何不在2列中包含具有匹配值的行

时间:2018-05-30 18:40:52

标签: postgresql

我有一个有3列的表。我不想根据具有相同值的2列重复该行。

  |start_date(date)| end_date(date) |theDiff(integer)
  ---------------------------------------------------
  |2018-05-14      |  2018-05-18    |        5      |
  |2018-05-14      |  2018-05-18    |        10     |
  ---------------------------------------------------

预期产出:

  |start_date(date)| end_date(date) |theDiff(integer)
  ---------------------------------------------------
  |2018-05-14      |  2018-05-18    |        5      |
  ---------------------------------------------------

我有很多这样的行,不希望它们重复。我只是想添加他们的第一次出现。我总是希望它输出较低的差异。

2 个答案:

答案 0 :(得分:1)

目前尚不清楚如何确定结果theDiff,一些替代的AGGREGATING函数:

-- diff between max and min
SELECT start_date, end_date, MAX(thediff)-MIN(thediff)
FROM T
GROUP BY start_date, end_date

-- min value
SELECT start_date, end_date, MIN(thediff)
FROM T
GROUP BY start_date, end_date

答案 1 :(得分:1)

如果我理解正确,您希望第一次出现所有不同的(start_date,end_date)。

您可以使用row_number()函数,然后选择行号= 1的行。

with x as
(
    select start_date, end_date, theDiff,
           row_number() over (partition by start_date, end_date order by start_date) rn
    from   tbl
)
select start_date, end_date, theDiff
from   x
where  rn = 1; 
start_date | end_date   | thediff
:--------- | :--------- | ------:
2018-05-14 | 2018-05-18 |       5

db<>小提琴here

您可以将CTE移动到FROM子句:

select start_date, end_date, theDiff
from
    (
        select start_date, end_date, theDiff,
               row_number() over (partition by start_date, end_date order by start_date) rn
        from   tbl
    ) x
where  rn = 1;