Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Database > SQL Language
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old January 13th, 2007, 12:51 PM
Registered User
 
Join Date: Jan 2007
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default problem getting the last 4 dates

I am busy with a stored procedure where I need to get the 4 last dates of different advertisements in a transaction table. (ms sql server)

This is the data in the transaction table:

id | AdvID | Date
1 | 14 | 13-2-2007 0:10:27
2 | 14 | 14-2-2007 0:10:27
3 | 14 | 15-2-2007 0:10:27
4 | 15 | 13-2-2007 0:10:27
5 | 15 | 15-2-2007 0:10:27
6 | 16 | 14-2-2007 0:10:27
7 | 16 | 16-2-2007 0:10:27
8 | 17 | 13-2-2007 0:10:27
9 | 17 | 15-2-2007 0:10:27
10 | 17 | 16-2-2007 0:10:27


These are the records that I need to have:

id | AdID | ValidThru
3 | 14 | 15-2-2007 0:10:27
5 | 15 | 15-2-2007 0:10:27
7 | 16 | 16-2-2007 0:10:27
10 | 17 | 16-2-2007 0:10:27

It will probably be very easy to get these records in a query but it's killing me for a while now

I hope someone can help me through this.

Thanks in advance!

Smiter
Reply With Quote
  #2 (permalink)  
Old January 13th, 2007, 04:03 PM
Authorized User
 
Join Date: Jan 2007
Location: , , .
Posts: 46
Thanks: 2
Thanked 1 Time in 1 Post
Default

Now as far as i can see you are required to retrieve last 4 records up to a specific date.And if this is the case then all you have to do is to use the ORDER BY and WHERE clauses as needed:

EXAMPLE

SELECT * FROM
TRANSACTION
WHERE ValidThru >= 14-2-2007 0:10:27
ORDER BY ValidThru;

---------------------------
Try out this query, hope it helps and if you are still stuck then reply me back.


MAXOOD!

Life is an endless journey towards perfection
Reply With Quote
  #3 (permalink)  
Old January 15th, 2007, 01:30 PM
Authorized User
 
Join Date: Oct 2005
Location: , , .
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Try this query:

SELECT A.*
FROM [dbo].[YourTable] A INNER JOIN (SELECT [AdvID], MAX([ID]) AS [ID] FROM [dbo].[YourTable] GROUP BY [AdvID]) B ON A.[ID] = B.[ID]


SQL Server Helper
How well do you know SQL? Find out with the free test assessment from SQL Server Helper!!!
http://www.sql-server-helper.com/free-test/default.aspx

Got a SQL Server Question? Ask us here: http://www.sql-server-helper.com/forums/default.asp
Reply With Quote
  #4 (permalink)  
Old January 16th, 2007, 05:19 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Bangalore, KA, India.
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Not sure if this is what you are looking for.
Code:
Select Top 4 * from [TableName] Order by Date Desc, AdvID
Hope that helps.
Cheers.

_________________________
- Vijay G
Strive for Perfection
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem when inserting dates jhansib4u SQL Server DTS 1 January 28th, 2008 11:06 AM
Problem importing dates IronStar MySQL 0 April 16th, 2005 05:32 PM
SQL dates - Problem malecumbria Classic ASP Databases 4 February 16th, 2005 01:18 PM
Problem with Dates in Access Varg_88 Classic ASP Databases 2 December 8th, 2004 09:42 PM
Compare dates [problem]... Varg_88 VB Databases Basics 3 August 20th, 2004 09:55 AM



All times are GMT -4. The time now is 06:03 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.