Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 与case语句相同,SUM结果值也会重复_Java_Sql_Hibernate_Postgresql_Sum - Fatal编程技术网

Java 与case语句相同,SUM结果值也会重复

Java 与case语句相同,SUM结果值也会重复,java,sql,hibernate,postgresql,sum,Java,Sql,Hibernate,Postgresql,Sum,我使用posgresql作为数据库,java作为hibernate的编程语言。我的问题是这个问题: select cast(sum(CASE WHEN p.nropack > 0 THEN p.nropack ELSE 0 END) as integer), cast(sum(CASE WHEN p.nropack < 0 THEN p.nropack ELSE 0 END) as integer), cast(p.fechareg as date) from pronostico

我使用posgresql作为数据库,java作为hibernate的编程语言。我的问题是这个问题:

select cast(sum(CASE WHEN p.nropack > 0 THEN p.nropack ELSE 0 END) as integer),
cast(sum(CASE WHEN p.nropack < 0 THEN p.nropack ELSE 0 END) as integer),
cast(p.fechareg as date) 
from pronostico p 
inner join aeropuerto a on (a.idaeropuerto=p.idaeropuerto)
inner join ciudad c on (a.idciudad=c.idciudad)
inner join pais ps on (ps.idpais=c.idpais)
inner join continente ct on (ct.idcontinente=ps.idcontinente)
where c.idciudad=105
group by cast (p.fechareg as date);
但当我在程序中使用它时:

public ArrayList<RepKardex> listarKardex(int ciud){  
    ciud=105; 
    ArrayList<RepKardex> listaKardex = new ArrayList<>();
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction tx = session.beginTransaction();        
    String q = "select cast(sum( case when p.nropack > 0 then p.nropack ELSE 0 end ) as integer), "
            + "cast(sum( case when p.nropack < 0 then p.nropack ELSE 0 end ) as integer), "
            + "cast(p.fechareg as date) "
            + "from Pronostico p "
            + "inner join Aeropuerto a on (p.idaeropuerto = a.idaeropuerto) "
            + "inner join Ciudad c on (a.idciudad = c.idciudad) "
            + "inner join Pais ps on (c.idpais = ps.idpais) "
            + "inner join Continente ct on (ct.idcontinente = ps.idcontinente) "
            + "where c.idciudad = :ciud "
            + "group by cast(p.fechareg as date) ";                    
    Query query = session.createSQLQuery(q);        
    query.setInteger("ciud", ciud);
    List lista = query.list();        
    Iterator iter = lista.iterator(); 
    while (iter.hasNext()) {    
        Object[] row = (Object[]) iter.next();            
        if (row!=null){
            System.out.println("entrantes : "+(Integer)row[0]); 
            System.out.println("salientes : "+(Integer)row[1]); 
            RepKardex rep = new RepKardex((int)row[0],(int)row[1],(Date)row[2]);              
            listaKardex.add(rep);
        }

    }

    tx.commit();
    session.close();

    return listaKardex;
} 

有人能帮我弄清楚为什么即使在查询中使用case语句,它也会重复正数吗?提前感谢。

您可以通过以下方式简化查询:

除此之外,它看起来还不错。我看第二列不可能返回正数。这里肯定出了什么问题

编辑:
有助于发现客户机代码似乎被相同的列名弄糊涂了(两个sum列都默认为
“sum”
)。显式列别名似乎可以解决这个问题。

我不熟悉java,但如果查询返回您期望的值,可能问题在于您处理返回值的方式,您是否可以尝试将行的内容作为一个整体打印,而不是打印行[0]或行[1]看到结果了吗?我看到一些系统在有多个同名列(sum)时会感到困惑,但我不知道这是否是导致问题的原因。你能试着看看当你显式地命名你的列时会发生什么吗<代码>选择cast(sum(当p.nropack>0时,则p.nropack ELSE 0结束)作为整数)作为sum1。。。总之,…@hvd谢谢!,这就解决了问题。我不知道为什么会发生这种情况,但我认为这是因为没有列的显式名称,它可能会将两个和函数视为一个?。好的,再次感谢你和另一位帮助我的人。谢谢。这简化了查询,尽管当我将其放入程序时,强制转换不起作用,因为hibernate使用字符“:”来调用查询中的参数。不过我用的是最大的和最小的
public ArrayList<RepKardex> listarKardex(int ciud){  
    ciud=105; 
    ArrayList<RepKardex> listaKardex = new ArrayList<>();
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction tx = session.beginTransaction();        
    String q = "select cast(sum( case when p.nropack > 0 then p.nropack ELSE 0 end ) as integer), "
            + "cast(sum( case when p.nropack < 0 then p.nropack ELSE 0 end ) as integer), "
            + "cast(p.fechareg as date) "
            + "from Pronostico p "
            + "inner join Aeropuerto a on (p.idaeropuerto = a.idaeropuerto) "
            + "inner join Ciudad c on (a.idciudad = c.idciudad) "
            + "inner join Pais ps on (c.idpais = ps.idpais) "
            + "inner join Continente ct on (ct.idcontinente = ps.idcontinente) "
            + "where c.idciudad = :ciud "
            + "group by cast(p.fechareg as date) ";                    
    Query query = session.createSQLQuery(q);        
    query.setInteger("ciud", ciud);
    List lista = query.list();        
    Iterator iter = lista.iterator(); 
    while (iter.hasNext()) {    
        Object[] row = (Object[]) iter.next();            
        if (row!=null){
            System.out.println("entrantes : "+(Integer)row[0]); 
            System.out.println("salientes : "+(Integer)row[1]); 
            RepKardex rep = new RepKardex((int)row[0],(int)row[1],(Date)row[2]);              
            listaKardex.add(rep);
        }

    }

    tx.commit();
    session.close();

    return listaKardex;
} 
entrantes: 30
salida:    30
SELECT sum(GREATEST(p.nropack, 0))::int AS entrantes
      ,sum(LEAST(p.nropack, 0))::int AS salida
      ,p.fechareg::date AS fechareg
from   pronostico  p 
JOIN   aeropuerto  a ON a.idaeropuerto = p.idaeropuerto
JOIN   ciudad      c ON c.idciudad = a.idciudad
JOIN   pais       ps ON ps.idpais = c.idpais
JOIN   continente ct ON ct.idcontinente = ps.idcontinente
where  c.idciudad = 105
GROUP  BY p.fechareg::date;