1
They gave me a system of a company to update, they have 3 tables Mysql 'vendaesc, sale, venda_forma_pagamento'. From what I understand the vendaesc table serves to store the items of sale.
I need to create a report that brings me the data of the product sold, quantity sold, unit value, total sale value, form of payment, installments and the problem comes, often in a single sale insert numerous products, But there is only one form of payment for the sale. Payment method information is saved in the Sale table.
The structure of the tables: Sale
`id` int(11) NOT NULL AUTO_INCREMENT,
`Cod` varchar(10) NOT NULL DEFAULT '',
`Data` date DEFAULT NULL,
`Hora` varchar(50) NOT NULL DEFAULT '',
`Desconto` varchar(20) NOT NULL DEFAULT '',
`TotalS` double(10,2) NOT NULL,
`TotalC` double(10,2) NOT NULL,
`Cod_Cliente` varchar(20) NOT NULL DEFAULT '',
`Nome_Cliente` varchar(150) NOT NULL DEFAULT '',
`Mes` varchar(50) NOT NULL DEFAULT '',
`Ano` varchar(4) NOT NULL DEFAULT '',
`forma_pagamento` int(11) DEFAULT NULL,
`forma_pagamento_detalhe` int(11) DEFAULT NULL,
`forma_pagamento_detalhe_nome` varchar(40) NOT NULL DEFAULT '',
`forma_pagamento_parcelas` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
Vendaesc
`Cod` varchar(20) NOT NULL DEFAULT '',
`Venda` varchar(10) NOT NULL DEFAULT '',
`CodTipo` varchar(10) NOT NULL DEFAULT '',
`Tipo` varchar(50) NOT NULL DEFAULT '',
`ObsTipo` varchar(50) NOT NULL DEFAULT '',
`CodProd` varchar(20) NOT NULL DEFAULT '',
`Prod` varchar(50) NOT NULL DEFAULT '',
`referencia` varchar(50) NOT NULL DEFAULT '',
`ValUniProd` varchar(15) NOT NULL DEFAULT '',
`UniProd` varchar(5) NOT NULL DEFAULT '',
`QuantidadeProd` varchar(10) NOT NULL DEFAULT '',
`ValorProd` varchar(20) NOT NULL DEFAULT '',
`Maquina` varchar(50) NOT NULL DEFAULT '',
`Cod_Cliente` varchar(50) NOT NULL DEFAULT '',
`Cliente` varchar(250) NOT NULL DEFAULT '',
`Data` date DEFAULT NULL,
`Mes` varchar(45) NOT NULL DEFAULT '',
`Ano` varchar(4) NOT NULL DEFAULT '',
`Cod_vendedor` varchar(15) NOT NULL DEFAULT '',
`Nome_Vendedor` varchar(155) NOT NULL DEFAULT '',
PRIMARY KEY (`Cod`)
Forma Pagamento
`cod` varchar(20) NOT NULL DEFAULT '',
`descricao` varchar(255) NOT NULL DEFAULT '',
Sql that I tried, 20% of the records returned with wrong data relative to the same or different
SQL = "SELECT *,
IF(venda.cod = vendaesc.Venda and venda.forma_pagamento > 1,'IGUAL','DIFERENTE') AS resultado_agrupa,
venda.TotalS AS total_venda_agrupa,
venda_forma_pagamento.descricao AS nome_forma_pagamento,
vendaesc.QUANTIDADEprod as qtd,
(vendaesc.QUANTIDADEprod * vendaesc.ValUniProd) As total_soma
FROM vendaesc,venda,venda_forma_pagamento
where vendaesc.data='" & DateRat & "' AND
venda.cod = vendaesc.venda AND venda.forma_pagamento =
venda_forma_pagamento.cod order by venda.cod"
Programming is VB6 :(
SQL:
You could prepare an example of data from the three tables (it doesn’t have to be many, something that represents two sales for example) and show how the desired result would be based on the data you use?! It would make it much easier to help you! If you can do even better, create a Sqlfiddle with this example
– Jorge Campos
Oops, thank you for answering, SQL http://sqlfiddle.com/#! 2/8a61f/1/0, the important thing is the field 'resultado_group', return the correct value, if the same sale bring the 'EQUAL' if it is not 'DIFFERENT', Vlw
– sNniffer