如何 Return 多列,但删除 1 列的重复项

How to Return multiple columns, but remove duplicates of 1 column

我正在尝试向 return 所有列编写查询,并通过显示来自特定列的 DISTINCT 记录进行过滤。

这是我现在的查询,它要求我 "Group By" select 语句中的所有内容。我显然不想这样做。:

SELECT     lVisitID, sFirstName, sLastName, sAddress1, sStoreNumber
FROM        Customers
WHERE     (dtServiceDate < '5/1/15') AND (sStoreNumber = '123') 
GROUP BY sAddress1

我也试过下面的方法,但是 return 是重复的:

    SELECT     lVisitID, MAX(sAddress1)
    FROM        Customers
    WHERE     (dtServiceDate < '5/1/15') AND (sStoreNumber = '123') 
    GROUP BY sAddress1, lvisitID

这是我的数据:

lVisitID     sFirstName   sLastName     sAddress1     sStoreNumber
  1             Bob          Jones         14 Place     123
  2             Jim          Bibby         12 Place     123
  3             John         Smith         12 Place     123
  4             Jen         Jones          22 Place     193
  6             Kim         Smith          15 Place     123

这里的想法是 return 只有不同的地址和商店编号。当我尝试上述操作时,我需要:

  1             Bob          Jones         14 Place     123
  2             Jim          Bibby         12 Place     123
  6             Kim         Smith          15 Place     123

如果您只需要最近访问的地址,请使用 row_number():

select c.*
from (select c.*,
             row_number() over (partition by address order by lVisitId desc) as seqnum
      from customers c
     ) c
where seqnum = 1;

我在 Postgres SQL 上试过这个。

SELECT     Distinct on (sAddress1) *
FROM        Customers
WHERE     (dtServiceDate < '5/1/15') AND (sStoreNumber = '123')