Solved

Getting a specific number of records from Oracle DB with Coldfusion

Posted on 2016-10-05
6
44 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

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

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This is an updated version of a post made on my blog over 3 years ago. It is unfortunately, still very relevant as we continue to see both SQLi (SQL injection) and XSS (cross site scripting) attacks hitting some of the most recognizable website and …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
Via a live example, show how to take different types of Oracle backups using RMAN.

706 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

20 Experts available now in Live!

Get 1:1 Help Now