Generate a table from other 5

2

I need help to build a query that brings data from 5 other tables. I will try to be as clear as possible, if information is missing, please let me know.

Come on:

I have the Media table: link

and the Proposals table: link

The other 3 tables are quiet, as I need only bring a field, not needing sums or any other function.

The result should be this:

link

My current query is this:

    SELECT
       data_proposta,
       propostas.source_code,
       sum(midias.custo) as custo,
       COUNT(data_proposta) AS propostas,
       SUM(if(precog_FK <> 9, 1, 0)) AS validas
    FROM propostas
    LEFT JOIN midias
      ON 'data_proposta' = 'midias'.'data'
    GROUP BY data_proposta

I'm having problems when I have to make the sum of the costs of the media table.

    
asked by anonymous 02.05.2014 / 15:43

1 answer

1

I changed the query, it was as follows:

SELECT
    m.data,
    date_format(m.data,'%d-%m-%Y') AS mdata,
    m.midia_origem,
    p.source_code,
    m.campanha,
    COUNT(m.campanha) AS propostas,
    SUM(m.custo) as custo
FROM 
    propostas p
LEFT JOIN
    midias m
    ON m.source_code = p.source_code
GROUP BY
    mdata,m.campanha
ORDER BY 
    mdata ASC, m.source_code ASC

Now I do not understand the need for the following code:

  

SUM (if (precog_FK

02.05.2014 / 16:05