how to select the last three value in database

Posted by Venky.Net under Sql Server on 4/28/2012 | Points: 10 | Views : 1214 | Status : [Member] | Replies : 4
hi
how to select last three value in databse




Responses

Posted by: Gow.Net on: 4/28/2012 [Member] Starter | Points: 25

Up
0
Down
ask your question clearly
select last three value in databse or Table

gowthaman8870226416

Venky.Net, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Niladri.Biswas on: 4/30/2012 [Member] Platinum | Points: 25

Up
0
Down
Try this

Declare @t table(name varchar(100))


Insert Into @t

select 'abhijit' union all select 'debasis' union all select 'mangoli' union all select 'hobita' union all

select 'jansu' union all select 'uday' union all select 'santunu' union all select 'sachin' union all

select 'jeeva' union all select 'sashi'



Select Top 3 Name From(

Select

Rn = ROW_NUMBER() Over(Order by(Select 1) )

,Name

From @t

)x

Order By x.Rn desc




--Result

Name


sashi

jeeva

sachin


Best Regards,
Niladri Biswas

Venky.Net, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Muhsinathk on: 6/20/2012 [Member] Bronze | Points: 25

Up
0
Down


SELECT TOP 3* FROM SAMPLE2 ORDER BY S_ID DESC

Venky.Net, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: CGN007 on: 6/20/2012 [Member] Silver | Points: 25

Up
0
Down
WITH employeeCTE AS

(
SELECT *,ROW_NUMBER()OVER(ORDER BY empid DESC) AS rowno FROM employee1
)

SELECT TOP 3 empid,empname FROM employeeCTE ORDER BY rowno



Table script

CREATE TABLE [dbo].[employee1](

[empid] [int] IDENTITY(1,1) NOT NULL,
[empname] [varchar](100) NULL,
PRIMARY KEY CLUSTERED
(
[empid] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]


Insert Script

GO

SET ANSI_PADDING OFF
GO
SET IDENTITY_INSERT [dbo].[employee1] ON
INSERT [dbo].[employee1] ([empid], [empname]) VALUES (1, N'Pandian')
INSERT [dbo].[employee1] ([empid], [empname]) VALUES (2, N'Mani')
INSERT [dbo].[employee1] ([empid], [empname]) VALUES (3, N'Raj')
INSERT [dbo].[employee1] ([empid], [empname]) VALUES (4, N'Kv')
INSERT [dbo].[employee1] ([empid], [empname]) VALUES (5, N'Naveen')
SET IDENTITY_INSERT [dbo].[employee1] OFF


Venky.Net, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response