DISTINCT 不适用于 MSSQL 中的 SELECT 查询

DISTINCT is not working for SELECT query in MSSQL

我这里有发票详细信息 table。 我需要不重复 [invoiceno] 的所有值。 我试试

select distinct invoiceno,name,addr1,addr2,id from invoice_table;

结果:

invoiceno    name             addr1       addr2             id
2016718001  Severus Sanpe   7,Hogwards, Sevilee,USA 7451    5
2016718002  Severus Sanpe   7,Hogwards, Sevilee,USA 7451    8
2016718002  Severus Sanpe   7,Hogwards, Sevilee,USA 7451    9

我想要结果:

 invoiceno   name             addr1       addr2             id
2016718001  Severus Sanpe   7,Hogwards, Sevilee,USA 7451    5
2016718002  Severus Sanpe   7,Hogwards, Sevilee,USA 7451    8

SELECT 中没有 id 时工作正常。但我也需要那个。 如何做到这一点?

您可能需要使用 MIN 聚合函数的 GROUP BY 查询:

select
  invoiceno, name, addr1, addr2, min(id) as id
from
  invoice_table
group by
  invoiceno, name, addr1, addr2

或者,如果同一张发票可以有多个姓名 and/or 地址,您可以使用如下方式:

select t.invoiceno, t.name, t.addr1, t.addr2, t.id
from
  invoice_table t inner join (select invoiceno, min(id) as min_id) m
  on t.invoiceno=m.invoiceno and t.id=m.min_id

这将 return 每个 invoiceno 的第一个 ID(具有最低值的那个)。

如果你不想invoiceno重复,那么典型的方法是使用row_number():

select iv.*
from (select iv.*, row_number() over (partition by invoiceno order by id desc) as seqnum
      from invoice_table iv
     ) iv
where seqnum = 1 ;

您误解了 select distinct 的工作原理。它适用于 select 列表中的 all 列(和表达式),包括 id.

您似乎只希望每张发票有 1 个条目。 假设它是 SQL Server 2008 或更高版本

select * from (
select invoiceno,name,addr1,addr2,id, Row_number() over( partition by invoiceno order by invoiceno) RN from invoice_table
) a where Rn=1

DISTINCT 关键字适用于完整的结果集:invoiceno,name,addr1,addr2,id。第 2 行和第 3 行在 id 字段不同,因此它们将是不同的。

有两种选择:

  1. 跳过ID
  2. 使用子选择预过滤 ID-s

显然,您可以像这样跳过 id 字段:

select distinct invoiceno,name,addr1,addr2 from invoice_table;

分组有点棘手:

select invoiceno,name,addr1,addr2,id -- no distinct
from invoice_table
where id in (
    select min(id) from invoice_table
    group by invoiceno -- we have this group by + min instead of the distinct keyword
)

第二个可能会对你的表现产生负面影响,所以要小心。

SELECT * FROM invoice_table 其中 ID IN( SELECT 来自 invoice_table
的最小值(ID) GROUP BY invoiceno,name,addr1,addr2)