gersonab 1 Denunciar post Postado Novembro 26, 2013 Estou com um problema numa query, pois tenho duas tabelas, sendo estas com dois códigos de controle, porem a tabela A tem um dos códigos que repetem , tipo : id - cod1 - cod2 - desc 01 - 001 - 111 - aaa 02 - 002 - 111 - bbb 03 - 003 - 111 - ccc 04 - 004 - 112 - ddd 05 - 005 - 113 - eee Ja na tabela B todos os códigos 111 são considerados o 001 da tabela A para não haver repetição: idb - codb2 - quant 01 - 111 - 3 02 - 112 - 2 03 - 113 - 11 04 - 113 - 1 05 - 112 - 1 06 - 111 - 2 Porém quando eu somo e mostro com a descrição ele ta somando a quantidade da tabela B e multiplicando com a quantidade repetida da tabela A, ficando assim : idb - codb2 - quant - desc 01 - 111 - 15 - aaa 04 - 112 - 3 - ddd 05 - 113 - 12 - eee SELECT A.cod1 , A.cod2, A.desc, B.codb2, SUM(b.quant) As sa FROM A, B WHERE A.cod2 = B.codb2 GROUP BY A.cod2 Espero que tenha entendido. Compartilhar este post Link para o post Compartilhar em outros sites
Motta 645 Denunciar post Postado Novembro 26, 2013 Como "desempata" codb2 da tabela B ?! Compartilhar este post Link para o post Compartilhar em outros sites
gersonab 1 Denunciar post Postado Novembro 26, 2013 Vem de um relatório externo , neste todos os códigos 111 da tabela A são como se fossem um só na B. Para ser mais especifico , mostrando original. o problema esta no código 35 ! DROP TABLE IF EXISTS `novodia`; CREATE TABLE `novodia` ( `id_ndia` int(10) NOT NULL AUTO_INCREMENT, `da_ndia` date NOT NULL DEFAULT '0000-00-00', `prod_ndia` int(6) DEFAULT NULL, `sa_ndia` decimal(10,3) DEFAULT NULL, `custo_ndia` decimal(10,2) DEFAULT NULL, `ven_ndia` decimal(10,2) DEFAULT NULL, `crt_ndia` decimal(10,2) DEFAULT NULL, `tot_ndia` decimal(10,2) DEFAULT NULL, PRIMARY KEY (`id_ndia`) ) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci; INSERT INTO `novodia` VALUES (1,'2013-02-01',35,0.17,32.75,60,5.57,10.2); INSERT INTO `novodia` VALUES (2,'2013-02-01',013,2,1.1,1.8,1.1,1.8); INSERT INTO `novodia` VALUES (3,'2013-02-01',033,8,3.3,5.4,4.4,7.2); INSERT INTO `novodia` VALUES (4,'2013-02-01',042,4,1.65,2.7,2.2,3.6); INSERT INTO `novodia` VALUES (5,'2013-02-01',126,6,2.4,5,3.6,7.5); INSERT INTO `novodia` VALUES (6,'2013-02-01',127,2,1.2,2.5,1.2,2.5); DROP TABLE IF EXISTS `estoque`; CREATE TABLE `estoque` ( `id_est` int(10) NOT NULL AUTO_INCREMENT, `sap_est` int(6) DEFAULT NULL, `cod_est` int(7) DEFAULT NULL, `desc_est` varchar(250) COLLATE latin1_general_ci DEFAULT NULL, `unid_est` decimal(10,3) DEFAULT NULL, PRIMARY KEY (`id_est`) ) ENGINE=MyISAM AUTO_INCREMENT=15 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci ROW_FORMAT=DYNAMIC; INSERT INTO `estoque` VALUES (1,35,1001,'produto1 ',9); INSERT INTO `estoque` VALUES (2,35,1002,'produto2 ',9); INSERT INTO `estoque` VALUES (3,35,1003,'produto3 ',9); INSERT INTO `estoque` VALUES (4,35,1004,'produto4 ',9); INSERT INTO `estoque` VALUES (5,35,1005,'produto5 ',9); INSERT INTO `estoque` VALUES (6,35,1006,'produto6 ',9); INSERT INTO `estoque` VALUES (7,35,1007,'produto7 ',9); INSERT INTO `estoque` VALUES (8,35,1008,'produto8 ',9); INSERT INTO `estoque` VALUES (9,013,1009,'produto9 ',6); INSERT INTO `estoque` VALUES (11,033,1000,'produto10',8); INSERT INTO `estoque` VALUES (12,042,1001,'produto11',8); INSERT INTO `estoque` VALUES (13,126,1002,'produto12',18); INSERT INTO `estoque` VALUES (14,127,1014,'produto13 ',18); $sql2 = mysql_query("SELECT novodia.prod_ndia, SUM(novodia.sa_ndia) AS 'sa' , SUM(novodia.crt_ndia) AS 'crt', SUM(novodia.tot_ndia) AS 'tot', estoque.cod_est , estoque.sap_est, estoque.desc_est FROM novodia, estoque WHERE estoque.sap_est = novodia.prod_ndia GROUP BY estoque.sap_est ORDER BY sa DESC ") or die(mysql_error()); ?> <table width="100%"> <thead> <tr> <th>Código</th> <th>SAP</th> <th>Descrição</th> <th>Saida</th> <th>Custo</th> <th>Venda</th> <th>Lucro</th> </tr> </thead> <?php while($row = mysql_fetch_assoc($sql2)) { $cod = $row['cod_est']; $sap = $row['sap_est']; $prod = $row['prod_ndia']; $ndesc = $row['desc_est']; $sa = $row['sa']; $crt = $row['crt']; $tot = $row['tot']; $luc = $tot - $crt; ?> <tbody> <tr> <td><?php echo $cod; ?></td> <td><?php echo $sap; ?></td> <td><?php echo $ndesc ?></td> <td><?php echo number_format($sa, 3, ',', '.'); ?></td> <td><?php echo 'R$ '.number_format($crt, 2, ',', '.'); ?></td> <td><?php echo 'R$ '.number_format($tot, 2, ',', '.'); ?></td> <td><?php echo 'R$ '.number_format($luc, 2, ',', '.'); ?></td> </tr> </tbody> <?php } ?> </table> Compartilhar este post Link para o post Compartilhar em outros sites
gersonab 1 Denunciar post Postado Novembro 26, 2013 Não sei c é a maneira correta, porém aparentemente esta funcionando. <?php $sql2 = mysql_query("SELECT novodia.prod_ndia, SUM(novodia.sa_ndia) AS 'sa' , SUM(novodia.crt_ndia) AS 'crt', SUM(novodia.tot_ndia) AS 'tot', estoque.cod_est , estoque.sap_est, estoque.desc_est FROM novodia, estoque WHERE estoque.sap_est = novodia.prod_ndia GROUP BY estoque.sap_est ORDER BY sa DESC ") or die(mysql_error()); $sqlest = mysql_query("SELECT sap_est FROM estoque WHERE sap_est = '35'") or die (mysql_error()); $lin = mysql_num_rows($sqlest); ?> <table width="100%"> <thead> <tr> <th>Código</th> <th>SAP</th> <th>Descrição</th> <th>Saida</th> <th>Custo</th> <th>Venda</th> <th>Lucro</th> </tr> </thead> <?php while($row = mysql_fetch_assoc($sql2)) { $sap = $row['sap_est']; if ($sap == '35'){ $cod = '35'; $ndesc = 'Produto1'; $sa1 = $row['sa'] / $lin; $crt1 = $row['crt'] / $lin; $tot1 = $row['tot'] / $lin; $luc1 = $tot1 - $crt1; } else { $cod = $row['cod_est']; $ndesc = $row['desc_est']; $sa1 = $row['sa']; $crt1 = $row['crt']; $tot1 = $row['tot']; $luc1 = $tot1 - $crt1; } ?> <tbody> <tr> <td><?php echo $cod; ?></td> <td><?php echo $sap; ?></td> <td><?php echo $ndesc ?></td> <td><?php echo number_format($sa1, 3, ',', '.'); ?></td> <td><?php echo 'R$ '.number_format($crt1, 2, ',', '.'); ?></td> <td><?php echo 'R$ '.number_format($tot1, 2, ',', '.'); ?></td> <td><?php echo 'R$ '.number_format($luc1, 2, ',', '.'); ?></td> </tr> </tbody> <?php } ?> </table> Compartilhar este post Link para o post Compartilhar em outros sites