我有一个名称和地址表,想返回一个完整的名称和地址列表,并带有一个额外的列,用于标识该地址是重复地址还是唯一地址。运行更简单的语句时,我可以看到几个重复的地址条目-
SELECT PersonAddressLine1, Count (*)
FROM CompanyTable1
GROUP BY PersonAddressLine1
或
SELECT PersonAddressLine1, Count (*)
FROM CompanyTable1
GROUP BY PersonAddressLine1
HAVING COUNT (*) > 1
但是我需要返回带有列的完整表-
PersonID, PersonName, PersonAddressLine1, AddressVerification (Duplicate / Unique), CompanyID, CompanyName.
PersonID - 1
PersonID - 2
PersonID - 3
PersonName - Ryan
PersonName - Andrew
PersonName - Ben
PersonAddressLine1 - 100 Avenue
PersonAddressLine1 - 100 Avenue
PersonAddressLine1 - 200 Avenue
如果我使用以下case语句,则组条件会影响PersonAddressLine1的计数结果。它显示所有地址都是唯一的。
SELECT PersonID, PersonName, PersonAddressLine1, CompanyID, CompanyName,
CASE WHEN count(*) > 1
THEN CONCAT(PersonName,' ','Address Line 1 is duplicate')
ELSE CONCAT(PersonName,' ','Address Line 1 is unique')
END AS 'Person Address Verification'
FROM CompanyTable1
GROUP by PersonAddressLine1, PersonID, PersonName, CompanyID, CompanyName
答案 0 :(得分:0)
您可以使用窗口函数public class TestRules {
static KieContainer kieContainer;
StatelessKieSession sessionStateless = null;
KieSession sessionStatefull = null;
@BeforeClass
public static void beforeClass() {
kieContainer = KnowledgeSessionHelper.createRuleBase();
}
@Test
public void testCuentasCoactivoLimite(){
sessionStatefull= KnowledgeSessionHelper
.getStatefulKnowledgeSession(kieContainer,"ksession-rules");
Data datos=new Data();
sessionStatefull.setGlobal("datos", datos);
Embargo embargo=new Embargo("e1",LocalDate.of(2018, 11, 24),TipoEmbargo.COACTIVO,new BigDecimal(18000000));
sessionStatefull.insert(embargo);
ArrayList<Persona> personas=new ArrayList<>();
personas.add(new Persona("p1", "e1",TipoIdentificacion.NATURAL, new BigDecimal(6000000)));
personas.add(new Persona("p2", "e1",TipoIdentificacion.NATURAL, new BigDecimal(6000000)));
personas.add(new Persona("p3", "e1",TipoIdentificacion.JURIDICA, new BigDecimal(6000000)));
personas.stream().forEach(x->sessionStatefull.insert(x));
ArrayList<Cuenta> cuentas=new ArrayList<>();
//p1
cuentas.add(new Cuenta("c1","p1","e1",TipoCuenta.AHORROS,SubtipoCuenta.BASICA,LocalDate.of(2014, 05, 26),
new BigDecimal(17000000),EstadoCuenta.ACTIVA));
cuentas.add(new Cuenta("c2","p1","e1",TipoCuenta.AHORROS,SubtipoCuenta.BASICA,LocalDate.of(2015, 05, 26),
new BigDecimal(3000000),EstadoCuenta.ACTIVA));
cuentas.add(new Cuenta("c3","p1","e1",TipoCuenta.CORRIENTE,SubtipoCuenta.BASICA,LocalDate.of(2016, 05, 26),
new BigDecimal(2000000),EstadoCuenta.ACTIVA));
//p2
cuentas.add(new Cuenta("c4","p2","e1",TipoCuenta.AHORROS,SubtipoCuenta.BASICA,LocalDate.of(2016, 05, 26),
new BigDecimal(3000000),EstadoCuenta.ACTIVA));
cuentas.add(new Cuenta("c5","p2","e1",TipoCuenta.CDT,SubtipoCuenta.BASICA,LocalDate.of(2016, 05, 26),
new BigDecimal(1000000),EstadoCuenta.ACTIVA));
cuentas.add(new Cuenta("c6","p2","e1",TipoCuenta.CDAT,SubtipoCuenta.BASICA,LocalDate.of(2016, 05, 26),
new BigDecimal(1000000),EstadoCuenta.ACTIVA));
cuentas.add(new Cuenta("c7","p2","e1",TipoCuenta.ELECTRONICOS,SubtipoCuenta.PUBLICO,LocalDate.of(2016, 05, 26),
new BigDecimal(1000000),EstadoCuenta.ACTIVA));
//p3
cuentas.add(new Cuenta("c8","p3","e1",TipoCuenta.AHORROS,SubtipoCuenta.BASICA,LocalDate.of(2014, 05, 26),
new BigDecimal(37000000),EstadoCuenta.ACTIVA));
cuentas.add(new Cuenta("c9","p3","e1",TipoCuenta.AHORROS,SubtipoCuenta.BASICA,LocalDate.of(2015, 05, 26),
new BigDecimal(3000000),EstadoCuenta.ACTIVA));
cuentas.stream().forEach(x->sessionStatefull.insert(x));
sessionStatefull.fireAllRules();
imprimir(embargo, personas, cuentas);
}
来计算重复次数
COUNT(*) OVER (PARTITION BY PersonAddressLine1)
您当前的查询现在正在做什么,不仅在检查SELECT PersonID, PersonName, PersonAddressLine1, CompanyID, CompanyName,
CASE WHEN COUNT(*) OVER (PARTITION BY PersonAddressLine1) > 1
THEN CONCAT(PersonName,' ','Address Line 1 is duplicate')
ELSE CONCAT(PersonName,' ','Address Line 1 is unique')
END AS 'Person Address Verification'
FROM CompanyTable1
,而且还检查了以下所有内容
PersonAddressLine1