¿Resultados de orden SQL y luego extraer más de una columna?

I am writing a SQL function. I would like to order all of the data from a table (any table, doesnt matter for this question). I would then like to order the data on one column. Then once this order has been performed I would like to extract a few of the columns from the top row (after the ordering).

I dont think I can use partititon by because the ordering is on the whole data, not groups of it?

Im stuck because you can only use ORDER BY at the end of a function

preguntado el 12 de junio de 12 a las 13:06

What are you trying to do exactly? Is it a table valued funtion? -

2 Respuestas

Sounds like a complicated description of a SELECT TOP 1 consulta:

SELECT TOP 1 t.ColumnA, t.ColumnD, t.ColumnF
FROM YourTable t
ORDER BY t.ColumnB DESC,

Would order the table by ColumnB in descending order, then return Columns A, D and F.

I'm not sure you even need a function.

Respondido el 12 de junio de 12 a las 13:06

Intente algo como esto:

CREATE FUNCTION fn_dummy()
RETURNS TABLE
AS
RETURN (

SELECT TOP 1
    name, object_id, type_desc
FROM sys.tables
ORDER BY create_date DESC)

Respondido el 12 de junio de 12 a las 13:06

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.