SQL Group由一致性组成

时间:2014-09-11 15:54:48

标签: caching oracle-coherence

我想在Coherence中执行以下查询

Select AVG(Salary)
     , Count(*)
from Employees
group by Company
Having AVG(Salary)> 1000

我正在尝试使用GroupAggregators但是我在引用avarage薪水时遇到问题,因为它没有在Employee Class中定义,而是在Aggregator中定义:

GroupAggregator high_salary = GroupAggregator.createInstance("getDepartment"
                                                  , new DoubleAverage("getSalary")
                                                  , new GreaterFilter("DoubleAverage", 1000));

我该怎么做?

1 个答案:

答案 0 :(得分:0)

您应该使用IdentityExtractor.INSTANCE代替"DoubleAverage"作为GreaterFilter中的值提取器,因为传递给此过滤器的值是avarage薪水本身,而不是某些带有" DoubleAverage&的对象#34;属性。

但是如果你想获得平均工资和数量(这与你的SQL查询一致),你将不得不使用CompositeAggregator。在这种情况下,传递给过滤器的值将不再是数字,而是List,您将必须使用ValueExtractor来从此列表中提取avarage薪水。在Coherence 12.2.1中,它看起来像这样:

    DoubleAverage<Employee> avgSalary = new DoubleAverage<>(Employee::getSalary);
    Count<String, Employee> count = new Count<>();

    CompositeAggregator<String, Country> compositeAggregator = CompositeAggregator
            .createInstance(new InvocableMap.EntryAggregator[] { avgSalary, count });

    ValueExtractor<Object, ? extends Double> salaryExtractor = 
            list -> ((List<Number>) list).get(0).doubleValue();
    Filter having = Filters.greater(salaryExtractor, 1000.0);
    GroupAggregator<String, Country, Employee, String, List> groupAggregator = 
            GroupAggregator.createInstance(Employee::getDepartment, compositeAggregator, having);

也可以在旧版本中完成,但是在不使用lambda表达式的情况下实现salaryExtractor还需要更多的工作。

相关问题