SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Select Bottom 16
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

ConfusedAgain
Yak Posting Veteran

United Kingdom
81 Posts

Posted - 05/04/2012 :  04:28:01  Show Profile  Reply with Quote
How can I select the bottom 16.

I want to run an update query that selects the last 16 records. These are used to in a game schedule table creating game time for the same time but pushed on by one day;
As shown here:
INSERT INTO GameSchedule
(StartTime,EndTime, GameType)
SELECT TOP (16) DATEADD(dd, 1, StartTime) AS NewStartTime, DATEADD(dd, 1, EndTime) AS NewEndTime, GameType
FROM GameSchedule AS GameSchedule_1
ORDER BY GameID

I know I can change the order to DESC but I want to keep the automated transaction number in the table following a continuous sequence. So by reversing the order I will end up with the last game of the day having a lower transaction number.

Hopefully that all makes sense.

webfred
Flowing Fount of Yak Knowledge

Germany
8513 Posts

Posted - 05/04/2012 :  05:19:51  Show Profile  Visit webfred's Homepage  Reply with Quote
Try this:
INSERT INTO GameSchedule(StartTime,EndTime, GameType)

select NewStartTime,NewEndTime,GameType
from
(
	SELECT 
	DATEADD(dd, 1, StartTime) AS NewStartTime, 
	DATEADD(dd, 1, EndTime) AS NewEndTime, 
	GameType,
	ROW_NUMBER() OVER (order by GameID DESC) as rnum
	FROM GameSchedule AS GameSchedule_1
)dt
where rnum <= 16
order by rnum DESC




No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
29138 Posts

Posted - 05/04/2012 :  05:30:33  Show Profile  Visit SwePeso's Homepage  Reply with Quote
;WITH cteSource(GameID, StartTime, EndTime, GameType)
AS (
	SELECT TOP(16)	GameID,
			DATEADD(DAY, 1, StartTime) AS StartTime,
			DATEADD(DAY, 1, EndTime) AS EndTime,
			GameType
	FROM		dbo.GameSchedule
	ORDER BY	GameID DESC
)
INSERT		dbo.GameSchedule
		(
			StartTime,
			EndTime,
			GameType
		)
SELECT		StartTime,
		EndTime,
		GameType
FROM		cteSource
ORDER BY	GameID



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22460 Posts

Posted - 05/04/2012 :  06:46:47  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
Also refer this
http://beyondrelational.com/modules/2/blogs/70/posts/10845/return-top-n-rows.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

ConfusedAgain
Yak Posting Veteran

United Kingdom
81 Posts

Posted - 05/06/2012 :  04:53:37  Show Profile  Reply with Quote
Thank guys sorry for the slow reply I have only just got around to testing it. All replies are excellent and really helpful.
Cheers
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000