Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Sql max column

Posted on 2009-05-14
5
Medium Priority
?
394 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 93

Accepted Solution

by:
Patrick Matthews earned 200 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 93

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

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

715 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