KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

LeftJoin result has more rows than left data store #3256

Open Adolfo Alonso opened this topic on on Mar 1 - 3 comments

Adolfo Alonso commented on Mar 1

Hello, I got two different DataStores ( one MySQL and one SQl Server). I'm doing a leftjoin, where the DataStore has 722 rows, but the resulting table has 736 rows, why do you think this is?

MyReport.php

<?php
	// Require autoload.php from koolreport library
	require_once "../../koolreport/core/autoload.php";
	
	use \koolreport\processes\Join;
	
	//Step 2: Creating Report class
	class MyReport extends \koolreport\KoolReport
	{
		use \koolreport\amazing\Theme;
		use \koolreport\inputs\Bindable;
		use \koolreport\inputs\POSTBinding;
		
			protected function defaultParamValues()
			{
				return array(
					"dateRange"=>array(
						date("Y-m-d", strtotime('-7 days')) . " 00:00:00",
						date("Y-m-d", strtotime('-0 days')) . " 23:59:59"
					),
					"customers"=> "",
				);
			}
			
			protected function bindParamsToInputs()
			{
				return array(
				"dateRange"=>"dateRange",
				"customers"=>"customers"
				);
			}
		
		public function settings()
		{
			return array(
				"dataSources"=>array(
					XXX
				)
			);
		}
	    public function setup()
	    {
		$query="
		SELECT
		....
		UPPER(lpa.cartaporte) AS 'Carta Porte',
                ...
                FROM....
		";
		$this->src('automaker')
		->query($query)
		->params(array(
			":customers" => $this->params["customers"],
			":start"=>		$this->params["dateRange"][0],
			":end"=>		$this->params["dateRange"][1]
		))
		->saveTo($plibro)
		->pipe($this->dataStore('libro'));

		$query2="SELECT 
			tg.fecha_guia, 
			tg.num_guia,
			trg.cantidad,
			trg.volumen_m3
			FROM trafico_guia tg
			LEFT JOIN trafico_renglon_guia trg
			ON trg.id_area = tg.id_area
			AND trg.no_guia = tg.no_guia
			WHERE tg.id_area = 3
			AND tg.status_guia <> 'B'";
		$this->src('sqlserver')
		->query($query2)
		->saveTo($pZAM)
		->pipe($this->dataStore('ZAM'));
				
		$this->src("automaker")->query("SELECT nombre, id_planta FROM cat_linde_cargaen")
		->pipe($this->dataStore("planta"));
	    }
    }

MyReport.view.php

<?php 
    use \koolreport\inputs\Select2;
	use \koolreport\inputs\DateRangePicker;
    use \koolreport\chartjs\ColumnChart;
	use \koolreport\datagrid\DataTables;
	date_default_timezone_set("America/Mexico_City");
	$dt = date('dmYHi');
?>
<head>
	<title>Libro Azul</title>
</head>
<div class="container-fluid app-container">
	<div style="margin-bottom:20px;"></div>	
	<div class="card card-accent-primary">
		<div class="card-body smart-title">
			<div class="doc-content"><h1>Libro Azul</h1>
				<p>V.10</p>
				<hr>
				<div class="row">
					<div class="col-md-4">
						<form method="post">	
							<div class="form-group">
								<?php
									DateRangePicker::create(array(
										"name"=>"dateRange"
									))
								?>
							</div>
							<div class="form-group text-center">
								<button class="btn btn-success"><i class="glyphicon glyphicon-refresh"></i>Cargar</button>
							</div>
						</form>
					</div>
				</div>
				<hr>
				<div class="row">
					<div class= "col-md-12 form-group">
						<?php 
						$new_store = $this->dataStore('libro')->leftJoin($this->dataStore('ZAM'),array("Carta Porte"=>"num_guia"));
						DataTables::create(array(
							"dataSource"=>$new_store,

							"cssClass"=> array(
								"td" => "text-center",
								"th" => "text-center",
							),
						)
						);
						?>
					</div>
				</div>
			</div>	
		</div>
	</div>
</div>
Sebastian Morales commented on Mar 1

I think it's normal. The second datastore/table could have multiple rows that are joined with one row of the first one. Thus, the result has more rows.

It's only a problem if a left join result has fewer rows than the left datastore/table.

Martin Parkes commented on Mar 13

Have you tried an inner join instead of a left.

The difference between INNER JOIN and LEFT JOIN. Inner join returns only the matching rows between both the tables, non matching rows are eliminated. Left Join or Left Outer Join returns only the matching rows between both the tables, plus non matching rows from the left table

Sebastian Morales commented on Mar 14

Do you have any specific example where the Datastore's join or leftjoin method returns an incorrect result?

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
None yet

None