SQL - Top 1 selection with Order by?

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

The stored procedure I'm writing won't let me 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" - This is invalid in the ORDER BY clause because it is not contained in either an aggregate function or GROUP BY clause.

What am I doing wrong?

[Edit] There is no command instruction here. SQL2005.

Here's another 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

      

0


a source to share


8 answers


The problem was in another part of the stored procedure. I have used counter (*) elsewhere and I need a group. Thanks for the help.



+1


a source


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



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


a source


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

Why not try SELECT MAX (..

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

      

+2


a source


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

+1


a source


To be honest, the only thing I see wrong is that @dtTopDate = / = @dtLatestDate Also, there is no GROUP BY clause in your SQL statement .

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

      

+1


a source


Try to define the columns correctly to avoid any ambiguity or x-database architecture issues.

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 a pseudonym

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

      

+1


a source


Try SELECT @dtLatestDate = TOP 1 date_build ...

0


a source


if you want to get very complex, in T-SQL you can try using row_number () method and inner select:

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;

      

0


a source







All Articles