June 17, 2014 at 3:50 am
HI All,
I have an issue while display the result in the required order.
please help me to get the required output.
Code :
USE tempdb
GO
IF OBJECT_ID('tempdb..#VersionFormat_tbl') IS NOT NULL
DROP TABLE #VersionFormat_tbl
CREATE TABLE #VersionFormat_tbl
(
[FormatID] [smallint] NOT NULL,
[Description] [varchar](50) NULL,
[fileExtension] [varchar](10) NOT NULL,
[versionFormatTypeId] [tinyint] NOT NULL,
CONSTRAINT [PK_VersionFormat_tbl] PRIMARY KEY CLUSTERED
(
[FormatID] ASC
)
)
INSERT INTO #VersionFormat_tbl
( FormatID ,
Description ,
fileExtension ,
versionFormatTypeId
)
VALUES
(1,'Text','txt',1),
(2,'HTML','html',1),
(3,'XML','xml',1),
(4,'PDF','pdf',1),
(5,'Excel','xls',1),
(6,'Word','doc',1)
GO
SELECT fileExtension,* FROM #VersionFormat_tbl
Present output :
fileExtensionFormatIDDescriptionfileExtensionversionFormatTypeId
txt1Texttxt1
html2HTMLhtml1
xml3XMLxml1
pdf4PDFpdf1
xls5Excelxls1
doc6Worddoc1
Required output:
fileExtensionFormatIDDescriptionfileExtensionversionFormatTypeId
html2HTMLhtml1
xls5Excelxls1
doc6Worddoc1
pdf4PDFpdf1
txt1Texttxt1
xml3XMLxml1
let me know for more details.
Thanks
Bhanu
June 17, 2014 at 3:55 am
How is the order you want being decided? They appear essentially random to me.
How to post a question to get the most help http://www.sqlservercentral.com/articles/Best+Practices/61537
June 17, 2014 at 3:55 am
Easiest way will be to add another column, call it DisplayOrder or something like that. Populate that column with the order you want the rows to be displayed and ORDER BY that column.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
June 17, 2014 at 4:49 am
yes you are right Gail Shaw, the suggestion you proposed is working fine.
thank you.
June 17, 2014 at 4:55 am
kbhanu15 (6/17/2014)
yes you are right Gail Shaw, the suggestion you proposed is working fine.thank you.
Can you post your solution please kbhanu15? It would be very handy for other folks who may stumble upon this thread.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
June 17, 2014 at 7:05 am
sure, please find the solution for this issue.
FIX 1:
USE tempdb
GO
IF OBJECT_ID('tempdb..#VersionFormat_tb') IS NOT NULL
DROP TABLE #VersionFormat_tbl
CREATE TABLE #VersionFormat_tbl
(
[FormatID] [smallint] NOT NULL,
[Description] [varchar](50) NULL,
[fileExtension] [varchar](10) NOT NULL,
[versionFormatTypeId] [tinyint] NOT NULL,
DisplayOrder SMALLINT NOT NULL
CONSTRAINT [PK_VersionFormat_tbl] PRIMARY KEY CLUSTERED
(
[FormatID] ASC
)
)
INSERT INTO #VersionFormat_tbl
( FormatID ,
Description ,
fileExtension ,
versionFormatTypeId,
DisplayOrder
)
VALUES
(1,'Text','txt',1,5),
(2,'HTML','html',1,1),
(3,'XML','xml',1,6),
(4,'PDF','pdf',1,4),
(5,'Excel','xls',1,2),
(6,'Word','doc',1,3)
GO
SELECT * FROM #VersionFormat_tbl
ORDER BY DisplayOrder
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply