Sql 如何获得除一列以外所有值都相同的两行
我想获得除列Id之外所有列都具有相同值的所有行。我尝试使用where语句进行此操作,但仍然可以看到所有其他行Sql 如何获得除一列以外所有值都相同的两行,sql,ssms,Sql,Ssms,我想获得除列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_custo
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
您需要将表本身连接为all columns EQUALE和id not EQUALE,如下例所示
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 int,
col1 int,
col2 int
)
选择t1*
从测试t1开始
在t1.col1=t2.col1和t1.col2=t2.col2和t1.id t2.id上连接测试t2
我想获得除列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;