Solved

Selecting records by a maximum value

Posted on 2008-10-09
3
766 Views
Last Modified: 2013-12-07
Hi there,

I am looking for a way to alter my select query to select only the highest values from the revision collumn.

SELECT c.check_id ,  c.doc_id,  c.revision, o.ORDER_NUM,d.doc_line FROM docsChecks c INNER JOIN docsDocuments d ON d.doc_id = c.doc_id INNER JOIN OPORDHD o ON c.order_no = o.ORDER_NUM

I sort of achieved whta i wanted by adding ' WHERE c.revision = (SELECT MAX(c.revision) from c)' but this only returns the single row with the highest revision number, I wish for multiple rows with the highest numbers. I have included before and after exmaples of the result sets I am wishing to acheive in the code snippets.
CHECK_ID  DOC_ID  REVISION  ORDER_NUM  	DOC_LINE

100 	  1 	  0 	    AM10000 	1

101 	  1 	  1 	    AM10000 	1

102 	  1 	  2 	    AM10000 	1

103 	  2 	  0 	    AM10000 	2

104 	  2 	  1 	    AM10000 	2

105 	  2 	  2 	    AM10000 	2

106 	  3 	  0 	    AM10000 	3
 

CHECK_ID  DOC_ID  REVISION  ORDER_NUM  	DOC_LINE

102 	  1 	  2 	    AM10000 	1

105 	  2 	  2 	    AM10000 	2

106 	  3 	  0 	    AM10000 	3

Open in new window

0
Comment
Question by:wellso
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
Cvijo123 earned 500 total points
ID: 22676328
Something like this ?


SELECT 

	max(c.check_id) as check_id,

	c.doc_id,

	max(c.revision) as revision,

	max(o.ORDER_NUM) as ORDER_NUM,

	max(d.doc_line) as doc_line

FROM docsChecks c

	INNER JOIN docsDocuments d

			ON d.doc_id = c.doc_id

	INNER JOIN OPORDHD o 

		ON c.order_no = o.ORDER_NUM

group by 

 	c.doc_id

Open in new window

0
 
LVL 7

Expert Comment

by:fluglash
ID: 22676338
add order by clause to your query and add the outer query to limit number of rows
select * 

from (<i>Your_SQL_Query_Here</i>

         order by conum desc)

where  rownum < <i>Number_of_records_needed</i>

Open in new window

0
 
LVL 7

Expert Comment

by:fluglash
ID: 22676340
select *
from (Your_SQL_Query_Here
         order by conum desc)
where  rownum < Number_of_records_needed
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…

947 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

21 Experts available now in Live!

Get 1:1 Help Now