Solved

Sql max column

Posted on 2009-05-14
5
392 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
[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
  • 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

729 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