WITH bombas AS (

    SELECT	
        bc.numero AS numero_bico,
        bc.denominacao AS denominacao_bico,
        MIN(emv.encerrante_inicial) AS encerrante_inicial,
        MAX(emv.encerrante_final) AS encerrante_final,
        SUM(emv.quantidade_aferida) AS quantidade_aferida,
        SUM(emv.desconto) AS desconto,
        SUM(emv.acrescimo) AS acrescimo,
        SUM(emv.quantidade_vendida) AS litros,
        SUM(emv.total_bruto) AS total_bruto,
        SUM(emv.total_venda) AS total_venda
    FROM	movimento_venda_terminal AS mvt
        INNER JOIN encerrante_movimento_venda AS emv ON (emv.id_movimento_venda_terminal = mvt.id_movimento_venda_terminal)
        INNER JOIN bico_combustivel AS bc ON (bc.id_bico_combustivel = emv.id_bico_combustivel)
    WHERE	mvt.data_movimento = :DataMovimento
    AND	mvt.id_empresa = :__ID_EMPRESA
    GROUP BY bc.numero,
        bc.denominacao
    ORDER BY bc.numero
)

SELECT
    1 AS order, 
    'Nº Atual' as tipo,
    numero_bico,
    denominacao_bico,
    encerrante_final AS valor
FROM bombas
UNION ALL
SELECT
    2 AS order, 
    'Aferições' as tipo,
    numero_bico,
    denominacao_bico,
    quantidade_aferida AS valor
FROM bombas
UNION ALL
SELECT
    3 AS order, 
    'Nº Anterior' as tipo,
    numero_bico,
    denominacao_bico,
    encerrante_inicial AS valor
FROM bombas
UNION ALL
SELECT
    4 AS order, 
    'TOTAL = R$' as tipo,
    numero_bico,
    denominacao_bico,
    total_venda AS valor
FROM bombas
