日期:2014-05-18 浏览次数:20640 次
采购申请单号 采购单号 采购类型 PURCHASEAPPLYNO PURCHASENO PURCHASETYPE PA001 P001 外采 PA001 P002 内采 PA002 P003 外采 PA003 P004 内采
if object_id('tb') is not null drop table tb go create table tb ( PURCHASEAPPLYNO varchar(10), PURCHASENO varchar(10), PURCHASETYPE varchar(10) ) go insert into tb select 'PA001','P001','外采' union all select 'PA001','P002','内采' union all select 'PA002','P003','外采' union all select 'PA003','P004','内采' go select PURCHASEAPPLYNO,PURCHASENO,PURCHASETYPE from ( select *,row=row_number() over(partition by PURCHASEAPPLYNO order by getdate()) from tb )t where row=1 go /* PURCHASEAPPLYNO PURCHASENO PURCHASETYPE --------------- ---------- ------------ PA001 P001 外采 PA002 P003 外采 PA003 P004 内采 (3 行受影响) */
------解决方案--------------------
---------------------------- -- Author :fredrickhu(小F,向高手学习) -- Date :2011-12-20 16:15:31 -- Version: -- Microsoft SQL Server 2008 R2 (RTM) - 10.50.1617.0 (Intel X86) -- Apr 22 2011 11:57:00 -- Copyright (c) Microsoft Corporation -- Enterprise Evaluation Edition on Windows NT 6.1 <X64> (Build 7600: ) (WOW64) -- ---------------------------- --> 测试数据:[tb] if object_id('[tb]') is not null drop table [tb] go create table [tb]([PURCHASEAPPLYNO] varchar(5),[PURCHASENO] varchar(4),[PURCHASETYPE] varchar(4)) insert [tb] select 'PA001','P001','外采' union all select 'PA001','P002','内采' union all select 'PA002','P003','外采' union all select 'PA003','P004','内采' --------------开始查询-------------------------- select * from tb t where PURCHASENO=(select MIN(PURCHASENO) from tb where PURCHASEAPPLYNO=t.PURCHASEAPPLYNO) ----------------结果---------------------------- /* PURCHASEAPPLYNO PURCHASENO PURCHASETYPE --------------- ---------- ------------ PA001 P001 外采 PA002 P003 外采 PA003 P004 内采 (3 行受影响) */