向高手求一SQL语句...
比如有这样一个表
BillNo OrderNo
-----------------
以下两行记录的BillNo 和 OrderNo 值都一样
A00001 PO_0001
A00001 PO_0001
以下两行记录的BillNo相同, OrderNo不同
A00002 PO_0001
A00002 PO_0002
A00003 PO_0002
想查找出相同BillNo和OrderNo都相同记录的BillNo: A00001,A00003
[解决办法]
select distinct billno from T where exists(select id from T i where i.id>T.id and (i.billno=T.billno or i.orderno=T.orderno)
问题有些奇怪,但只要检测有满足条件的记录就行了,注意用primary key 对比避免比较同一条记录。
[解决办法]
表示看不明白楼主要表达的意思
[解决办法]
if object_id('[test]') is not null
drop table [test]
go
create table [test]
(
[BillNo] varchar(6),
[OrderNo] varchar(7)
)
insert [test]
select 'A00001','PO_0001' union all
select 'A00001','PO_0001' union all
select 'A00002','PO_0001' union all
select 'A00002','PO_0002' union all
select 'A00003','PO_0002'
go
;with t as(
select distinct BillNo,OrderNo from test
)
,temp as(
select COUNT(BillNo) over(partition by BillNo) CR
,BillNo,OrderNo
from t
)
select temp.BillNo,temp.OrderNo
from temp
where CR=1
BillNoOrderNo
A00001PO_0001
A00003PO_0002
create table [test]
(
[BillNo] varchar(6),
[OrderNo] varchar(7)
)
insert [test]
select 'A00001','PO_0001' union all
select 'A00001','PO_0001' union all
select 'A00002','PO_0001' union all
select 'A00002','PO_0002' union all
select 'A00003','PO_0002'
go
select BillNo
from test
group by BillNo,OrderNo
having COUNT(1)>1
if object_id('[test]') is not null
drop table [test]
go
create table [test]
(
[BillNo] varchar(6),
[OrderNo] varchar(7)
)
insert [test]
select 'A00001','PO_0001' union all
select 'A00001','PO_0001' union all
select 'A00002','PO_0001' union all
select 'A00002','PO_0002' union all
select 'A00003','PO_0002'
go
;with sel as(select distinct billno,orderno from test)
select billno from sel group by BillNo having count(1)=1