在线等待:求助SQL记录集获取怎么写

pingo888 2007-09-12 04:39:48
Table1结构及参考数据如下
-------------------------
ID1 ID2 PrjName CorpName TableDate
129258022 321 工程1 单位1 2007-9-01
10949498386 123 工程2 单位1 2007-9-02
0709109494983 213 工程1 单位1 2007-9-02
321546646 432 工程3 单位2 2007-9-08
467894664697 324 工程2 单位1 2007-9-08
3231454 532 工程1 单位1 2007-9-10
--------------------------
注:ID1、ID2为不规则生成的不重复字符串。

得出结果要求:同一工程(PrjName)中日期最新(TableDate)的一条记录的记录集,记录列表必须包括(ID1、ID2、PrjName、TableDate)字段。

想要的得出结果:
---------------------------------------------------
ID1 ID2 PrjName CorpName TableDate
3231454 532 工程1 单位1 2007-9-10
467894664697 324 工程2 单位1 2007-9-08
321546646 432 工程3 单位2 2007-9-08
---------------------------------------------------
请各位大侠指教,谢谢!
...全文
181 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
pingo888 2007-09-12
  • 打赏
  • 举报
回复
测试发现可以,多谢各位大虾帮助。
稍后结帖,看有其它相关问题再问问大侠不,^_^
divmedia 2007-09-12
  • 打赏
  • 举报
回复
select t.* from Table1 t where ID1=(select top 1 ID1 from Table1 where PrjName=t.PrjName order by TableDate desc)
OracleRoob 2007-09-12
  • 打赏
  • 举报
回复
select *
from 表名 as t
where TableDate = (select max(TableDate) from 表名 where PrjName=t.PrjName)

--或

select *
from 表名 as t
where not exists (select * from 表名 where PrjName = t.PrjName and TableDate>t.TableDate)


--或

select *
from 表名 as t
where TableDate = (select top 1 TableDate from 表名 where PrjName=t.PrjName order by TableDate desc)

--或
select a.*
from 表名 as a
inner join (select PrjName,max(TableDate) as TableDate from 表名 group by PrjName) as b on a.PrjName=t.PrjName and a.TableDate=b.TableDate

子陌红尘 2007-09-12
  • 打赏
  • 举报
回复
select t.* from Table1 t where ID1=(select top 1 ID1 from Table1 where PrjName=t.PrjName order by TableDate desc)
OracleRoob 2007-09-12
  • 打赏
  • 举报
回复

select *
from 表名 as t
where TableDate = (select max(TableDate) from 表名 where PrjName=t.PrjName)


--或

select *
from 表名 as t
where not exists (select * from 表名 where PrjName = t.PrjName and TableDate>t.TableDate)


--或

select *
from 表名 as t
where TableDate = (select top 1 TableDate from 表名 where PrjName=t.PrjName order by TableDate desc)
OracleRoob 2007-09-12
  • 打赏
  • 举报
回复
select *
from 表名 as t
where not exists (select * from 表名 where PrjName = t.PrjName and TableDate>t.TableDate)
子陌红尘 2007-09-12
  • 打赏
  • 举报
回复
select t.* from Table1 t where not exists(select 1 from Table1 where PrjName=t.PrjName and TableDate>t.TableDate)
OracleRoob 2007-09-12
  • 打赏
  • 举报
回复
select *
from 表名 as t
where TableDate = (select max(TableDate) from 表名 where PrjName=t.PrjName)

34,838

社区成员

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

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