SQL - 使用Order by选择Top 1?

时间:2022-02-15 11:15:51

This was resolved. The statement was in another part of the stored procedure.

这已经解决了。该声明是存储过程的另一部分。

The stored procedure I'm writing won't allow me to do this:

我写的存储过程不允许我这样做:

 declare @dtTopDate datetime
  select top 1 @dtTopDate  = date_build
    from database..table
   where database..table.parent = @Parent
     and database..table.child = @Child
   order by date_build desc

Gives me this error:

给我这个错误:

Column "database..table.date_build" is invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause.

“database..table.date_build”列在ORDER BY子句中无效,因为它不包含在聚合函数或GROUP BY子句中。

What am I doing wrong?

我究竟做错了什么?

[Edit] There is no group by statement here. SQL2005.

[编辑]这里没有分组声明。 SQL2005。

Here is some more context:

这是一些更多的背景:

if @Notify = 0
begin
     declare @dtTopDate datetime
      select top 1 @dtTopDate  = date_build
        from database..table
       where database..table.parent = @Parent
         and database..table.child = @Child
       order by date_build desc

      insert 
        into database2..table
             (parent, child, notification_date, change_date)
      values (@Parent, @Child, @dtTopDate, getdate())
     return 
end

8 个解决方案

#1


This works for me, but I'm not sure if this is what you are trying to do b/c your example has some errors.

这对我有用,但我不确定这是否是你想要做的事情b / c你的例子有一些错误。

use Test
go
CREATE TABLE [dbo].[MyTable]
(
    [MyTableId] [uniqueidentifier] NOT NULL,
    [MyDate] [datetime] NOT NULL,
    CONSTRAINT [PK_MyTable] PRIMARY KEY CLUSTERED([MyTableId] ASC,[MyDate] ASC)
)
GO
CREATE PROCEDURE ProcTopDate
(
    @MyDate datetime OUT
)
AS
BEGIN
    SET NOCOUNT ON;

    SELECT TOP 1
        @MyDate = [MyDate]
    FROM [Test].[dbo].[MyTable]
    order by MyDate
END
GO

insert into MyTable(MyTableId, MyDate)
values(newid(), getdate())
go

declare @MyDate datetime
exec ProcTopDate @MyDate OUT
print @MyDate

#2


Instead of SELECT TOP 1 ... ORDER BY ...

而不是SELECT TOP 1 ... ORDER BY ...

Why not try SELECT MAX( ..

为什么不试试SELECT MAX(..

DECLARE @dtTopDate datetime
SELECT @dtTopDate = MAX(date_build)
 from database..table 
 where database..table.parent = @Parent
 and database..table.child = @Child 

#3


What version of SQL are you using? It works fine for me on MS SQL Server 2005 (ionce I fix the declaration).

您使用的是哪个版本的SQL?它在MS SQL Server 2005上运行正常(我修复了声明)。

#4


Honestly the only thing I can see wrong is that @dtTopDate =/= @dtLatestDate Apart from that, there is no GROUP BY clause in your SQL statement.

老实说,我唯一看错的是@dtTopDate = / = @dtLatestDate除此之外,你的SQL语句中没有GROUP BY子句。

I just ran this and it worked fine.

我刚刚运行它,它工作正常。

declare @OrderDate datetime

select top 1 @OrderDate = OrderDate
from Orders
where Orders.CustomerID = 'ALFKI'
 and Orders.EmployeeID = 4
order by OrderDate desc

SELECT @OrderDate

#5


Try qualifying the columns correctly to avoid any ambiguities or x-database schema issue

尝试正确限定列以避免任何歧义或x数据库架构问题

declare @dtTopDate datetime

select top 1 
    @dtTopDate  = [database]..[table].date_build
FROM
    [database]..[table]
where
    [database]..[table].parent = @Parent
    and [database]..[table].child = @Child
order by
    [database]..[table].date_build desc

Or alias it

或别名

declare @dtTopDate datetime

select top 1
    @dtTopDate  = foo.date_build
FROM
    [database]..[table] foo
where
    foo.parent = @Parent
    and foo.child = @Child
order by
    foo.date_build desc

#6


The problem was in another part of the stored procedure. I was using a count(*) elsewhere and it required a group by. Thanks for the help.

问题出在存储过程的另一部分。我在其他地方使用了一个计数(*),它需要一个分组。谢谢您的帮助。

#7


Try SELECT @dtLatestDate = TOP 1 date_build...

尝试SELECT @dtLatestDate = TOP 1 date_build ...

#8


if you want to get really tricky, in T-SQL you can try using the row_number() method and an inner select:

如果你想变得非常棘手,在T-SQL中你可以尝试使用row_number()方法和内部选择:

select * from
(
    select 
        db.groupId
        , db.date_build
        , date_build_rank = row_number() over ( partition by db.groupId order by db.date_build desc)
    from
        #date_build_tbl db
) as a 
where a.date_build_rank < 2;

#1


This works for me, but I'm not sure if this is what you are trying to do b/c your example has some errors.

这对我有用,但我不确定这是否是你想要做的事情b / c你的例子有一些错误。

use Test
go
CREATE TABLE [dbo].[MyTable]
(
    [MyTableId] [uniqueidentifier] NOT NULL,
    [MyDate] [datetime] NOT NULL,
    CONSTRAINT [PK_MyTable] PRIMARY KEY CLUSTERED([MyTableId] ASC,[MyDate] ASC)
)
GO
CREATE PROCEDURE ProcTopDate
(
    @MyDate datetime OUT
)
AS
BEGIN
    SET NOCOUNT ON;

    SELECT TOP 1
        @MyDate = [MyDate]
    FROM [Test].[dbo].[MyTable]
    order by MyDate
END
GO

insert into MyTable(MyTableId, MyDate)
values(newid(), getdate())
go

declare @MyDate datetime
exec ProcTopDate @MyDate OUT
print @MyDate

#2


Instead of SELECT TOP 1 ... ORDER BY ...

而不是SELECT TOP 1 ... ORDER BY ...

Why not try SELECT MAX( ..

为什么不试试SELECT MAX(..

DECLARE @dtTopDate datetime
SELECT @dtTopDate = MAX(date_build)
 from database..table 
 where database..table.parent = @Parent
 and database..table.child = @Child 

#3


What version of SQL are you using? It works fine for me on MS SQL Server 2005 (ionce I fix the declaration).

您使用的是哪个版本的SQL?它在MS SQL Server 2005上运行正常(我修复了声明)。

#4


Honestly the only thing I can see wrong is that @dtTopDate =/= @dtLatestDate Apart from that, there is no GROUP BY clause in your SQL statement.

老实说,我唯一看错的是@dtTopDate = / = @dtLatestDate除此之外,你的SQL语句中没有GROUP BY子句。

I just ran this and it worked fine.

我刚刚运行它,它工作正常。

declare @OrderDate datetime

select top 1 @OrderDate = OrderDate
from Orders
where Orders.CustomerID = 'ALFKI'
 and Orders.EmployeeID = 4
order by OrderDate desc

SELECT @OrderDate

#5


Try qualifying the columns correctly to avoid any ambiguities or x-database schema issue

尝试正确限定列以避免任何歧义或x数据库架构问题

declare @dtTopDate datetime

select top 1 
    @dtTopDate  = [database]..[table].date_build
FROM
    [database]..[table]
where
    [database]..[table].parent = @Parent
    and [database]..[table].child = @Child
order by
    [database]..[table].date_build desc

Or alias it

或别名

declare @dtTopDate datetime

select top 1
    @dtTopDate  = foo.date_build
FROM
    [database]..[table] foo
where
    foo.parent = @Parent
    and foo.child = @Child
order by
    foo.date_build desc

#6


The problem was in another part of the stored procedure. I was using a count(*) elsewhere and it required a group by. Thanks for the help.

问题出在存储过程的另一部分。我在其他地方使用了一个计数(*),它需要一个分组。谢谢您的帮助。

#7


Try SELECT @dtLatestDate = TOP 1 date_build...

尝试SELECT @dtLatestDate = TOP 1 date_build ...

#8


if you want to get really tricky, in T-SQL you can try using the row_number() method and an inner select:

如果你想变得非常棘手,在T-SQL中你可以尝试使用row_number()方法和内部选择:

select * from
(
    select 
        db.groupId
        , db.date_build
        , date_build_rank = row_number() over ( partition by db.groupId order by db.date_build desc)
    from
        #date_build_tbl db
) as a 
where a.date_build_rank < 2;