求一SQL

meixiaofeng 2005-09-12 12:05:47
testID testID1
1 2 1 2 1
1 2 1 2 2
2 2 1 2 3
1 2 变成 2 2 1
3 3 3 3 1
4 5 4 5 1
4 6 4 6 1


还有个问题,求四个日期中的最大值(四个日期为一行数据中的四个字段)
...全文
112 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
vivianfdlpw 2005-09-12
  • 打赏
  • 举报
回复
求四个日期中的最大值(四个日期为一行数据中的四个字段)
=================>
可以建立一个函数来求最大值,或者使用case...end不过语句比较长


vivianfdlpw 2005-09-12
  • 打赏
  • 举报
回复
declare @tb table
(
testID int,
testID1 int
)
insert @tb
select 1,2 union all
select 1,2 union all
select 2,2 union all
select 1,2 union all
select 3,3 union all
select 4,5 union all
select 4,6

select identity(int,1,1) as ID
,*
into #
from @tb

--查询
select testID
,testID1
,(select count(1)
from #
where testID=t.testID
and testID1=t.testID1
and ID<=t.ID
) as [order]
from # t
order by testID,testID1,ID

--结果
/*
testID testID1 order
----------- ----------- -----------
1 2 1
1 2 2
1 2 3
2 2 1
3 3 1
4 5 1
4 6 1

(所影响的行数为 7 行)
*/
vivianfdlpw 2005-09-12
  • 打赏
  • 举报
回复
select identity(int,1,1) as ID
,*
into #
from 表

--查询
select testID
,testID1
,(select count(1)
from #
where testID=t.testID
and testID1=t.testID
and ID<=t.ID
) as [order]
from # t
order by testID,testID1,ID
funsuzhou 2005-09-12
  • 打赏
  • 举报
回复
学习!
anchky 2005-09-12
  • 打赏
  • 举报
回复
学习
meixiaofeng 2005-09-12
  • 打赏
  • 举报
回复
不用临时表

34,588

社区成员

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

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