繁体   English   中英

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

[英]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

您需要将表与自身连接起来,所有列都相等,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

我想获取所有列都具有相同值的所有行,但列 Id 除外。

使用窗口函数:

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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM