Solved

Getting a specific number of records from Oracle DB with Coldfusion

Posted on 2016-10-05
6
66 Views
Last Modified: 2016-10-05
Good morning All!!! I have a dilemma that I was hoping I could get some help with. I have an Oracle database, with two specific tables I'm dealing with. The first table is a list of various projects, and has the project number (PROJ_NO) as a distinct field. My second table has rows with information about the projects in the first table, but there isn't a one to one or even one to may correlation. So I could have projects in the first table that has no corresponding record in the second table, or I could have one record in the first table with multiple corresponding records in the second table. So in my query I used a left join of the second table to the first, and my results are correct, giving me all projects, whether a project number appears in the second table or not. The problem I have is that for a given project number, I could have 10 or more records in the second table and I only want the latest 3 records. I do record the date that the info is inserted into the second table. I thought I could use "FETCH NEXT 3 ROWS ONLY" but that doesn't appear to work correctly in a left join. On the ColdFusion side, I'm using the CFOUPUT query to display my results. Am I wrong to try to use the FETCH NEXT 3 ROWS ONLY?? It appears it's trying to get the next three project numbers rather than the three comments about the projects from the second table.

<CFQUERY DATASOURCE="MCA_Oracle" NAME="Input">
SELECT current_bulk_filter.PROJ_NO,
X.PROJ_CLOSED_REASON
FROM MCA.current_bulk_filter

LEFT JOIN
(SELECT spend_plan_history.PROJ_NO,
spend_plan_history.PROJ_CLOSED_REASON FROM
MCA.spend_plan_history

ORDER BY spend_plan_history.IDA DESC
FETCH NEXT 3 ROWS ONLY) X
ON x.proj_no = current_bulk_filter.proj_no

where current_bulk_filter.proj_no = X.PROJ_NO
</CFQUERY>




<TABLE>

<cfset projectNo = 0>
<cfoutput query="Input" group="PROJ_NO">
<cfset projectNo = projectNo + 1>

<TR>

<TD><CFINPUT TYPE="TEXT"
             NAME="PROJNO#projectNo#"
             VALUE="#PROJ_NO#"
             SIZE="3"
             MAXLENGTH="35"
             style="font-size:12px; background-color:white"></TD>
             
             
<TD><textarea rows="8"  cols="35 name="PROJ_CLOSED_REASONP#projectNo#" id="PROJ_CLOSED_REASONP" style="font-size:12px; background-color:white"><CFOUTPUT>#PROJ_CLOSED_REASON# // </CFOUTPUT></textarea></TD></TR></cfoutput></TABLE>
0
Comment
Question by:diecasthft01
  • 3
  • 2
6 Comments
 
LVL 34

Accepted Solution

by:
johnsone earned 500 total points
ID: 41830018
I believe that this should get you pretty close to what you are looking for:
SELECT proj_no, 
       proj_closed_reason 
FROM   (SELECT current_bulk_filter.proj_no, 
               mca.proj_closed_reason, 
               Row_number() 
                 over ( 
                   PARTITION BY current_bulk_filter.proj_no 
                   ORDER BY mca.ida DESC) rn 
        FROM   mca.current_bulk_filter 
               left join mca.spend_plan_history 
                      ON ( current_bulk_filter.proj_no = mca.proj_no )) 
WHERE  rn <= 3; 

Open in new window

0
 

Author Comment

by:diecasthft01
ID: 41830129
Yup...that gets my results that I expected to get. I totally missed the idea of partition. Am I able to add another LEFT JOIN to this query?? I actually have a few LEFT JOINS from different tables, and just left them out of the example for clarity.
0
 
LVL 34

Expert Comment

by:johnsone
ID: 41830144
That shouldn't be a problem at all.  You may want to do it after the row number so that you aren't joining all the rows and then cutting some off.  Extra work you don't need to do.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:diecasthft01
ID: 41830163
Got ya!!!! Thanks A lot!!! I believe this is exactly what I needed.
0
 
LVL 32

Expert Comment

by:awking00
ID: 41830169
Slight variation -
select a.proj_no, b.proj_closed_reason
from mca.current_bulk_filter a
left join
(select proj_no, proj_closed_reason,
 row_number() over (partition by proj_no order by ida desc) rn
 from mca.spend_plan_history) b
on a.proj_no = b.proj_no
where b.rn <= 3;
0
 
LVL 34

Expert Comment

by:johnsone
ID: 41830189
I didn't think that doing the row limitation first would be helpful because either way it has to go through all the rows in the child table.  In fact, it could be longer because you have to go through them once to limit the rows and then a second time on the smaller set to do the join.
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

CFGRID Custom Functionality Series -  Part 1 Hi Guys, I was once asked how it is possible to to add a hyperlink in the cfgrid and open the window to show the data. Now this is quite simple, I have to use the EXT JS library for this and I achiev…
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.
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

803 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