日期:2014-05-17  浏览次数:20581 次

向高手求一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
BillNo OrderNo
A00001 PO_0001
A00003 PO_0002

------解决方案--------------------
借下2楼的表结构:



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

------解决方案--------------------
create table [test] 
(     [BillNo] varchar(6),     
[OrderNo] varchar(7) ) 
go

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 distinct billno,OrderNo
from [test]
where billno in
(
select m.a
from ( select distinct billno a,OrderNo  b from [test] )m
group by a
having count(b)=1
)
------解决方案--------------------
select BillNo,OrderNo from Table where BillNo=OrderNo and( BillNo='A00001' or OrderNo ='A00003') 是吗?
------解决方案--------------------