如何从表中获取几个最高值?
我有一张像这样的表
id f1
--------------
1 2000-01-01
1 2001-01-01
1 2002-01-01
1 2003-01-01
,我想知道一行中的最新 3 个日期
CREATE TABLE Test
(
id INT NOT NULL,
f1 DATETIME NOT NULL,
)
INSERT INTO Test (id, f1) VALUES (1, '1/1/2000')
INSERT INTO Test (id, f1) VALUES (1, '1/1/2001')
INSERT INTO Test (id, f1) VALUES (1, '1/1/2002')
INSERT INTO Test (id, f1) VALUES (1, '1/1/2003')
SELECT T1.* FROM Test as T1
正在尝试类似的事情
SELECT T1.*,T2.*
FROM Test AS T1
LEFT OUTER JOIN Test AS T2 ON T1.id = T2.id AND (T2.f1 > T1.f1)
I have a table like
id f1
--------------
1 2000-01-01
1 2001-01-01
1 2002-01-01
1 2003-01-01
And I want to get say the latest 3 dates in one row
CREATE TABLE Test
(
id INT NOT NULL,
f1 DATETIME NOT NULL,
)
INSERT INTO Test (id, f1) VALUES (1, '1/1/2000')
INSERT INTO Test (id, f1) VALUES (1, '1/1/2001')
INSERT INTO Test (id, f1) VALUES (1, '1/1/2002')
INSERT INTO Test (id, f1) VALUES (1, '1/1/2003')
SELECT T1.* FROM Test as T1
Was trying something like
SELECT T1.*,T2.*
FROM Test AS T1
LEFT OUTER JOIN Test AS T2 ON T1.id = T2.id AND (T2.f1 > T1.f1)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
虽然我不确定如何将它们放入单行,但您可以从以下开始:
这应该会给您一个类似的结果:
不过,将它们放入单行可能会有点困难......
Although I'm not sure how to get them into a single row, you could start with:
That should give you a result like:
Putting them into a single row, though, may be a bit more difficult...
在 sql server 中,您可以执行
select top 3 * from Test order by f1 desc
。其他 DBMS 也有类似的可能性,例如 MySql 的limit
、Oracle 的rownum
等。In sql server you could do
select top 3 * from Test order by f1 desc
. Other DBMS's have similar posibilities such as MySql'slimit
, Oracle'srownum
etc.至少在 SQL Server 上,您可以结合使用
ORDER BY
、TOP
和PIVOT
来实现此目的。似乎许多其他答案都忽略了结果“全部在一行”的需要。You can do this with a combination of
ORDER BY
,TOP
andPIVOT
, at least on SQL Server. It seems that many of the other answers have ignored the need for the result to be "all on one row."在 T-SQL 中(这将为您提供前三个日期,即使它们都是相同的值)
这将为您提供前三个不同的日期
在 SQL Server 2005 中尝试此操作
in T-SQL (This will get you the top three dates even if they are all the same value)
This will get you the top three DISTINCT dates
Try this in SQL Server 2005
您想要做的事情称为数据透视表,这里有一篇关于如何执行此操作的文章:
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::NO::P11_QUESTION_ID:766825833740
也,如果你使用 Oracle,我会查看分析函数,特别是 OVER PARTITION BY
What you are trying to do is called pivot table, here is an article on how to do it:
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::NO::P11_QUESTION_ID:766825833740
also, if you use Oracle I would look at analytic functions, OVER PARTITION BY in particular
这又如何呢?
哪个输出:
唯一的缺点是您至少需要三行,否则它不会返回任何内容...
使用
JOIN
,您可以执行以下操作:它将返回类似以下内容:
缺点是
date1
、date 2
和date 3
不一定按特定顺序(根据上面的示例输出)。但这可以通过编程来实现。好的一面是,您可以在GROUP BY
之前插入WHERE
子句,并且可以按T1.id
进行搜索。What about this ?
Which outputs :
The only downside is that you need at least three rows, otherwise it won't return anything...
Using
JOIN
, you can do this :Which will return something like :
The downside is that
date1
,date 2
anddate 3
will not necessarily be in a specific order (as per the above sample output). But this can be achieved programatically. The plus side is that you can insert aWHERE
clause before theGROUP BY
and you can search byT1.id
, for example.您可以通过旋转表格来获取连续前 3 个日期,如果您希望在旋转后将它们放在一列中,则可以将日期连接起来。
编辑 :
下面是一个查询,用于对表进行透视并提供连续的最新 3 个日期。但要进行透视,您需要了解表中可用的数据。我想既然我们正在查询最新的 3 个日期,我们将不知道围绕日期列旋转的确切值。首先,我将最新的 3 个日期查询到临时表中。然后对 row_number 1、2 和 3 运行透视以获取连续的最新 3 个日期。
现在,在 row_number 列上进行透视 -
这会导致 -
当然,
You can get top 3 dates in a row by pivoting the table and perhaps concatenating the dates if you want them in one column after pivoting.
Edit :
here is a query to pivot the table and provide the latest 3 dates in a row. But to pivot you would need to know the data that is available in the table. I figured since we are querying for the latest 3 dates we will not know the exact vales to pivot around the date column. So First, I queried the latest 3 dates into a temp table. Then ran a pivot on the row_number 1, 2 and 3 to obtain the latest 3 dates in a row.
Now, pivot on the row_number column -
This results in -
And, of course