1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[SQL] Join two SQL queries to return one response and join same rows

Discussão em 'Outras Linguagens' iniciado por Stack, Outubro 1, 2024 às 12:52.

  1. Stack

    Stack Membro Participativo

    I received an answer like the image below (A) using the following query. How should I change the query to get a response like image B? so that each username has one line.

    SELECT userName,userId, SUM(amount) AS debit, 0 as credit
    FROM debts
    where buyerId != userId
    GROUP BY userName,userId

    UNION ALL

    SELECT buyer,buyerId, 0 as total_debit, SUM(amount) AS credit
    FROM debts
    where buyerId != userId
    GROUP BY buyer,buyerId

    ORDER BY buyerId


    [​IMG]

    Continue reading...

Compartilhe esta Página