再次求助!!!!!!!

sljz 2006-04-27 06:18:25
iddatetime idname
2006-4-27 15:00:00 Miller
2006-4-27 16:00:00 Miller
2006-4-27 15:00:00 Jerry
2006-4-27 16:00:00 Jerry
2006-4-28 16:00:00 Miller
2006-4-28 16:00:00 Jerry

现在需要列出每天最大记录和最小记录存成两张表

table A(最小记录)
iddatetime idname
2006-4-27 15:00:00 Miller
2006-4-27 15:00:00 Jerry
2006-4-28 16:00:00 Miller
2006-4-28 16:00:00 Jerry

table B(最大记录)
iddatetime idname
2006-4-27 16:00:00 Miller
2006-4-27 16:00:00 Jerry
2006-4-28 16:00:00 Miller
2006-4-28 16:00:00 Jerry

谢谢
...全文
138 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
xeqtr1982 2006-04-27
  • 打赏
  • 举报
回复
declare @t table(iddatetime datetime,idname varchar(10))
insert into @t select '2006-4-27 15:00:00' , 'Miller'
union all select '2006-4-27 16:00:00' , 'Miller'
union all select '2006-4-27 15:00:00' , 'Jerry'
union all select '2006-4-27 16:00:00' , 'Jerry'
union all select '2006-4-28 16:00:00' , 'Miller'
union all select '2006-4-28 16:00:00' , 'Jerry'

select * from @t a where not exists(select 1 from @t where convert(varchar(10),iddatetime,120)=convert(varchar(10),a.iddatetime,120) and convert(varchar,iddatetime,108)>convert(varchar,a.iddatetime,108))


select * from @t a where not exists(select 1 from @t where convert(varchar(10),iddatetime,120)=convert(varchar(10),a.iddatetime,120) and convert(varchar,iddatetime,108)<convert(varchar,a.iddatetime,108))
mugua604 2006-04-27
  • 打赏
  • 举报
回复
desc
WangZWang 2006-04-27
  • 打赏
  • 举报
回复
--取小
Insert into A(iddatetime,idname)
select iddatetime,idname
from tbl as a
where iddatetime=(select top 1 iddatetime from tbl where idname=a.idname
and datediff(day,a.iddatetime,iddatetime)=0 order by iddatetime)

--取大
Insert into B(iddatetime,idname)
select iddatetime,idname
from tbl as a
where iddatetime=(select top 1 iddatetime from tbl where idname=a.idname
and datediff(day,a.iddatetime,iddatetime)=0 order by iddatetime desc)
mugua604 2006-04-27
  • 打赏
  • 举报
回复
select top 1 iddatetime,idname into #tableA from 表1
group by iddatetime,idname
order by idname

select top 1 iddatetime,idname into #tableB from 表1
group by iddatetime,idname
order by idname dese

34,590

社区成员

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

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