Solved

Ms sql query last order by customer

Posted on 2010-08-19
5
519 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
  • 3
5 Comments
 
LVL 12

Expert Comment

by:mcv22
Comment Utility
;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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now