SQL Server 选择子集中的顶部?
我有这样的数据:
Create Table #Some ( ForeignId Int , SubValue UniqueIdentifier , WasRead Bit)
Insert Into #Some Values ( 1 , 'ADEE70A8-E804-4A0C-93AD-00CCCB3DA7DA' , 0 )
Insert Into #Some Values ( 1 , '024EDED5-744B-497A-BFB8-06A0C62DE0AE' , 0 )
Insert Into #Some Values ( 1 , 'C3F255F9-3648-4D8D-8D84-0951BD2D0551' , 0 )
Insert Into #Some Values ( 1 , '25CE8E67-A263-409E-8E7F-1A814EF76524' , 0 )
Insert Into #Some Values ( 2 , '1B339D13-B580-4660-9642-0C0FA9C84D14' , 0 )
Insert Into #Some Values ( 2 , '579C5BD3-EE3A-4789-A57B-0FAD0F7227A2' , 0 )
Insert Into #Some Values ( 3 , '31D1E0F2-5F8A-451B-8114-14DB685E9F53' , 0 )
Insert Into #Some Values ( 3 , '52347A60-4156-401F-8570-15554DD905EF' , 0 )
Insert Into #Some Values ( 3 , '6ECD072E-BC43-4A4C-AACD-1A6C256DCFDE' , 0 )
Insert Into #Some Values ( 3 , '3264625D-94CE-4798-9E4B-2672EDE496BC' , 0 )
我需要为每个ForeignId选择前三行,将其WasPolled值更新为1,并发出多个结果集,但所有结果集的总大小不能超过8:
set:
1 , 'ADEE70A8-E804-4A0C-93AD-00CCCB3DA7DA'
1 , '024EDED5-744B-497A-BFB8-06A0C62DE0AE'
1 , 'C3F255F9-3648-4D8D-8D84-0951BD2D0551'
set:
2 , '1B339D13-B580-4660-9642-0C0FA9C84D14'
2 , '579C5BD3-EE3A-4789-A57B-0FAD0F7227A2'
set:
3 , '31D1E0F2-5F8A-451B-8114-14DB685E9F53'
3 , '52347A60-4156-401F-8570-15554DD905EF'
3 , '6ECD072E-BC43-4A4C-AACD-1A6C256DCFDE'
I have this data:
Create Table #Some ( ForeignId Int , SubValue UniqueIdentifier , WasRead Bit)
Insert Into #Some Values ( 1 , 'ADEE70A8-E804-4A0C-93AD-00CCCB3DA7DA' , 0 )
Insert Into #Some Values ( 1 , '024EDED5-744B-497A-BFB8-06A0C62DE0AE' , 0 )
Insert Into #Some Values ( 1 , 'C3F255F9-3648-4D8D-8D84-0951BD2D0551' , 0 )
Insert Into #Some Values ( 1 , '25CE8E67-A263-409E-8E7F-1A814EF76524' , 0 )
Insert Into #Some Values ( 2 , '1B339D13-B580-4660-9642-0C0FA9C84D14' , 0 )
Insert Into #Some Values ( 2 , '579C5BD3-EE3A-4789-A57B-0FAD0F7227A2' , 0 )
Insert Into #Some Values ( 3 , '31D1E0F2-5F8A-451B-8114-14DB685E9F53' , 0 )
Insert Into #Some Values ( 3 , '52347A60-4156-401F-8570-15554DD905EF' , 0 )
Insert Into #Some Values ( 3 , '6ECD072E-BC43-4A4C-AACD-1A6C256DCFDE' , 0 )
Insert Into #Some Values ( 3 , '3264625D-94CE-4798-9E4B-2672EDE496BC' , 0 )
I need to select the first three rows for each ForeignId, update their WasPolled value to 1, and emit multiple result sets, but the total size for all sets cannot be more than 8:
set:
1 , 'ADEE70A8-E804-4A0C-93AD-00CCCB3DA7DA'
1 , '024EDED5-744B-497A-BFB8-06A0C62DE0AE'
1 , 'C3F255F9-3648-4D8D-8D84-0951BD2D0551'
set:
2 , '1B339D13-B580-4660-9642-0C0FA9C84D14'
2 , '579C5BD3-EE3A-4789-A57B-0FAD0F7227A2'
set:
3 , '31D1E0F2-5F8A-451B-8114-14DB685E9F53'
3 , '52347A60-4156-401F-8570-15554DD905EF'
3 , '6ECD072E-BC43-4A4C-AACD-1A6C256DCFDE'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不知道你有没有主键。如果您确实使用它进行连接。我假设ForeignId + SubValue是唯一的
I don't know if you have a primary key. If you do use it for the join. I assumed that ForeignId + SubValue is unique
这就是更新前三行的方式 - 您需要提供一种对数据进行排序的方法:
“发出多个结果集,但所有结果集的总大小不能超过 8:” - 嗯?!你所说的“发射”是什么意思。选择意味着单独声明:
Non-CTE 版本:
CTE 版本:
This is how you'd update the first three rows - you need to provide a means of ordering the data:
"emit multiple result sets, but the total size for all sets cannot be more than 8:" - Huh?! What do you mean by "emit". Selection means a separate statement:
Non-CTE version:
CTE version:
使用排名函数,特别是
ROW_NUMBER
限制返回结果的数量。Use the Ranking Functions, particularly
ROW_NUMBER
to limit the number of results returned.