简单问题不简单

tomyuansir 2008-04-30 05:38:17

declare @a table (id int identity(1,1) ,x int,y int,shopid int)
insert @a
select 1,1,1
union all
select 1,2,1
union all
select 2,1,1
union all
select 2,2,1
union all
select 2,5,2
union all
select 1,8,3
union all
select 1,7,3
union all
select 3,2,4
union all
select 3,1,4
union all
select 3,3,4
union all
select 4,1,4
union all
select 4,3,4
union all
select 4,2,4


select * from @a
需要结果:
x y shopid id
1 1 1 1
1 2 1 2
2 1 1 3
2 2 1 4
2 5 2 1
1 8 3 2
1 7 3 1
3 2 4 5
3 1 4 4
3 3 4 6
4 1 4 1
4 3 4 3
4 2 4 2
1>找出数据的规律
2>实现如上效果






...全文
58 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Larry 2008-04-30
  • 打赏
  • 举报
回复
select a.*,b.rnk from @a as a
inner join
(select x,y,shopid,rank() over(partition by shopid order by x,y) as rnk from @a) as b
on a.x = b.x and a.y = b.y and a.shopid = b.shopid order by a.shopid,a.x,a.id
-晴天 2008-04-30
  • 打赏
  • 举报
回复
select x,y,shopid,id=id-(select count(*) from @a where shopid<=a.shopid-1)+case when shopid=4 and x=3 then 3 else 0 end+case when shopid=4 and x=4 then -3 else 0 end from @a a order by shopid,x,id,y
/*
x y shopid id
----------- ----------- ----------- -----------
1 1 1 1
1 2 1 2
2 1 1 3
2 2 1 4
2 5 2 1
1 8 3 1
1 7 3 2
3 2 4 4
3 1 4 5
3 3 4 6
4 1 4 1
4 3 4 2
4 2 4 3

(13 行受影响)

*/
-晴天 2008-04-30
  • 打赏
  • 举报
回复

select x,y,shopid,k=id-(select count(*) from @a where shopid<=a.shopid-1) from @a a order by shopid,x,id,y
/*
x y shopid k
----------- ----------- ----------- -----------
1 1 1 1
1 2 1 2
2 1 1 3
2 2 1 4
2 5 2 1
1 8 3 1
1 7 3 2
3 2 4 1
3 1 4 2
3 3 4 3
4 1 4 4
4 3 4 5
4 2 4 6
*/

??????
-晴天 2008-04-30
  • 打赏
  • 举报
回复
xhopid,x,y,id排序

34,594

社区成员

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

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