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

luoxp520 2012-07-30 03:52:19

我想查询出相同的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


...全文
91 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
以学习为目的 2012-07-30
  • 打赏
  • 举报
回复

select cuno,cano,cadt from tb t
where cadt=(select min(cadt) from tb where cuno=t.cuno )
--or

select * from tb t
where not exists(select 1 from tb where cuno=t.cuno and cadt<t.cadt )
筱筱澄 2012-07-30
  • 打赏
  • 举报
回复
--0r
select * from tb as t
where 创建日期=(select min(创建日期) from tb where 客户=t.客户 )
xuam 2012-07-30
  • 打赏
  • 举报
回复
select  *  from  T a where exists (select cuno ,min(cadt)
from T group by cuno where a.cuno =cuno)
筱筱澄 2012-07-30
  • 打赏
  • 举报
回复
select * from tb as t
where not exists(select 1 from tb where 客户=t.客户 and 创建日期<t.创建日期)

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧