PLSQL / Oracle如何"映射" /"规范化"数

时间:2016-03-22 05:21:28

标签: c# oracle plsql

我有一个View从一个基表中获取一个名为PRIORITYPOINT的数字:

CREATE OR REPLACE VIEW VW_INFO
(... --all column names here)
BEQUEATH DEFINER
AS 
SELECT 
  ... -- other selected columns
  MT.PRIORITYPOINT, -- the column in question
  ... --other selected columns
FROM MTINFO MT, UTINFO UT
WHERE MT.UTID = UT.UTID 
  AND MT.EXECREADY = 'R'
  AND MT.ISEXEC > 0

现在,我想将查询的PRIORITYPOINT更改为 - 缺少/不知道更好的术语 - "标准化" /"映射"形式。

为了说明,我的PRIORITYPOINT可能如下所示:

76
53
99
1
0
99
345

当它们被标准化" /"映射"时,它们应该变成这样:

3
2
4
1
0
4
5

"规范化" /"映射"会发生什么?以上是优先级点(0-345)的宽值范围转换为简单优先级(0-5)。

此刻,我做了#34;规范化"在使用该表的代码中(我在C#编码):

public List<InfoTable> AssignT5MOInfoViewNormalizedPriorities(List<InfoTable> list, out string eStr) {
  eStr = "";
  try {
    ... //something else
    List<double> distinctPriorities = list.Select(x => x.PriorityPoint).Distinct().ToList(); //secondly, normalize points
    distinctPriorities.Sort(); //This Sort belongs to List
    Dictionary<double, int> distinctPrioritiesDict =
      distinctPriorities.Zip(Enumerable.Range(0, distinctPriorities.Count), (k, v) => new { k, v })
      .ToDictionary(x => x.k, x => x.v);
    for (int i = 0; i < list.Count; ++i)
      list[i].PriorityPoint = distinctPrioritiesDict[list[i].PriorityPoint];
    return list;
  } catch (Exception e){ //for whatever reason, doesn't matter for now...
    eStr = e.ToString();
    //something else
    return null;
  }
}

有没有办法进行&#34;规范化&#34; /&#34;映射&#34;在PLSQL

修改

我正在使用Oracle数据库。我对使用Oracle内置函数的解决方案也很满意。

1 个答案:

答案 0 :(得分:1)

可以使用Oracle SQL中的rankdense_rank函数来获取规范化值。 E.g。

SELECT 
  MT.PRIORITYPOINT
  , RANK()       OVER (ORDER BY MT.PRIORITYPOINT ASC)     AS EXAMPLE1
  , DENSE_RANK() OVER (ORDER BY MT.PRIORITYPOINT ASC) - 1 AS EXAMPLE2
  ... --other selected columns
FROM MTINFO MT, UTINFO UT
WHERE MT.UTID = UT.UTID 
  AND MT.EXECREADY = 'R'
  AND MT.ISEXEC > 0