Solved

Ms sql query last order by customer

Posted on 2010-08-19
5
572 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 50 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

634 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