Solved

Sql max column

Posted on 2009-05-14
5
386 Views
Last Modified: 2012-05-07
Hi

I am selecting records mulitple tables as follows

select distinct o.order_id, o.order_date, u.name, a.addressid
from orders o
inner join users u on o.user_id=u.user_id
inner join addresses a on u.user_id=a.user_id
where o.order_status='c'

I also want to add a column to the above which contains the max order_id from the above selected records.  

How can I acheive this?

Wing
0
Comment
Question by:WingYip
  • 3
  • 2
5 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 50 total points
ID: 24384489
select MAX(o.order_id) AS order_id, o.order_date, u.name, a.addressid
from orders o
inner join users u on o.user_id=u.user_id
inner join addresses a on u.user_id=a.user_id
where o.order_status='c'
GROUP BY o.order_date, u.name, a.addressid
0
 
LVL 1

Author Comment

by:WingYip
ID: 24384506
I am an idiot!
0
 
LVL 1

Author Closing Comment

by:WingYip
ID: 31581434
thanks
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 24384524
WingYip said:
>>I am an idiot!

No, you just need more coffee :)
0
 
LVL 1

Author Comment

by:WingYip
ID: 24384974
actually that doesnt work as it returns the highest order number within the grouping.

If there are a total of 300 records selected using the query above then I want to return the highest order_id out of those 300 records so that each row will show the same value for the MaxOrderId column (as below)

order_id    MaxOrderId
1                 4
2                 4
3                 4
4                 4
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

In this article I will describe the Detach & Attach 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.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

895 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

12 Experts available now in Live!

Get 1:1 Help Now