Generate a table from another 5

Asked

Viewed 100 times

2

I need help to build a query that brings data from 5 other tables. I will try to be as clear as possible, in case information is lacking, please inform me.

Come on:

I have the Midias table: http://jotacomdigital.com.br/imgs/midias.png

and the Proposals table: http://jotacomdigital.com.br/imgs/propostas.png

The other 3 tables is quiet, because I just need to bring a field, not needing sums or any other function.

The result must be this:

http://jotacomdigital.com.br/imgs/resultado.png

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 in trouble when I have to do the sums of the media table costs.

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 don’t understand the need for the following code:

SUM(if(check mark <> 9, 1, 0)) AS validates

If in the table "proposals" the code "souce_code" is unique, the SUM does not make sense, it will always be 1 or 0 because there is only one record.

Another important topic is to create indexes to optimize the query. In the table "media" create the index in the following order: source_code, date, campaign

And in the table "proposed" create the index on the column "source_code".

  • @Felipe I took a test and it didn’t go very well. I changed the query to this: SELECT date_format(a.data_proposed,'%d-%m-%Y') AS data1, date_format(b.data,'%d-%m-%Y') AS data2, a.source_code, sum(b.cost) AS cost, COUNT(a.data_proposed) Proposals, sum(if(a.precog_FK <> 9, 1, 0)) AS validates FROM proposals a, midias b LEFT JOIN midias ON data1 = data2 GROUP BY data_proposition is showing error #1054 - Unknown column 'data1' in 'on clause'

  • @Debarros I apologize, I made a mistake. I changed the query, I put "Midas" and not "midias" in the table name.

  • @Felipe, didn’t roll no man.

  • @So I got your problem wrong, I’m analyzing it better. Is there any connection between the media table and proposals? any id?

  • There is no relationship declared in the database, but there is the code (source_code) that is repeated in the tables and also the dates. I’m thinking the problem might be in SUM().

  • @Check the column "source_code" is unique in the table of proposals?

  • Only in the primarykey sense? No. It can be repeated. But if you want to know if in the proposed table there is another 'source_code', no.There is no such thing.

  • @Debarros created the tables here and changed the post above, see if it solves the problem.

  • The precog_FK, when different from 9 indicates that it is approved, so I want the amount of it. That’s why I use the SUM in the precog_FK.

  • @You must ok, I understand, but if using the information this way will be repeated on all lines, wouldn’t it be better to do a query just for this instead of doing a SUM on all lines? Fewer resources consumed when executing the query. This new query solved the problem?

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.