I need to order data by two columns, How do i do that?
This is my table:
Name | ImpFile | ImpTime
Sam Imp01 2012-05-16 09:54:02.477
Ann Imp01 2012-05-16 09:54:02.478
Mark Imp01 2012-05-16 09:54:02.477
John Import12 2012-05-16 09:55:37.384
Bart Import12 2012-05-16 09:55:37.387
Sasha Import12 2012-05-16 09:55:37.385
I need to sort this table by ImpTime and ImpName and it should look like this:
Name | ImpFile | ImpTime
Import12 2012-05-16 09:55:37.387
Bart Import12 2012-05-16 09:55:37.387
John Import12 2012-05-16 09:55:37.384
Sasha Import12 2012-05-16 09:55:37.385
Imp01 2012-05-16 09:54:02.478
Ann Imp01 2012-05-16 09:54:02.478
Mark Imp01 2012-05-16 09:54:02.477
Sam Imp01 2012-05-16 09:54:02.477
I am using this query, but it does not order the table by Name, in only orders by name when the time is the same value for multiple rows.
select Name, ImpFile, ImpTime
from people
union
select distinct '', ImpFile, max(ImpTime)
from people
group by ImpFile
order by ImpTime desc, Name
This query gives me table like this:
Name | ImpFile | ImpTime
Import12 2012-05-16 09:55:37.387
John Import12 2012-05-16 09:55:37.384
Bart Import12 2012-05-16 09:55:37.387
Sasha Import12 2012-05-16 09:55:37.385
Imp01 2012-05-16 09:54:02.478
Sam Imp01 2012-05-16 09:54:02.477
Ann Imp01 2012-05-16 09:54:02.478
Mark Imp01 2012-05-16 09:54:02.477
Is there any way to order by those two column at the same time?
EDIT
What happens when I use order by ImpFile DESC, ImpTime desc
?
It gives me a result table like this:
Name | ImpFile | ImpTime
Import12 2012-05-16 09:55:37.387
Imp01 2012-05-16 09:54:02.478
Bart Import12 2012-05-16 09:55:37.387
John Import12 2012-05-16 09:55:37.384
Sasha Import12 2012-05-16 09:55:37.385
Ann Imp01 2012-05-16 09:54:02.478
Mark Imp01 2012-05-16 09:54:02.477
Sam Imp01 2012-05-16 09:54:02.477
Why can you just do it like this:
order by ImpFile DESC, ImpTime desc
No it do not result in what you are showing. It result in this:
Import12 2012-05-16 09:55:37.387
Bart Import12 2012-05-16 09:55:37.387
Sasha Import12 2012-05-16 09:55:37.387
John Import12 2012-05-16 09:55:37.383
Imp01 2012-05-16 09:54:02.477
Ann Imp01 2012-05-16 09:54:02.477
Mark Imp01 2012-05-16 09:54:02.477
Sam Imp01 2012-05-16 09:54:02.477
See here for an example
EDIT
I have a suggestion for you. Maybe something like this:
Test data
DECLARE @T TABLE(Name VARCHAR(100),ImpFile VARCHAR(100),ImpTime DATETIME)
INSERT INTO @T
([Name], [ImpFile], [ImpTime])
VALUES
('Sam', 'Imp01', '2012-05-16 09:54:02.477'),
('Ann', 'Imp01', '2012-05-16 09:54:02.478'),
('Mark', 'Imp01', '2012-05-16 09:54:02.477'),
('John', 'Import12', '2012-05-16 09:55:37.384'),
('Bart', 'Import12', '2012-05-16 09:55:37.387'),
('Sasha', 'Import12', '2012-05-16 09:55:37.385');
Query
;WITH CTE
AS
(
SELECT
ROW_Number() OVER(PARTITION BY t.[ImpFile]
ORDER BY t.[ImpTime] DESC) AS RowNbr,
'' AS Name,
t.ImpFile,
t.[ImpTime]
FROM
@T AS t
)
SELECT
CTE.Name,
CTE.ImpFile,
CTE.[ImpTime],
0 as SortOrder
FROM
CTE
WHERE
CTE.RowNbr=1
UNION ALL
SELECT
t.Name,
t.ImpFile,
t.[ImpTime],
1 as SortOrder
FROM
@T AS t
ORDER BY
ImpFile DESC,SortOrder, ImpTime desc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With