Solved

Ms sql query last order by customer

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

920 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