Where am I going wrong on this select?

Asked

Viewed 67 times

0

I made the Select below only that it is not adding by the correct column, where I am missing?

In my structure I have the field dtConcat and the field dtConcat2 (these dates are different one is for order date and the other is for the date that effected the sale) he is adding the column dtConcat2 even I put in the select that I want the month and year of the column dtConcat

SELECT cat.id, cat.nome_vendedor, MONTH (dtConcat) AS mes, YEAR (dtConcat) AS ano,
SUM( IF( mov.tipo_venda =  'Atacado', mov.total, 0 ) ) AS Atacado
FROM lc_controle AS mov
INNER JOIN vendedor AS cat ON cat.id = mov.vendedor
where mov.exportado = 'Sim'
GROUP BY ano, mes, cat.nome_vendedor 
ORDER BY nome_vendedor, ano, mes

1 answer

1

Good afternoon Robert. Maybe the problem is that Group By is using the name of the renamed columns and within the same query you have to use the exact From fields you are using.

If you separate the code by doing Inner Join first in a complete way, generate a base and then run with group by in a second step work.

However this process will not be so fast because it will split into 2 steps but it works.

  • Sometimes simple things get complicated. kkk Solved like this: SELECT DISTINCT cat.id, cat.salesperson, MONTH (dtConcat) AS dt_mes, YEAR (dtConcat) AS dt_year, SUM( IF( mov.dtConcat , mov.total, 0 ) AS Wholesale FROM lc_control AS mov INNER JOIN seller AS cat ON cat.id = mov.seller Where mov.exporter = 'Yes' GROUP BY cat.salesperson, dt_mes, dt_year ORDER BY salesperson, dtConcat

Browser other questions tagged

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