I have the following table:
CREATE TABLE [dbo].[TableB](
[id] [int] NULL,
[FileName] [varchar](20) NULL
)
INSERT INTO [TableB] ([id],[FileName])VALUES(1,'File2')
INSERT INTO [TableB] ([id],[FileName])VALUES(2,'File1')
INSERT INTO [TableB] ([id],[FileName])VALUES(3,'File1')
When I do a simple SELECT, it gives following results:
id FileName
----------- --------------------
1 File2
2 File1
3 File1
But, when I add the DISTINCT it gives:
FileName
--------------------
File1
File2
I dont need the sorting, I need results something like below:
FileName
--------------------
File2
File1
I am working on SQL Server 2008. I want to maintain the original order of the results.
Assuming you want the results to be ordered by the first id recorded for each FileName, try: