Drop Table

Support Forum for database administrators and web based access to important newsgroups related to databases
Register on Database Support Forum Edit your profileCalendarFind other Database Support forum membersFrequently Asked QuestionsSearch this forum -> 
For Database admins: Free Database-related Magazines Now Free shipping to Texas


Post New Thread










Thread
Author

Select statement for last and secondlast value
Assume I have a table like:

 CustomerName	OrderDa
te 	OrderPartID
London	        7/7/99	        33
Paris	        7/6/99		22
Rome	        7/5/99		22
London	        6/3/99		44
Paris		6/4/99		11
Rome		6/20/99		99
London		4/3/99		12
Paris		4/4/99		13
Rome		4/20/99		94

I a looking of a single SQL statement which will find the last and
second last order from customer "London" in a way like

 CustomerName	LastOrd
 erDate	SecondLastOrd
erDate
London		7/7/99		6/3/99

Can anyone help me?

Gerald


Report this thread to moderator Post Follow-up to this message
Old Post
gerald
08-24-05 04:24 PM


Re: Select statement for last and secondlast value
Something like this might work (but you need to consider what to do if
there are multiple orders on the same date):

select
'London',
max(dt.orderdate) as 'LastOrder',
min(dt.orderdate) as 'SecondLastOrder'
from
(
select top 2 orderdate
from dbo.orders
where customername = 'London'
order by orderdate desc
) dt

Simon


Report this thread to moderator Post Follow-up to this message
Old Post
Simon Hayes
08-24-05 04:24 PM


Sponsored Links





Last Thread Next Thread
Post New Thread

Microsoft SQL Server forum archive

Show a Printable Version Email This Page to Someone! Receive updates to this thread
Microsoft SQL Server
Access database support
PostgreSQL Replication
SQL Server ODBC
FoxPro Support
PostgreSQL pgAdmin
SQL Server Clustering
MySQL ODBC
Web Applications with dBASE
SQL Server CE
MySQL++
Sybase Database Support
MS SQL Full Text Search
PostgreSQL Administration
SQL Anywhere support
DB2 UDB Database
Paradox Database Support
Filemaker Database
Berkley DB
SQL 2000/2000i database
ASE Database
Forum Jump:
All times are GMT. The time now is 12:17 PM.

 
Mobile devices forum | Database support forum archive




Copyrights DropTable.com Database Support Forum 2004 - 2006