KoolReport's Forum

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

Combine Tables that are not linked by a common ID #2240

Open Augustine Arthur opened this topic on on Jul 29, 2021 - 6 comments

Augustine Arthur commented on Jul 29, 2021

Please any suggestion on how to solve this. I have three tables that are not linked by any common ID. The tables names are: MinLicReceived, MinLicProcessed, MinLicGranted. All the three tables have the same list of mineral rights, namely: Mining, Quarry, Salt, Prospecting, Mica etc. With the MinLicReceived we capture numeric data of mineral rights licenses received within each region and also date of receipt With the MinLicProcessed we capture numeric data on the number of licenses processed. The last table which is MinLicGranted, we capture numeric data on the number of mineral licenses processed in a specific region and also date granted. We do not follow through of each licenses from time of receipt to the time granted. We are only interested on how many of each of these mineral rights received, processed and granted within each quarter and within each region.

Now this the format of the report I want to generate:

Sorry for the lengthy description. Please I am ready for further clarification if anything is not clear. Thank you.

Sebastian Morales commented on Jul 30, 2021

I see, here's a solution outline:

1 . In your report setup, select data from each of the 3 tables and group each of them by Mineral Rights, sum(received) (or sum(processed), or sum(granted)) and quarter if you need it into 3 datastores.

2 . In your report view, join the 3 datastores by Mineral Rights column into one datastore with columns of sum(received), sum(processed), sum(granted).

3 . Use Table or DataTables widget to display this datastore and group by quarter if you need it.

Let us know if this approach works for you. Tks,

Augustine Arthur commented on Jul 30, 2021

Dear Sebastian,

Thanks for your quick response I will implement same. The individual mineral rights are actually presented in the table structure as column names. In my report I used Transpose to present the report but I was only able to do for the first table i.e. the MinLicReceived. With the way I have structured the table can I still implement the suggestion? These are snapshot of the original reports I generated.

The MyReport view

The generated pdf report

Sebastian Morales commented on Aug 2, 2021

Can you use the Transpose for other mineral right tables as well? If you can, just transpose all of them, pipe to datastores and join the datastores. If you can't let us know what is the trouble. Tks,

Augustine Arthur commented on Aug 2, 2021

Hi Sebastian! I changed my database structure by combining all the three tables into one table. All the data from the three tables are captured in a single table. The items under the mineral rights are accessed in a drop down menu under table column MineralRights. MinRightsReceived, MinRightsProcesses and MineralRightsGranted are the column names in that single table.

It works perfectly. The only problem is that when I set Multiple to False, anytime I load the page for the first time I get some error message in the Region and Year parameter text fields. However if I click on the submit button the message is cleared. Any solution to that please. These are my files and the screenshot.

After clicking on submit I get this:

MyReport.php <?php //Step 1: Load KoolReport require_once "../load.koolreport.php";

use \koolreport\clients\Bootstrap; use \koolreport\processes\ColumnMeta; use \koolreport\processes\Transpose; use \koolreport\processes\ColumnRename; use \koolreport\core\src\core\Utility; use \koolreport\cube\processes\Cube; //Step 2: Creating Report class class MyReport extends \koolreport\KoolReport

{

use \koolreport\export\Exportable;
use \koolreport\inputs\Bindable;
use \koolreport\inputs\POSTBinding;

use \koolreport\clients\Bootstrap;


protected function defaultParamValues()
{
    return array(
	"years"=>array(2019),
        "Years"=>array(),
        "Region"=>array(),
    
    );
}
 protected function bindParamsToInputs()
{
    return array(
        "Years",
        "Region",
     
    );
}
protected function settings()
{
    return array(
        "dataSources"=>array(
            "data"=>array(
                "connectionString"=>"mysql:host=localhost;dbname=mlnrgh",
                "username"=>"root",
                "password"=>"",
                "charset"=>"utf8"
            ),
        )
    );
}


protected function setup()
{
	$query_params = array();
    if($this->params["Years"]!=array())
    {
        $query_params[":Years"] = $this->params["Years"];
    }
    if($this->params["Region"]!=array())
    {
        $query_params[":Region"] = $this->params["Region"];
$this->src('data')->query("
        select
            MineralRights,
            sum(ApplicationsRcvd) as 'ApplicationsReceived',
			sum(ApplicationsProc) as 'ApplicationsProcessed',
			sum(ApplicationGranted) as 'ApplicationsGranted',
			YEAR(RecDate) as 'Year'
			
          
        from mineralsmgt
		where 1=1
        ".(($this->params["Years"]!=array())?"and YEAR(RecDate) in (:Years)":"")."
        ".(($this->params["Region"]!=array())?"and Region in (:Region)":"")."
         
        GROUP BY Year, Region, MineralRights
    ")->params($query_params)
	//$node->pipe(new Cube(array(
    //    "row" => "MineralRights",
        
   // )))
		
   // ->saveTo($source);

    //Save orginal data
  // $source->pipe($this->dataStore("origin"));
    
    //Pipe through process to get result
// ->pipe(new Transpose())
/*  ->pipe(new ColumnRename(array(
"c0"=>"Mineral Rights",
"c1"=>"Number of Applications Received",

))) */

  ->pipe($this->dataStore("result")); 

}

}

}

MyReport.view.php

<?php

use \koolreport\widgets\koolphp\Table;
use \koolreport\inputs\Select2;
use \koolreport\widgets\google;
use \koolreport\widgets\google\PieChart;
use \koolreport\clients\bootstrap;
use \koolreport\widgets\google\ColumnChart;
use \koolreport\widgets\google\BarChart;

?> <div class="report-content">

<div class="text-center">
    <h1>Minerals Management</h1>
    <p class="lead">Mineral Rights / Licenses</p>
</div>
<form method="post">
    <div class="row">
        <div class="col-md-6">
            <div class="form-group">
                <b>Select Year</b>
                <?php 
                Select2::create(array(
                    "multiple"=>false,
                    "name"=>"Years",
                    "dataSource"=>$this->src("data")->query("
                        select YEAR(RecDate) as Year
                        from mineralsmgt                            
						group by Year
                    "),
                    "attributes"=>array(
                        "class"=>"form-control"
						//"size"=>1
                    )
                ));
                ?>
            </div>    

                               
            <div class="form-group">
                <b>Select Region</b>
                <?php 
                Select2::create(array(
                   "multiple"=>false,
                    "name"=>"Region",
                    "dataSource"=>$this->src("data")->query("
                        select Region
                        from mineralsmgt
                        group by Region
                    ")->params(
                        $this->params["Years"]!=array()?
                        array(":Years"=>$this->params["Years"]):
                        array()
                    ),
                    "attributes"=>array(
                        "class"=>"form-control"
						//"size"=>1
                    )
                ));
                ?>                
            </div>  
            <div class="form-group">
                <button class="btn btn-primary">Submit</button>
				<button formaction="exportLicenses.php" class="btn btn-primary">Download PDF</button>
				
			
            </div>    
        </div>
    </div>
    
</form>

<?php
 Table::create(array(
 
    "dataSource"=>$this->dataStore("result"),
	
	"columns"=>array(
			"MineralRights"=>array(
			"label"=>"Mineral Rights",
			"type"=>"string",
           
			),

//"Region",

           "ApplicationsReceived"=>array(
		   "label"=>"Number of Applications Received",
            "type"=>"number",
		   ),
			"ApplicationsProcessed"=>array(
			"label"=>"Number of Applications Processed",
            "type"=>"number",
			),
			"ApplicationsGranted"=>array(
			"label"=>"Number of Licenses/Leases Granted",
			"type"=>"number",
			),
			
       // "amount"=>array("prefix"=>"$"),
	   
	  //   "Year"=>array("format"=>false)
		

    ),			
		"grouping"=>array(
        "Year"=>array(
            "calculate"=>array(
            "{ApplicationsReceived}"=>array("sum","ApplicationsReceived"),
			"{ApplicationsProcessed}"=>array("sum","ApplicationsProcessed"),
			"{ApplicationsGranted}"=>array("sum","ApplicationsGranted"),
			
		
            ),
            "top"=>"<b>Year {Year}</b>",
            "bottom"=>"<td><b>Total of year {Year}</b></td>
			<td><b>{ApplicationsReceived}</b></td>
			<td><b>{ApplicationsProcessed}</b></td>
			<td><b>{ApplicationsGranted}</b></td>
			
			",
        ),
		
    ),
	"paging"=>array(
        "pageSize"=>25
    ),
    
	"cssClass"=>array(
        "table"=>"table-bordered table-striped table-hover",
)

) )

?>

<i class="fa fa-arrow-down" style="font-size:24px;"></i> <pre style="font-weight:bold"><code>

</code></pre> <i class="fa fa-arrow-down" style="font-size:24px;"></i>

<div style="margin-top:20px;">
    <div class="row">
        
		<div class="col-md-8">
        <?php 
     BarChart::create(array(
    "dataStore"=>$this->dataStore('result'),
	"options"=>array(
            'title' => 'Barchart Showing No. of Applications Received, Processed and Granted',
            'isStacked' => false
            ),		
    "columns"=>array(
        "MineralRights"=>array(
            "label"=>"Mineral Rights",
            "type"=>"string",
           
        ),
        "ApplicationsReceived"=>array(
            "label"=>"Number of Applications Received",
            "type"=>"number",
           
        ),
		"ApplicationsProcessed"=>array(
            "label"=>"Number of Applications Processed",
            "type"=>"number",
           
        ),
		"ApplicationsGranted"=>array(
            "label"=>"Number of Licenses/Leases Granted",
            "type"=>"number",
           
        ),
    ),
    "width"=>"100%",
	
));
        ?>
    </div>
        <div class="col-md-8">
        <?php
        PieChart::create(array(
            "dataSource"=>$this->dataStore("result"),
			"options"=>array(
            'title' => 'Piechart Showing Revenue by Regions',
            ),
		"columns"=>array(
        "MineralRights"=>array(
            "label"=>"Mineral Rights",
            "type"=>"string",
           
        ),
        "ApplicationsReceived"=>array(
            "label"=>"Number of Applications Received",
            "type"=>"number",
           
        ),
		"ApplicationsProcessed"=>array(
            "label"=>"Number of Applications Processed",
            "type"=>"number",
           
        ),
		"ApplicationsGranted"=>array(
            "label"=>"Number of Applications Granted",
            "type"=>"number",
           
        ),
        ),
        
           
            "options"=>array(
                "legend"=>array(
                    "position"=>"right"
                ),
               
            ) 
        ));
        ?>
        </div>
    </div>


</div>

</div>

MyReportPdf.view.php

<?php

use \koolreport\widgets\koolphp\Table;
use \koolreport\inputs\Select2;
use \koolreport\widgets\google;
use \koolreport\widgets\google\PieChart;
use \koolreport\clients\bootstrap;
use \koolreport\widgets\google\ColumnChart;
use \koolreport\widgets\google\BarChart;

?> <html>

<body style="margin:0.5in 1in 0.5in 1in">
    <link rel="stylesheet" href="../../../assets/bs3/bootstrap.min.css" />
    <link rel="stylesheet" href="../../../assets/bs3/bootstrap-theme.min.css" />   
    <div class="page-header" style="text-align:right"><i></i></div>
    <div class="page-footer" style="text-align:right">{pageNum}</div>
    <div class="text-center">
    <div class="report-content">

Minerals Management

Mineral Rights / Licenses for

$this->dataStore("result"), "columns"=>array( "MineralRights"=>array( "label"=>"Mineral Rights", "type"=>"string", ), //"Region", "ApplicationsReceived"=>array( "label"=>"Number of Applications Received", "type"=>"number", ), "ApplicationsProcessed"=>array( "label"=>"Number of Applications Processed", "type"=>"number", ), "ApplicationsGranted"=>array( "label"=>"Number of Licenses/Leases Granted", "type"=>"number", ), ), "grouping"=>array( "Year"=>array( "calculate"=>array( "{ApplicationsReceived}"=>array("sum","ApplicationsReceived"), "{ApplicationsProcessed}"=>array("sum","ApplicationsProcessed"), "{ApplicationsGranted}"=>array("sum","ApplicationsGranted"), ), "top"=>"Year {Year}", "bottom"=>"Total of year {Year} {ApplicationsReceived} {ApplicationsProcessed} {ApplicationsGranted} ", ), ), "paging"=>array( "pageSize"=>25 ), "cssClass"=>array( "table"=>"table-bordered table-striped table-hover", ) ) ) ?>
$this->dataStore('result'), "options"=>array( 'title' => 'Barchart Showing No. of Applications Received, Processed and Granted', 'isStacked' => false ), "columns"=>array( "MineralRights"=>array( "label"=>"Mineral Rights", "type"=>"string", ), "ApplicationsReceived"=>array( "label"=>"Number of Applications Received", "type"=>"number", ), "ApplicationsProcessed"=>array( "label"=>"Number of Applications Processed", "type"=>"number", ), "ApplicationsGranted"=>array( "label"=>"Number of Licenses/Leases Granted", "type"=>"number", ), ), "width"=>"100%", )); ?>
$this->dataStore("result"), "options"=>array( 'title' => 'Piechart Showing Number of Applications Received', ), "columns"=>array( "MineralRights"=>array( "label"=>"Mineral Rights", "type"=>"string", ), "ApplicationsReceived"=>array( "label"=>"Number of Applications Received", "type"=>"number", ), "ApplicationsProcessed"=>array( "label"=>"Number of Applications Processed", "type"=>"number", ), "ApplicationsGranted"=>array( "label"=>"Number of Applications Granted", "type"=>"number", "emphasis"=>true ), ), "options"=>array( "legend"=>array( "position"=>"right" ), "width"=>'100%', ) )); ?>

</html>

Sebastian Morales commented on Aug 4, 2021

If you set the select's multiple to false then its default param value should be a string or null instead of an array:

protected function defaultParamValues()
{
    return array(
	"years"=>"2019", // or null. Not array(2019),

Rgds,

Augustine Arthur commented on Aug 4, 2021

Hi Sebastian,

It has worked perfectly. Thank you.

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
solved

None