【发布时间】:2019-08-12 11:11:18
【问题描述】:
我创建了一个 Coldfusion 脚本,允许从数组中获取对象“单元”。
我的数据库有关于单位的视图。对于相同的单元键“ORG_ID”,它可以存在多行(在字段“origin”上有所不同)。 “起源”字段可以是“当前”、“历史”或“不同”。
+---------+---------+------------+------------+----------|
| 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 |
+---------+---------+------------+------------+----------|
在这种情况下,例如我想要得到的结果:
+---------+---------+------------+------------+----------|--------------|
| ORG_ID | TITLE | VALID_FROM | VALID_TO | ORIGIN | CORRECT_VERS |
+---------+---------+------------+------------+----------|--------------|
| 1234 | A.1 | 01/03/2016 | 31/12/9999 | other | 0 |
| 1234 | A.1 | 01/03/2016 | 31/12/3333 | current | 1 |
| 1234 | A.1 | 01/03/2016 | 31/12/9999 | history | 0 |
| 5420 | A.2 | 01/01/2014 | 31/12/3333 | other | 1 |
| 9876 | A.3 | 01/03/2016 | 31/12/3333 | current | 1 |
| 9876 | B.3 | 01/03/2016 | 31/12/9999 | history | 0 |
| 5527 | A.1 | 01/03/2016 | 31/12/2199 | current | 1 |
| 5527 | D.2 | 01/01/2010 | 31/12/2015 | history | 0 |
| 5527 | A.1 | 01/01/2016 | 31/12/2199 | history | 0 |
| 6699 | E.5 | 01/01/2016 | 31/12/2017 | history | 0 |
| 6699 | A.4 | 01/01/2017 | 31/12/2018 | history | 0 |
+---------+---------+------------+------------+----------+--------------|
我的 Coldfusion 脚本: dataUnitArray 包含数组中的单位列表
<cftry>
<cfset hist = 0/>
<cfset unit = structNew() />
<cfloop index="i" from="1" to="#ArrayLen(dataUnitArray)#">
<cfif #dataUnitArray[i].ORIGIN# EQ "current">
<!--- Unit is current --->
<cfscript>
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.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 different (other) --->
<cfif hist EQ 0>
<cfscript>
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>
</cfloop>
<cfscript>
return unit;
</cfscript>
<cfcatch type="any">
<cfscript>
.....
</cfscript>
</cfcatch>
</cftry>
我的脚本工作正常。但是当我在大量数据上使用它时,我遇到了加载时间问题。这就是为什么我想直接在 ORACLE 中使用 CASE...WHEN 作为:
CASE
when ORIGIN = 'current' THEN 1
WHEN ORIGIN = 'history' THEN
CASE hist = 0 THEN ....
END
ELSE
0
END AS "IS_CORRECT_VERSION"
我想在视图中添加一个新列“CORRECT_VERSION”(版本正确时为 0 或 1),以便检索正确的单元版本。
但我不知道该怎么做,你能帮我吗?
提前感谢您的帮助。
赛博
【问题讨论】:
-
我不是冷聚变专家,而是预言机。那么您能否编辑问题并分享示例数据和预期输出?
-
嗨,我添加了所需的输出
标签: oracle coldfusion case