我从Java开始,希望将查询转换为Lambda中的SQL或Java 8中的Stream。
按照下面的查询操作。
SELECT * FROM Correspondencia c, (SELECT entidadeOrigem, MAX(m1) similaridadeMaxima FROM Correspondencia WHERE m1 <> 0 GROUP BY entidadeOrigem) r WHERE c.entidadeOrigem = r.entidadeOrigem AND c.m1 =
r.similaridadeMaxima
我创建了下面带有getters和setters的Correpondencia类:
public class Correspondencia {
int codigo;
String nome;
String entidadeOrigem;
String EntidadeDestino;
double m1;
double m2;
double m3;
double medMax;
double dem;
public int getCodigo() {
return codigo;
}
public void setCodigo(int codigo) {
this.codigo = codigo;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getEntidadeOrigem() {
return entidadeOrigem;
}
public void setEntidadeOrigem(String entidadeOrigem) {
this.entidadeOrigem = entidadeOrigem;
}
public String getEntidadeDestino() {
return EntidadeDestino;
}
public void setEntidadeDestino(String entidadeDestino) {
EntidadeDestino = entidadeDestino;
}
public double getM1() {
return m1;
}
public void setM1(double m1) {
this.m1 = m1;
}
public double getM2() {
return m2;
}
public void setM2(double m2) {
this.m2 = m2;
}
public double getM3() {
return m3;
}
public void setM3(double m3) {
this.m3 = m3;
}
public double getMedMax() {
return medMax;
}
public void setMedMax(double medMax) {
this.medMax = medMax;
}
public double getDem() {
return dem;
}
public void setDem(double dem) {
this.dem = dem;
}
}
我创建了一个列表:
List<Correspondencia> cor = new ArrayList<Correspondencia>();
cor.add(new Correspondencia(41,"Paper_Organization","Paper", "Organization",0,0.14,0.04,0.23,0.08));
cor.add(new Correspondencia(22,"Paper_Organization","Paper", "Organization",0,0.15,0.04,0.23,0.08));
cor.add(new Correspondencia(22,"Paper_Organization","Paper", "Organization",0,0.36,0.04,0.23,0.08));
cor.add(new Correspondencia(11,"email_hasanemail","email", "hasanemail",0.19,0.21,0.19,0.,0.12));
cor.add(new Correspondencia(11,"email_hasanemail","email", "hasanemail",0.25,0.21,0.19,0.,0.12));
cor.add(new Correspondencia(11,"email_hasanemail","email", "hasanemail",0.37,0.21,0.19,0.,0.12));
cor.add(new Correspondencia(31,"Review_Reviewer","Review","Reviewer",0.36,0.5,0.41,0.,0.25));
cor.add(new Correspondencia(31,"Review_Reviewer","Review","Reviewer",0.38,0.5,0.41,0.,0.25));
cor.add(new Correspondencia(31,"Review_Reviewer","Review","Reviewer",0.37,0.5,0.41,0.,0.25));
cor.add(new Correspondencia(32,"email_hasanemail","email", "hasanemail",0.36,0.5,0.41,0.,0.25));
cor.add(new Correspondencia(01,"PaperAbstract_Abstract","PaperAbstract","Abstract", 0.33,0.45,0.32,0.,0.22));
cor.add(new Correspondencia(01,"PaperAbstract_Abstract","PaperAbstract","Abstract", 0.37,0.45,0.32,0.,0.22));
现在我想根据下面的查询用Lambda或Stream过滤课程:
SELECT * FROM Correspondencia c, (SELECT entidadeOrigem, MAX(m1) similaridadeMaxima FROM Correspondencia WHERE m1 <> 0 GROUP BY entidadeOrigem) r WHERE c.entidadeOrigem = r.entidadeOrigem AND c.m1 = r.similaridadeMaxima
这将返回m1的最大值,按entidadeOrigem的项目分组:
22,"Paper_Organization","Paper", "Organization",0,0.36,0.04,0.23,0.08
11,"email_hasanemail","email", "hasanemail",0.37,0.21,0.19,0.,0.12
31,"Review_Reviewer","Review","Reviewer",0.38,0.5,0.41,0.,0.25
01,"PaperAbstract_Abstract","PaperAbstract","Abstract", 0.37,0.45,0.32,0.,0.22
一位同事给了我以下代码:
Correspondencia maxM1 = cor.stream()
.filter(x -> x.getM1() != 0)
.max(Comparator.comparing(x -> x.getM1()))
.get();
System.out.println(maxM1);
但是,它只返回entidadeOrigem的最大m1值。
我需要的是根据下面的SQL返回由entidadeOrigem分组的m1的每个重复项的最高值。
SELECT * FROM Correspondencia c, (SELECT entidadeOrigem, MAX(m1)
similaridadeMaxima FROM Correspondencia WHERE m1 <> 0 GROUP BY
entidadeOrigem) r WHERE c.entidadeOrigem = r.entidadeOrigem AND c.m1 =
r.similaridadeMaxima
首先根据entidadeOrigem
字段分组到Map
List<Correspondencia> result = cor.stream()
.collect(Collectors.groupingBy(Correspondencia::getEntidadeOrigem))
.values()
.stream()
.map(c->c.stream().max(Comparator.comparingDouble(Correspondencia::getM1)))
.filter(p->p.isPresent())
.map(Optional::get)
.collect(Collectors.toList());
我想在JPA 2.1中将“命名查询”转换为“SQL查询”,并在运行之前对其进行更改。 例如,我有一个命名查询:从CU所在的客户中选择CU。代码=?1,我希望在PAR之后获得查询并对其进行转换和编辑(例如添加模式名称)并创建此查询:从db1.cc1cust cu中选择*其中cu.cc1cod=?1. 我该怎么做? 问候
问题内容: 我有很多这样的SQL查询: 这些查询必须在不同的数据库引擎(MySql,Oracle,DB2,MS-Sql,Hypersonic)上运行,所以我只能使用通用的SQL语法。 在这里,我读到,使用MySql时,该语句未经过优化,而且速度确实很慢,因此我想将其切换为。 我试过了: 但这没有考虑到关键字。 问题:如何使用该方法消除重复的行? 问题答案: 要使用JOIN编写此代码,可以使用内部选
问题内容: 我的最后一篇文章是关于在LEFT OUTER JOIN上编写带有条件的SQL查询的最佳方法: LEFT OUTER JOIN带有条件(在哪里排序)? 现在,我需要将该段好的SQL转换成一个不错的Active Record查询(Rails 3)。;-) 我有2个型号: 培训 has_many:training_histories TrainingHistory 所属类别:培训 如何编写范
问题内容: 我想将以下SQL查询转换为Elasticsearch之一。谁能帮上忙 我尝试了以下方法: 但不确定我是否做对了,因为它无法验证结果。似乎要在聚合内添加查询。 问题答案: 假设您使用Elasticsearch 2.x,则有可能在Elasticsearch中 具有 -semantics。我不知道2.0之前的可能性。 您可以使用新的Pipeline Aggregation Bucket Se
我有两个表,它们通过一个外键来维护它们之间的父子关系。查询如下所示。我想在使用jpa的同时使用标准版。所以有人可以帮助我使用标准版吗 表“child”的“notification\u id\u child”列是外键,并引用表“parent”的主键。
问题内容: 多亏了Erwin Brandstetter在我之前的问题“具有has_many关系的订单”中的帮助,我的SQL查询才能正常工作。 如何将该SQL转换为ActiveRecords或AREL查询以在范围中使用? 我最近来的是在朋友的帮助下… …这给了我一个错误: 更新: 我之前的问题对相关的架构和查询有完整的描述。但是基本上Articles have_many Metrics和一个Metr