Solved

SQL MAX  Help

Posted on 2014-09-03
11
107 Views
Last Modified: 2014-09-03
Hi,
I have a very simple select statement that returns this dataset

SELECT parent_id, component_id, control_id FROM item where parent_id = '14923'

parent_id      component_id      control_id
14923      12054      53715
14923      12054      53721
14923      12054      53715
14923      12054      53721
14923      12423      53715
14923      12423      53721
14923      14176      53715
14923      14176      53721
14923      14783      53715
14923      14783      53721
14923      14795      53715
14923      14795      53721
14923      14840      53715
14923      14840      53721
14923      14880      53715
14923      14880      53721
14923      14924      53715
14923      14924      53721
14923      14926      53715
14923      14926      53721


I need to add an additional where clause that only returns the highest control values, so in this case the returned dataset would look like this:

parent_id      component_id      control_id
14923      12054      53721
14923      12054      53721
14923      12423      53721
14923      14176      53721
14923      14783      53721
14923      14795      53721
14923      14840      53721
14923      14880      53721
14923      14924      53721
14923      14926      53721

Any suggestions as to how I can accomplish this please
0
Comment
Question by:andyw27
[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
  • 4
  • 2
  • 2
  • +2
11 Comments
 
LVL 35

Accepted Solution

by:
Dan Craciun earned 500 total points
ID: 40300878
Maybe something like this (untested):
SELECT parent_id, component_id, control_id 
FROM item 
WHERE parent_id = '14923' AND control_id = (SELECT MAX(control_id) FROM item WHERE parent_id = '14923')

Open in new window

HTH,
Dan
0
 
LVL 15

Expert Comment

by:Vikas Garg
ID: 40300879
HI,

Select Parent_id, Component_id, (select max(control_id) from table) ControlID from Table

Open in new window

0
 
LVL 51

Expert Comment

by:Vitor Montalvão
ID: 40300884
Use the MAX() function:
SELECT parent_id, component_id, MAX(control_id)
FROM item 
WHERE parent_id = '14923'
GROUP BY parent_id, component_id

Open in new window

0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 66

Expert Comment

by:Jim Horn
ID: 40300894
The above answer is correct.

If you want to eliminate duplicates, use DISTINCT.
SELECT DISTINCT parent_id, component_id, MAX(control_id)
FROM item 
WHERE parent_id = '14923'
GROUP BY parent_id, component_id

Open in new window

Also, if you'd like further reading on aggregates such as MAX then I have an article out there called SQL Server GROUP BY Solutions that would be an excellent reading.
0
 
LVL 51

Expert Comment

by:Vitor Montalvão
ID: 40300904
Jim, you won't need the DISTINCT since it's already aggregate by parent_id and component_id so duplicates will be automatically dropped.
0
 

Author Closing Comment

by:andyw27
ID: 40300907
Thanks, worked like a charm.
0
 
LVL 51

Expert Comment

by:Vitor Montalvão
ID: 40300916
Andy, any of these solutions works (Vikas forgot the WHERE clause) but there's no need to use subselects.
You can do it in a more direct and efficient way.

Cheers
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 40300922
Glad I could help!

@Vitor and Jim: on your queries, if a component_id does not have a row where control_id is 53721, it will be listed with the highest control_id value if finds. The OP wanted only rows where control_id is 53721. At least that's the way I understood the question.
0
 
LVL 51

Expert Comment

by:Vitor Montalvão
ID: 40300931
Dan, I didn't understand the question like that:
where clause that only returns the highest control values
0
 
LVL 15

Expert Comment

by:Vikas Garg
ID: 40300969
Hello,

SELECT parent_id, component_id, control_id FROM item a
INNER JOIN
(Select Parent_id, Component_id, (select max(control_id) from item) ControlID from item) B
ON A.parent_id = B.Parent_id AND A.component_id = B.component_id AND A.control_id = B.control_id
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40302021
Vitor - You are correct.  What was I thinking...
0

Featured Post

10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
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.

628 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