按统一顺序选择行
我有一个表 customer,其中有 100 行。表中为活动/非活动客户设置状态列。
我想要做的是按特定顺序从表中选择所有客户 即
前 7 个活跃客户,然后 3 个不活跃客户,再次 7 个活跃客户和 3 个不活跃客户,依此类推
我如何在查询中实现此目的。
我正在使用 sql server 2008 。请帮我....
I have a table customer and 100 rows in it. a status column is set for active/inactive customer in the table.
What i want to do is to select all customers from the table in a specific order
ie
first 7 active customers then 3 inactive customers again 7 active customers and 3 inactive and so on
How can I achieve this in a query.
I am using sql server 2008 . Please Help me....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据您到目前为止列出的最低要求,类似这样的事情应该可以做到:
显然,根据需要更改表和列名称。
PKID
被假定为表中的其他列,可以根据该列对各个行进行排序。AlteredOrdering CTE 中的神奇数字希望是显而易见的 - 7 和 3 来自问题,代表每个“组”中应出现的每个项目的数量。 10 是每个“组”的总大小。因此,如果 9 个处于活动状态,4 个处于非活动状态,则 CASE 表达式将如下所示:
Something like this should do it, based on the minimal requirements you've listed so far:
Obviously, altering the table and column names as appropriate.
PKID
is assumed to be some other column in the table against which the individual rows can be ordered.The magic numbers in the AlteredOrdering CTE are hopefully obvious - 7 and 3 come from the question, and represent how many of each item should appear in each "group". The 10 is the total size of each "group". So if it was 9 active, 4 inactive, the CASE expression would look like:
据我所知你不能。从 SQL 检索数据后,可能需要对其进行处理
As far as I am aware you cannot. It would likely require processing of the data once you had retrieved it from SQL
如果你只有 100 行,那么也许限制 x,y 与 union 组合可以帮助
if you have only 100 rows then perhaps limit x,y combine with union can help