SHARE
TWEET

Consulta Mysql

a guest Dec 12th, 2018 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. SELECT
  2.     titulo_id as titulo_id,
  3. -- calculate both SUMs, select values for each dependent by what value it matches to
  4.     SUM(CASE WHEN ativos_extratos.data_import_id = borders.min_id THEN valor_bruto_atual END) AS valor_min,
  5.     SUM(CASE WHEN ativos_extratos.data_import_id = borders.max_id THEN valor_bruto_atual END) AS valor_max,
  6.     data_imports.data_import AS created_at,
  7.     month(data_imports.data_import) AS weeknumber
  8. FROM
  9.     ativos_extratos
  10.         INNER JOIN
  11.     titulos ON titulo_id = titulos.id
  12.         INNER JOIN
  13.     representantes ON representante_id = representantes.id
  14.         INNER JOIN
  15.     data_imports ON data_import_id = data_imports.id
  16. -- moved subquery and condition by it from WHERE
  17.     INNER JOIN ( SELECT
  18.                     MIN(ID) as min_id,
  19.                     MAX(ID) as max_id
  20.                  FROM
  21.                     data_imports
  22.                  WHERE
  23.                     data_import BETWEEN '2018-11-01' AND '2018-12-10'
  24.                  GROUP BY month(data_import)
  25.                ) borders ON ativos_extratos.data_import_id IN (borders.min_id, borders.max_id)
  26. WHERE
  27.     user_id = 1
  28. -- removed subquery and condition were there
  29.         AND data_imports.data_import BETWEEN '2018-11-01' AND '2018-12-10'
  30. GROUP BY titulos.nome_titulo , weeknumber
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top