Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 26th, 2003, 06:05 PM
Registered User
 
Join Date: Aug 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Easy one - last row

Hello all !

This ought to be an easy one.

I have made a query that returns ordered (by specific criteria)results. Now I am only interested in the last row returned by the query.

** Details **
I have a text field that contains strings that have letters and numbers. I then extract the numbers with mid() and sort the results of the query with these numbers.

TextField samples:
"Showing002-2003" refers to the 2nd item of 2003
"Showing021-2002" refers to the 21st item of 2002


SELECT
  TextField,
  mid(TextField, 8,3) as number,
  mid(TextField, 12, 4) as year
FROM Mytable
ORDER BY mid(TextField, 12, 4), mid(TextField, 8,3)

This query returns me all the items in order. Now I just want to have the last entry; Showing002-2003 if these were the only two entries in the table.

I have tried using the last() function, but can't get it to work.

Anybody has an idea?

Thanks,
FranyK
 
Old August 26th, 2003, 06:15 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 184
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Jonax
Default

I guess

Code:
SELECT TOP 1
  TextField,
  mid(TextField, 8,3) as number,
  mid(TextField, 12, 4) as year
FROM Mytable
ORDER BY mid(TextField, 12, 4) DESC, mid(TextField, 8,3) DESC
oughta do it...

 
Old August 26th, 2003, 06:17 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 184
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Jonax
Default

... actually I think you want:

ORDER BY mid(TextField, 8,3) DESC, mid(TextField, 12, 4) DESC


 
Old August 26th, 2003, 06:56 PM
Registered User
 
Join Date: Aug 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

yup, thanks ...

But your first answer was correct.

I want to sort by year first, then sort by number, instead of sorting by number, and then by year





Similar Threads
Thread Thread Starter Forum Replies Last Post
Scrollbar in row of Datagrid Except Header Row Theone84 ASP.NET 2.0 Professional 0 August 11th, 2008 12:10 AM
To retrieve a row by giving row number prad_a MySQL 1 March 22nd, 2007 11:20 PM
UPDATING 1 row with another row in same table rit01 SQL Server 2000 3 February 19th, 2006 08:55 AM
Read a column and Search Row by Row in another col AyatKh Excel VBA 2 January 26th, 2005 12:02 PM
Manage data row by row in datagrid Dragonist Classic ASP Databases 5 July 29th, 2004 04:17 AM





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