T-SQL 选择不同值的样本记录

发布于 2024-10-06 01:58:29 字数 489 浏览 5 评论 0原文

在 SQL Server 2008 中,我有表 Theory,其中包含列 Thesis 和 Class。

Thesis                   Class
<this is sample text>    1
<this is sample text>    2
<this is sample text>    2
<this is sample text>    3
<this is sample text>    3
<this is sample text>    1
<this is sample text>    3
<this is sample text>    1
<this is sample text>    1
<this is sample text>    2

我想编写将返回每个类的两条记录的选择语句。

非常感谢

In SQL server 2008 I have table Theory with columns Thesis and Class.

Thesis                   Class
<this is sample text>    1
<this is sample text>    2
<this is sample text>    2
<this is sample text>    3
<this is sample text>    3
<this is sample text>    1
<this is sample text>    3
<this is sample text>    1
<this is sample text>    1
<this is sample text>    2

I want to write select statement that will return two records of each class.

Many thanks

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

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

发布评论

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

评论(1

绻影浮沉 2024-10-13 01:58:29
;WITH T AS
(
SELECT Thesis, 
       Class, 
       ROW_NUMBER() OVER (PARTITION BY Class ORDER BY (SELECT 0) ) rn
FROM Theory 
)
SELECT Thesis, Class
FROM T 
WHERE rn <=2
;WITH T AS
(
SELECT Thesis, 
       Class, 
       ROW_NUMBER() OVER (PARTITION BY Class ORDER BY (SELECT 0) ) rn
FROM Theory 
)
SELECT Thesis, Class
FROM T 
WHERE rn <=2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文