如何获得除了一列之外所有值都相同的两行

How to get two rows with all values the same except for one column

我想获取所有列都具有相同值的所有行,列 ID 除外。我尝试使用 where 语句来完成它,但我仍然可以看到所有其他行。

SELECT 
a.strBedrijf,a.IdLeverancier,a.strLevFactNr, a.Id, a.dtmFactuur, a.fBedragInc, be.bDeleted,

'https://documents.blabla/' + (select top(1) textval from tblsettings where id='Workflow_customerid') + '/?p=PurchaseInvoiceDetails&Id=' + a.id  as Url,

case when be.bDeleted = 'JA' then 'NEE'

when be.bDeleted = 'NEE' then 'JA' end as AdministratieActief

FROM   tblfacturen A, tblfacturen B

        join tblBedrijven be on strBedrijf=be.Id

WHERE

be.bDeleted = 'NEE'  and

A.idleverancier = B.idleverancier

    AND A.strLevFactNr=B.strLevFactNr

    AND A.dtmFactuur=B.dtmFactuur

 AND A.fBedragInc=B.fBedragInc

   AND A.ID != B.ID

AND a.bDeleted ='NEE'

AND b.bDeleted ='NEE'

and a.strlevfactnr not like 'corr%'

group by A.strBedrijf,a.IdLeverancier,a.strLevFactNr, a.Id, a.dtmFactuur,     a.fBedragInc, be.bDeleted

这是我的输出:

L014909 227330  e2f02668-b1ac-416f-809d-a9a100c689c2    2018-11-23 00:00:00.000 288 NEE JA
L021960 24614767    48cf2ed2-160f-43a2-8bb0-a9a0006f8cf1    2018-11-16 00:00:00.000 232,05  NEE JA
L1630   297373  4200e599-f7ec-45fb-a003-a3570096289c    2014-06-24 00:00:00.000 484,75  NEE JA
L1630   297373  bfccef8d-dccb-4263-bcc7-a355006073de    2014-06-24 00:00:00.000 484,75  NEE JA
L017875 3493813 8e112901-13ea-4ed3-abf8-a9b200756b98    2018-12-07 00:00:00.000 1832,47 NEE JA

但我想要这样的输出:

L1630   297373  4200e599-f7ec-45fb-a003-a3570096289c    2014-06-24 00:00:00.000 484,75  NEE JA
L1630   297373  bfccef8d-dccb-4263-bcc7-a355006073de    2014-06-24 00:00:00.000 484,75  NEE JA

您需要将 table 与其自身连接起来,所有列都相等且 ID 不相等,如下例所示

create table test (
    id int,
    col1 int,
    col2 int
)

SELECT t1.*
FROM test t1
JOIN test t2 ON t1.col1 = t2.col1 AND t1.col2 = t2.col2 AND t1.id <> t2.id

i would like to obtain all rows where all columns have the same value, except for the column Id.

使用window函数:

select t.*
from (select t.*, count(*) over (partition by col1, col2) as cnt  -- list all columns here
      from t
     ) t
where cnt >= 2
order by col1, col2;