How to display rows that when added together equal zero

后端 未结 6 1793
一生所求
一生所求 2021-01-29 01:39

Been searching for a few weeks for a solution to this but have come up blank.

I have table of data similar to this:

client_ref  supplier_key  client_amou         


        
6条回答
  •  星月不相逢
    2021-01-29 01:48

    Try this instead:

    SELECT t1.*
    FROM transactions AS t1
    INNER JOIN
    (
      SELECT
        tbis.client_ref ,
        tbis.supplier_key,
        sum(tbis.client_amount) AS total
      FROM transactions tbis 
      WHERE tbis.client_amount !=0 
      GROUP BY tbis.client_ref, tbis.supplier_key 
      HAVING sum(tbis.client_amount) =0 
    ) AS t2  ON t1.client_ref = t2.client_ref
            AND t1.supplier_key = t2.supplier_key
    ORDER BY t2.total;
    
    • SQL Fiddle Demo

提交回复
热议问题