Wednesday, March 28, 2012

Latest record

My table are

Customer: customerId ,name

Order: orderId, customerId, product,date

I want to display latest order from the customer

You can use ORDER BY.

Select O.OrderId, C.Name, O.Product, O.DateFROM Order OJOIN Customer CON C.CustomerId O.CustomerIdORDER BY O.DateDesc
|||

SELECT orderId, customerId, product,[date], [name]FROM(select a.orderId, a.customerId, a.product,a.[date], b.name, row_number()over(partitionby a.customeridorderby [date]DESC)as RowNum

from [Order] ainnerjoin [Customer] bon a.customerId=b.customerId) t

WHERE RowNum= 1

sql

No comments:

Post a Comment