My application manage meetings. Each meeting is linked to a unit. So I have one table in my DB: MEETINGS and a view UNITS linked with the key ORG_ID.
In the view for a same unit key "ORG_ID", it can exist several rows (with a difference on a field "origin"). the field "origin" can be "current", "history" or "different".
+---------+---------+------------+------------+----------|
| ORG_ID | TITLE | VALID_FROM | VALID_TO | ORIGIN |
+---------+---------+------------+------------+----------|
| 1234 | A.1 | 01/03/2016 | 31/12/9999 | other |
| 1234 | A.1 | 01/03/2016 | 31/12/3333 | current |
| 1234 | A.1 | 01/03/2016 | 31/12/9999 | history |
| 5420 | A.2 | 01/01/2014 | 31/12/3333 | other |
| 9876 | A.3 | 01/03/2016 | 31/12/3333 | current |
| 9876 | B.3 | 01/03/2016 | 31/12/9999 | history |
| 5527 | A.1 | 01/03/2016 | 31/12/2199 | current |
| 5527 | D.2 | 01/01/2010 | 31/12/2015 | history |
| 5527 | A.1 | 01/01/2016 | 31/12/2199 | history |
| 6699 | E.5 | 01/01/2016 | 31/12/2017 | history |
| 6699 | A.4 | 01/01/2017 | 31/12/2018 | history |
+---------+---------+------------+------------+----------|
Thus for a meeting several records of the unit assigned can be retrieved. My goal would be to retrieve the correct record. That's why, I created a function in Coldfusion in order to get an object "Unit" of the correct version of the unit used when creating the meeting.
The parameter unitsArray contains all units of the Units table linked to the meeting.
Here my script:
<cffunction name="getUnitVersionFromMeetingOrgId" access="remote" returnformat="JSON" returntype="budgetting.class.ResponseVO" hint="This function get the correct unit version of a meeting.">
<cfargument name="meetingOrgId" type="numeric" required="yes">
<cfargument name="meetingId" type="numeric" required="yes">
<cfargument name="unitsArray" type="string" required="yes">
<cfset unit = "">
<cfscript>
var dataUnitArray = deserializeJSON(arguments.unitsArray,true);
</cfscript>
<cftry>
<cfset hist = 0/>
<cfset unit = structNew() />
<cfloop index="i" from="1" to="#ArrayLen(dataUnitArray)#">
<cfif #dataUnitArray[i].REUN_NUMRREUN# EQ #meetingId#>
<cfif #dataUnitArray[i].ORG_ID# EQ #meetingOrgId#>
<cfif #dataUnitArray[i].ORIGIN# EQ "current">
<!--- Unit is current --->
<cfscript>
unit.REUN_NUMRREUN = #dataUnitArray[i].REUN_NUMRREUN#;
unit.ORG_ID = #dataUnitArray[i].ORG_ID#;
unit.TITLE = #dataUnitArray[i].TITLE#;
unit.UNIT_VALID_FROM = #dateFormat(dataUnitArray[i].UNIT_VALID_FROM, 'DD/MM/YYYY')#;
unit.UNIT_VALID_TO = #dateFormat(dataUnitArray[i].UNIT_VALID_TO, 'DD/MM/YYYY')#;
unit.ORIGIN = #dataUnitArray[i].ORIGIN#;
return unit;
</cfscript>
<cfelse>
<cfif #dataUnitArray[i].ORIGIN# EQ "history">
<!--- Unit is history --->
<cfscript>
unit.REUN_NUMRREUN = #dataUnitArray[i].REUN_NUMRREUN#;
unit.ORG_ID = #dataUnitArray[i].ORG_ID#;
unit.TITLE = #dataUnitArray[i].TITLE#;
unit.UNIT_VALID_FROM = #dateFormat(dataUnitArray[i].UNIT_VALID_FROM, 'DD/MM/YYYY')#;
unit.UNIT_VALID_TO = #dateFormat(dataUnitArray[i].UNIT_VALID_TO, 'DD/MM/YYYY')#;
unit.ORIGIN = #dataUnitArray[i].ORIGIN#;
</cfscript>
<cfset hist++ >
<cfelse>
<!--- Unit is other --->
<cfif hist EQ 0>
<cfscript>
unit.REUN_NUMRREUN = #dataUnitArray[i].REUN_NUMRREUN#;
unit.ORG_ID = #dataUnitArray[i].ORG_ID#;
unit.TITLE = #dataUnitArray[i].TITLE#;
unit.UNIT_VALID_FROM = #dateFormat(dataUnitArray[i].UNIT_VALID_FROM, 'DD/MM/YYYY')#;
unit.UNIT_VALID_TO = #dateFormat(dataUnitArray[i].UNIT_VALID_TO, 'DD/MM/YYYY')#;
unit.ORIGIN = #dataUnitArray[i].ORIGIN#;
</cfscript>
</cfif>
</cfif>
</cfif>
</cfif>
</cfif>
</cfloop>
<cfscript>
return unit;
</cfscript>
<cfcatch type="any">
<cfscript>
...
</cfscript>
</cfcatch>
</cftry>
</cffunction>
My script is correctly working. But I have loading time problem when I used it on a lot of data. That's why I would like to do that directly in ORACLE by using CASE...WHEN (perhaps in a procedure but I'm not an expert for that):
CASE
when ORIGIN = 'current' THEN 1
WHEN ORIGIN = 'history' THEN
CASE hist = 0 THEN ....
END
ELSE
0
END AS "IS_CORRECT_VERSION"
Here I would like to have
+------------+--------+---------+------------+------------|----------|--------------|
| MEETING_ID | ORG_ID | TITLE | VALID_FROM | VALID_TO | ORIGIN | CORRECT_VERS |
+------------+--------+---------+------------+------------|----------|--------------|
| 23600 | 1234 | A.1 | 01/03/2016 | 31/12/9999 | other | 0 |
| 23600 | 1234 | A.1 | 01/03/2016 | 31/12/3333 | current | 1 |
| 23600 | 1234 | A.1 | 01/03/2016 | 31/12/9999 | history | 0 |
| 41500 | 5420 | A.2 | 01/01/2014 | 31/12/3333 | other | 1 |
| 37800 | 9876 | A.3 | 01/03/2016 | 31/12/3333 | current | 1 |
| 37800 | 9876 | B.3 | 01/03/2016 | 31/12/9999 | history | 0 |
| 24200 | 5527 | A.1 | 01/03/2016 | 31/12/2199 | current | 1 |
| 24200 | 5527 | D.2 | 01/01/2010 | 31/12/2015 | history | 0 |
| 24200 | 5527 | A.1 | 01/01/2016 | 31/12/2199 | history | 0 |
| 53690 | 6699 | E.5 | 01/01/2016 | 31/12/2017 | history | 0 |
| 53690 | 6699 | A.4 | 01/01/2017 | 31/12/2018 | history | 1 |
+------------+--------+---------+------------+------------|----------|--------------|
I would like to add a new column "CORRECT_VERSION" ( value 0 or 1 when the version is correct) in a view in order to retrieve the correct unit version of a meeting by filtering on the new column.
I'm trying to that but It's not working each time.
Could you please help me with that?
Thanks in advance for your help.
Seb