读书人

小弟我想查询出相同的cuno列对应的cad

发布时间: 2012-09-12 09:21:30 作者: rapoo

我想查询出相同的cuno列对应的cadt列中其最小的值,

我想查询出相同的cuno列对应的cadt列中其最小的值,
需求说明:账号表中有客户,账号和账号创建日期三个字段;其中一个客户下有许多账号,现在想查询出所有客户的创建日期;即它所属的账号的创建日期中最先的一个即为客户创建日期。

查询语句
客户 账号 账号创建日期
cuno cano cadt
001 afasdfasdf 20110101
001 dfdsddfifp 20120201
002 sergsfdsfs 20100101
002 wqdfsdfdf 20110101
003 ueoeijfadf 20100101
003 fyadssvvdf 20110101
003 fiadafasdf 20120201

得到的结果应该是:
001 afasdfasdf 20110101
002 sergsfdsfs 20100101
003 ueoeijfadf 20100101




[解决办法]
select * from tb as t
where not exists(select 1 from tb where 客户=t.客户 and 创建日期<t.创建日期)
[解决办法]

SQL code
select  *  from  T a where exists (select cuno ,min(cadt)  from T  group by cuno where a.cuno =cuno)
[解决办法]
SQL code
--0rselect * from tb as twhere 创建日期=(select min(创建日期) from tb where 客户=t.客户 )
[解决办法]
SQL code
select cuno,cano,cadt from tb twhere cadt=(select min(cadt) from tb where cuno=t.cuno )--orselect * from tb  twhere not exists(select 1 from tb where cuno=t.cuno  and cadt<t.cadt ) 

读书人网 >SQL Server

热点推荐