Onde estou errando nesse select?

1 resposta
R

Fiz o Select abaixo só que ele não está somando pela coluna correta, onde estou errando?

Na minha estrutura tenho o campo dtConcat e o campo dtConcat2 (essas datas são diferentes uma é para data do pedido e a outra é para a data que efetivou a venda) ele está somando a coluna dtConcat2 mesmo eu colocando no select que quero o mes e ano da coluna 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

Segue a estrutura das tabelas.

lc_controle

CREATE TABLE `lc_controle` (
  `id` int(11) NOT NULL,
  `dtConcat` date NOT NULL,
  `dtConcat2` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `clienteVa` varchar(100) NOT NULL,
  `fornec` varchar(100) DEFAULT NULL,
  `clienteAt` varchar(100) NOT NULL,
  `userml` varchar(50) DEFAULT NULL,
  `rastreador` text,
  `entrega` text,
  `total` decimal(20,2) DEFAULT NULL,
  `frete` decimal(20,2) DEFAULT NULL,
  `frete_real` decimal(20,2) DEFAULT NULL,
  `nf` varchar(50) DEFAULT NULL,
  `origem` varchar(50) DEFAULT NULL,
  `tipo_pagto` text,
  `descricao` text,
  `tipo` int(11) DEFAULT NULL,
  `tipo_venda` text,
  `vendedor` int(11) DEFAULT NULL,
  `exportado` varchar(11) DEFAULT NULL,
  `pago` varchar(11) NOT NULL,
  `incluido_por` varchar(25) NOT NULL,
  `date_incluido` datetime NOT NULL,
  `alterado_por` varchar(25) NOT NULL,
  `date_alterado` datetime NOT NULL,
  `exportado_por` varchar(25) NOT NULL,
  `date_exportado` datetime NOT NULL,
  `st` decimal(20,2) NOT NULL,
  `total_st` decimal(20,2) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

vendedor

CREATE TABLE `vendedor` (
  `id` int(11) NOT NULL,
  `data2` date NOT NULL,
  `nome_vendedor` text NOT NULL,
  `descricao` text NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

1 Resposta

Jonathan_Medeiros

Acredito que você tem de especificar na cláusula where do select a data ou período que você quer que seja calculado, por que até o momento o único critério é que a movimentação esteja como “Sim” na coluna exportado.

Obs: poderia ter colocado o seu post em banco de dados ao invés de assuntos gerais, fica muito mais fácil da galera do fórum contribuir com sua dúvida.

Criado 27 de abril de 2018
Ultima resposta 27 de abr. de 2018
Respostas 1
Participantes 2