出于广告目的移动/旋转 SQL Server 查询的输出

发布于 2024-12-09 03:05:04 字数 606 浏览 0 评论 0原文

我正在尝试创建一个查询,该查询将改变我网站上每个用户每次执行的结果。目标是使用 SQL Server 2008 数据库技术来实现这一目标。

例如,如果我有一个包含三行数据的表:

我试图找到一种方法来按名称升序选择此数据顺序,然后移动/旋转结果。

ID  Name 
1   Apple 
2   Orange 
3   Banana

用户 1 加载页面并看到

Apple
Banana
Orange

用户 1 重新加载同一页面,然后看到

Banana
Orange
Apple

用户 1 重新加载同一页面,然后看到

Orange
Apple
Banana

用户 2 等具有完全相同的体验,以相同的顺序旋转/移动结果。

我可以创建并存储任何必要的值,例如名字或姓氏或 ID。

是否有一个简单优雅的解决方案(查询、存储过程、函数等)可以在 SQL Server 级别完成此任务?

原因是让所有颜色都有机会显示在列表顶部,而不是按字母顺序 A 到 Z。

I am trying to create a query that will shift the results on each execution for each user on my website. Goal to accomplish this using SQL Server 2008 database technology.

For example if I have a table with three rows of data:

I am trying to find a way to select this data order by name ascending then shifting/rotating the result.

ID  Name 
1   Apple 
2   Orange 
3   Banana

User 1 loads a page and sees

Apple
Banana
Orange

User 1 reloads the same page and then sees

Banana
Orange
Apple

User 1 reloads the same page and then sees

Orange
Apple
Banana

User 2, etc have the exact same experience, rotating/shifting of the results in the same order.

I can create and store any values necessary such as the first or last name or ID.

Is there a simple elegant solution (query, stored procedure, function, etc) that will accomplish this task at the SQL Server level?

The reason being is to give all colors the opportunity to be displayed at the top of the list vs alphabetical ordered A to Z.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

就此别过 2024-12-16 03:05:04

如果 SQL Server 有序列和 nextval() 函数(如 PostgreSQL 或 Oracle),那么它可能就这么简单 - 假设您的基本排序是在 id 上,从 1 开始,没有漏洞:

SELECT id, name
  FROM mytbl
 ORDER BY CASE WHEN id > @nextval%number_of_rows THEN 0 ELSE 1 END, rn

请参阅此处了解解决方法: Oracle 序列,但随后在 MS SQL Server 中

使用问题中的新输入进行编辑

它可能看起来像这样:

DECLARE @nextval int;

WITH x AS
(
SELECT id
      ,name
      ,ROW_NUMBER() OVER(ORDER BY name) AS rn
      ,COUNT(*) OVER() AS ct
  FROM mytbl 
)
SELECT id
      ,name
  FROM x
 ORDER BY CASE WHEN rn > @nextval%ct THEN 0 ELSE 1 END, rn
  • 按字母顺序排序。
  • 动态行数。
  • 语法现已测试,
  • 您只需要一种递增@nextval 的方法。上面的链接可能有帮助。

为了让我的语法更清楚,我在 上编写了一个演示data.stackexchange.com
你可以去那里看看!

如果 SQL Server 有 LIMIT 和 OFFSET 之类的东西,这会更简单。 TOP 无法完成这项工作。请参阅:SQL Server 的 LIMIT 和 OFFSET 等效吗?

If SQL Server had sequences and a nextval() function like PostgreSQL or Oracle it could be as simple as that - given your base sort were on id, starting at 1, without holes:

SELECT id, name
  FROM mytbl
 ORDER BY CASE WHEN id > @nextval%number_of_rows THEN 0 ELSE 1 END, rn

See here for workarounds: Oracle sequence but then in MS SQL Server.

Edit with new input from question

It could look like this then:

DECLARE @nextval int;

WITH x AS
(
SELECT id
      ,name
      ,ROW_NUMBER() OVER(ORDER BY name) AS rn
      ,COUNT(*) OVER() AS ct
  FROM mytbl 
)
SELECT id
      ,name
  FROM x
 ORDER BY CASE WHEN rn > @nextval%ct THEN 0 ELSE 1 END, rn
  • Sort alphabetically.
  • Number of rows dynamic.
  • Syntax is tested now
  • You only need a way to increment @nextval. The above link may help.

To get my syntax straight, I wrote a demo on data.stackexchange.com
You can go there and check it out!

This would be simpler if SQL Server had something like LIMIT and OFFSET. TOP can't do the job. See: Equivalent of LIMIT and OFFSET for SQL Server?

乱了心跳 2024-12-16 03:05:04

我不知道 SQL Server 查询的移位或旋转输出,但我正在尝试做同样的事情,并且我使用 NEWID() 来随机排序我的输出。

select name,company,etc from tblCompany order by NEWID() desc

I don't know about shift or rotate output of SQL server query, but I'm trying to do it same you, and I use NEWID() to order random my outputs.

select name,company,etc from tblCompany order by NEWID() desc
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文