how to find last three value in table

Posted by Venky.Net under Sql Server on 4/28/2012 | Points: 10 | Views : 2068 | Status : [Member] | Replies : 10
how to select last three value in table and suitable examle




Responses

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

Up
0
Down
using BETWEEN and findout

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/18/2012 [Member] Bronze | Points: 25

Up
0
Down

CREATE TABLE [dbo].[sample2](
[s_id] [int] IDENTITY(1,1) NOT NULL,
[s_Name] [varchar](10) NULL,
[s_Price] [int] NULL,
[s_product] [varchar](29) NULL,
PRIMARY KEY CLUSTERED
(
[s_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]


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: Nksingh420 on: 6/18/2012 [Member] Starter | Points: 25

Up
0
Down
select top 3 * from books order by bid desc

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

Posted by: CGN007 on: 6/19/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]



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

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

Up
0
Down


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

Posted by: perfectchourasia-9163 on: 6/20/2012 [Member] Starter | Points: 25

Up
0
Down
select top 3 * from Table_Name order by Column_Name desc

ER sandeep chourasia
sandeepchrs@yahoo.com (on facebook)
http://www.aspnetcodes.com/

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
@Perfect.Chourasia@Gmail.Com
Question is to find-out the last 3 inserted value. Please check my answer.

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

Posted by: CGN007 on: 7/31/2012 [Member] Silver | Points: 25

Up
0
Down
please mark it as answer....That helps other who search the same...

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

Posted by: Ranjeet_8 on: 7/31/2012 [Member] [MVP] Gold | Points: 25

Up
0
Down
Select Top3 * From Table_Name Order by   Column_Name desc


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

Login to post response