Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Ms sql query last order by customer

Posted on 2010-08-19
5
Medium Priority
?
576 Views
Last Modified: 2012-05-10
I wanna to query a result for following table

O_Id  OrderDate     OrderPrice     Customer
1        2008/11/12   1000               Hansen
2        2008/10/23   1600               Nilsen
3        2008/09/02   700                 Hansen
4        2008/09/03   300                 Hansen
5        2008/08/30   2000               Jensen
6       2008/10/04    100                 Nilsen

Result
2008/09/03 300 Hansen
2008/10/04 100 Nilsen
2008/08/30 2000 Jensen

Thanks

Becky

0
Comment
Question by:beckyng
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 12

Expert Comment

by:mcv22
ID: 33481778
;WITH LastOrder(Customer, O_Id) AS
(
    SELECT
       Customer,
       MAX(O_Id)
    FROM
      Table
    GROUP BY
       Customer
)
SELECT
   t.OrderDate,
   t.OrderPrice,
   t.Customer
FROM
    LastOrder lo
JOIN
   Table t
ON
  lo.O_Id = t.O_Id
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33481780
select O_Id,OrderDate,OrderPrice,Customer
FROM
(
select O_Id,OrderDate,OrderPrice,Customer, rn=ROW_NUMBER() over (partition by Customer order by OrderDate desc)
from tbl
) SQ
where rn=1
0
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 200 total points
ID: 33481796
Interesting, my query above is by last OrderDate but the question sample result is by ID.

select O_Id,OrderDate,OrderPrice,Customer
FROM
(
select O_Id,OrderDate,OrderPrice,Customer, rn=ROW_NUMBER() over (partition by Customer order by O_Id desc)
from tbl
) SQ
where rn=1

The analytical version is normally faster than the MAX version, and is robust to break ties when the condition is not on the primary key (it seems here that it is).  If it is the unique ID, the MAX version from mcv22 can be rewritten

SELECT   OrderDate,   OrderPrice,   Customer
FROM  tbl
WHERE O_Id in (select Max(O_Id) from tbl group by Customer)
0
 

Author Comment

by:beckyng
ID: 33481846
Thanks all

But I have another query result wanna to get lastest record by customer & good as follows
I wanna to query a result for following table

O_Id  OrderDate     OrderPrice     Customer      Good
1        2008/11/12   1000               Hansen         Chair
2        2008/10/23   1600               Nilsen            Chair
3        2008/09/02   700                 Hansen         Table
4        2008/09/03   300                 Hansen         Chair
5        2008/08/30   2000               Jensen          Chair
6       2008/10/04    100                 Nilsen             Light

Result
2008/09/03 300 Hansen   Chair
2008/10/23 1600 Nilsen    Chair  
2008/09/02 700  Hansen  Table
2008/08/30 2000 Jensen   Chair
2008/10/04 100    Nilsen     Light  
Thanks

Becky
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33481885
select OrderDate,OrderPrice,Customer,Good
FROM
(
select O_Id,OrderDate,OrderPrice,Customer, rn=ROW_NUMBER() over (partition by Customer, Good order by O_Id desc)
from tbl
) SQ
where rn=1
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question