Problems with queries in mysql 5.6 (mysql)

1

I have the following query:

SELECT 
ncuenta,dcuenta,
(select SUM(saldo) where saldo>0) debe,
(select sum(-saldo) where saldo<0) haber 
FROM tabla GROUP BY NCUENTA ORDER BY NCUENTA;

Which works for me but in warmserver version of mysql 5.7.19 and he does this:

*********************************************************
|   ncuenta   |   dcuenta   |    debe     |    haber    |
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|    101      |    BANCOS   |   16255.00  |    NULL     |
|_____________|_____________|_____________|_____________|
|   410-001   |   INGRESOS  |    NULL     |   5910.00   |
|_____________|_____________|_____________|_____________|
|   410-002   |    OTROS    |    NULL     |   12345.00  |
|_____________|_____________|_____________|_____________|
|   510-001   |   COMPRAS   |   1000.00   |    NULL     |
|_____________|_____________|_____________|_____________|
|   610-001   |   SALARIOS  |   1000.00   |   NULL      |
|_____________|_____________|_____________|_____________| 

Which is exactly what I want.

But on the web server I have version 5.6.39 and when I do the query I get it:

  

Error   SQL query: Documentation

     

SELECT nccount, account, (select SUM (balance) where balance> 0) must,   (select sum (-salt) where balance < 0) have FROM table GROUP BY NCUENTA ORDER BY NCUENTA LIMIT 0, 25   MySQL has said: Documentation

     

1064 - Something is wrong in its near syntax 'where balance> 0) must,   (select sum (-salt) where balance < 0) have FROM tables on line 1

     

I edit to give more information.

the database contains the following data:

... nccount ... account ......should ......to have ...... balance
........ 101 .....BANKS .... 1234.00 ....... 0.00 .... 1234.00
.... 410-001 ...INCOME ....... 0.00 .... 1200.00 ...- 1200.00
........ 101 .....BANKS .... 1200.00 ....... 0.00 .... 1200.00
........ 101 .....BANKS ...... 12.00 ....... 0.00 ...... 12.00

and so on. when I do the query in the warmserver (I do not know why, but if the query works, I do not know if it will be the version or that)

SELECT ncuenta, dcuenta,
(select SUM (balance) where balance > 0) must,
(select sum (-salt) where balance < 0) have  FROM table GROUP BY NCUENTA ORDER BY NCUENTA

....... 101 ....BANKS ...... 2446 ......... 0 ...... 2446
... 410-001 ..INCOME ...... 0.00 ... 1200.00 ..- 1200.00

If I apply the same query with the suggestion that you are giving me then it makes me that the fields must have been equal or I add all the balance fields and it gives me the same result (if it serves me for something else but not for this consultation)

    
asked by Oscar Melendez 22.03.2018 в 22:43
source

1 answer

0

Doing the test, I found another solution

  select ncuenta, dcuenta, 
  sum(if(saldo > 0, saldo, 0)) as debe,
  sum(if(saldo < 0, -saldo, 0)) as haber 
  from cuentas group by ncuenta, dcuenta;

in the only_full_group_by mode, we must add the fields that we have selected, in such a way that they make a unique combination, that is; in the group there will be several values for that column, but there will only be space to show one, so we must indicate how to make those values unique. Reference

    
answered by 23.03.2018 / 17:17
source