KoolReport's Forum

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

Join two datasource #1411

Closed Michele Bassanini opened this topic on on Apr 28, 2020 - 13 comments

Michele Bassanini commented on Apr 28, 2020

Hi, i need to join two datasource that have the same common column "DATA_ORA_PRESENTAZIONE"

Neither works:

$join = new Join( $this->dataStore("domande_presentate_pergiorno"), $this->dataStore("domande_immatricolate"), ["DATA_ORA_PRESENTAZIONE"=>"DATA_ORA_PRESENTAZIONE"]);
        $join->pipe($this->dataStore('domande_presentate_con_immatricolazioni'));
        $newDatastore = $this->dataStore("domande_presentate_pergiorno")->join($this->dataStore("domande_immatricolate"), ["DATA_ORA_PRESENTAZIONE"=>"DATA_ORA_PRESENTAZIONE"]);
        $newDatastore->pipe($this->dataStore('domande_presentate_con_immatricolazioni2'));

First datasource

        $this->src("default")
            ->query("SELECT DATA_ORA_PRESENTAZIONE .....")
            ->pipe(new DateTimeFormat([
                "DATA_ORA_PRESENTAZIONE" => [
                    "from" => "d/m/Y H:i:s",
                    "to"   => "F j, Y",
                ],
            ]))
            ->pipe(new Group([
                "by"    => [
                    "DATA_ORA_PRESENTAZIONE",
                ],
                "count" => "TOTALE_PER_GIORNO",
            ]))
            ->pipe(new AccumulativeColumn([
                'TOTALE_DOMANDE' => 'TOTALE_PER_GIORNO',
            ]))
            ->pipe(new ColumnRename([
                "DATA_ORA_PRESENTAZIONE" => "Giorno",
                "TOTALE_PER_GIORNO"      => "Numero di domande",
            ]))
            ->pipe($this->dataStore("domande_presentate_pergiorno"));

Second datasource

$this->src("default")
            ->query("SELECT max(DATA_ORA_PRESENTAZIONE) DATA_ORA_PRESENTAZIONE ...")
            ->pipe(new DateTimeFormat([
                "DATA_ORA_PRESENTAZIONE" => [
                    "from" => "d/m/Y H:i:s",
                    "to"   => "F j, Y",
                ],
            ]))
            ->pipe(new Group([
                "by"    => [
                    "DATA_ORA_PRESENTAZIONE",
                ],
                "count" => "TOTALE_IMMATRICOLATI_PER_GIORNO",
            ]))
            ->pipe(new ColumnRename([
                "DATA_ORA_PRESENTAZIONE"          => "Giorno",
                "TOTALE_IMMATRICOLATI_PER_GIORNO" => "Numero di domande risultate immatricolate",
            ]))
            ->pipe($this->dataStore("domande_immatricolate"));

What is wrong?

Andrew Borell commented on Apr 28, 2020

Maybe try this

$join = new Join( $this->dataStore("domande_presentate_pergiorno"), $this->dataStore("domande_immatricolate"), array("DATA_ORA_PRESENTAZIONE"=>"DATA_ORA_PRESENTAZIONE"));
        $join->pipe($this->dataStore('domande_presentate_con_immatricolazioni'));

Im sure you are running php 5.4+ but i just thought I would throw it out there.

Michele Bassanini commented on Apr 29, 2020

I'm running PHP 5.6.30 The script you suggested doesn't work. It seems that it doesn't find the key on the two datasource to join. How I can inspect the problem?

KoolReport commented on Apr 29, 2020

Basically, you have renamed column from "DATA_ORA_PRESENTAZIONE" to "Giorno". So the old column name is not available for Join. So you should map array("Giorno"=>"Giorno") in the Join.

Michele Bassanini commented on Apr 29, 2020

I have already tried to comment the columnRename process without success (the example was a mix, my fault). Now I have tried to map array("Giorno"=>"Giorno") in the Join without success :(

KoolReport commented on Apr 29, 2020

Can you put the full setup() function

Michele Bassanini commented on Apr 29, 2020

It seems that the $this->container[i]["data"] is empty for both array in Join class. I don't know where and when it should be initialized.

KoolReport commented on Apr 29, 2020

Please give me the full source of your setup() function.

Michele Bassanini commented on Apr 29, 2020

Here it is

class MyReport extends \koolreport\KoolReport
{
    use \koolreport\yii2\Friendship;

    // By adding above statement, you have claim the friendship between two frameworks
    // As a result, this report will be able to accessed all databases of Yii2
    // There are no need to define the settings() function anymore
    // while you can do so if you have other datasources rather than those
    // defined in Laravel.

    function setup()
    {
        $idLista = \Yii::$app->user->identity->contesto;

        $this->src("default")
            ->query("SELECT DATA_ORA_PRESENTAZIONE
                        FROM DOMANDE D,
                            LISTE_SCAGLIONI SCA,
                            LISTE L
                        WHERE D.ID_SCAGLIONE = SCA.ID
                        AND SCA.ID_LISTA = L.ID
                        AND FLG_ANNULLATA = 1
                        AND L.ID = $idLista")
            ->pipe(new DateTimeFormat([
                "DATA_ORA_PRESENTAZIONE" => [
                    "from" => "d/m/Y H:i:s",
                    "to"   => "F j, Y",
                ],
            ]))
            ->pipe(new Group([
                "by"    => [
                    "DATA_ORA_PRESENTAZIONE",
                ],
                "count" => "TOTALE_PER_GIORNO",
            ]))
            ->pipe(new AccumulativeColumn([
                'TOTALE_DOMANDE' => 'TOTALE_PER_GIORNO',
            ]))
            ->pipe(new ColumnRename([
                "DATA_ORA_PRESENTAZIONE" => "Giorno",
                "TOTALE_PER_GIORNO"      => "Numero di domande",
            ]))
            ->pipe($this->dataStore("domande_presentate_pergiorno"));

        $this->src("default")
            ->query("SELECT DATA_APERTURA
                        FROM DOMANDE D,
                            LISTE_SCAGLIONI SCA,
                            LISTE L
                        WHERE D.ID_SCAGLIONE = SCA.ID
                        AND SCA.ID_LISTA = L.ID
                        AND FLG_ANNULLATA = 1
                        AND L.ID = $idLista")
            ->pipe(new DateTimeFormat([
                "DATA_APERTURA" => [
                    "from" => "d/m/Y H:i:s",
                    "to"   => "F j, Y",
                ],
            ]))
            ->pipe(new Group([
                "by"    => [
                    "DATA_APERTURA",
                ],
                "count" => "TOTALE_PER_SCAGLIONE",
            ]))
            ->pipe(new AccumulativeColumn([
                'TOTALE_DOMANDE' => 'TOTALE_PER_SCAGLIONE',
            ]))
            ->pipe(new ColumnRename([
                "DATA_APERTURA"        => "Inizio scaglione",
                "TOTALE_PER_SCAGLIONE" => "Numero di domande",
            ]))
            ->pipe($this->dataStore("domande_presentate_perscaglione"));

        $this->src("default")
            ->query("SELECT max(DATA_ORA_PRESENTAZIONE) DATA_ORA_PRESENTAZIONE
                        FROM DOMANDE D,
                            LISTE_SCAGLIONI SCA,
                            LISTE L,
                            V_IMMATRICOLATI_ANNO_CORR IMM
                        WHERE D.ID_SCAGLIONE = SCA.ID
                        AND SCA.ID_LISTA = L.ID
                        AND D.COD_FISC = IMM.COD_FISC
                        AND L.CDS_COD = IMM.CDS_COD
                        AND L.ID = $idLista
                        GROUP BY D.COD_FISC")
            ->pipe(new DateTimeFormat([
                "DATA_ORA_PRESENTAZIONE" => [
                    "from" => "d/m/Y H:i:s",
                    "to"   => "F j, Y",
                ],
            ]))
            ->pipe(new Group([
                "by"    => [
                    "DATA_ORA_PRESENTAZIONE",
                ],
                "count" => "TOTALE_IMMATRICOLATI_PER_GIORNO",
            ]))
            ->pipe(new ColumnRename([
                "DATA_ORA_PRESENTAZIONE"          => "Giorno",
                "TOTALE_IMMATRICOLATI_PER_GIORNO" => "Numero di domande risultate immatricolate",
            ]))
            ->pipe($this->dataStore("domande_immatricolate"));

        $join = new Join( $this->dataStore("domande_presentate_pergiorno"), $this->dataStore("domande_immatricolate"), array("Giorno"=>"Giorno"));
        $join->pipe($this->dataStore('domande_presentate_con_immatricolazioni'));

        $newDatastore = $this->dataStore("domande_presentate_pergiorno")->join($this->dataStore("domande_immatricolate"), ["Giorno"=>"Giorno"]);
        $newDatastore->pipe($this->dataStore('domande_presentate_con_immatricolazioni2'));
    }
}
KoolReport commented on Apr 29, 2020

Please try this:

class MyReport extends \koolreport\KoolReport
{
    use \koolreport\yii2\Friendship;

    // By adding above statement, you have claim the friendship between two frameworks
    // As a result, this report will be able to accessed all databases of Yii2
    // There are no need to define the settings() function anymore
    // while you can do so if you have other datasources rather than those
    // defined in Laravel.

    function setup()
    {
        $idLista = \Yii::$app->user->identity->contesto;

        $this->src("default")
            ->query("SELECT DATA_ORA_PRESENTAZIONE
                        FROM DOMANDE D,
                            LISTE_SCAGLIONI SCA,
                            LISTE L
                        WHERE D.ID_SCAGLIONE = SCA.ID
                        AND SCA.ID_LISTA = L.ID
                        AND FLG_ANNULLATA = 1
                        AND L.ID = $idLista")
            ->pipe(new DateTimeFormat([
                "DATA_ORA_PRESENTAZIONE" => [
                    "from" => "d/m/Y H:i:s",
                    "to"   => "F j, Y",
                ],
            ]))
            ->pipe(new Group([
                "by"    => [
                    "DATA_ORA_PRESENTAZIONE",
                ],
                "count" => "TOTALE_PER_GIORNO",
            ]))
            ->pipe(new AccumulativeColumn([
                'TOTALE_DOMANDE' => 'TOTALE_PER_GIORNO',
            ]))
            ->pipe(new ColumnRename([
                "DATA_ORA_PRESENTAZIONE" => "Giorno",
                "TOTALE_PER_GIORNO"      => "Numero di domande",
            ]))
            ->saveTo($domande_presentate_pergiorno)
            ->pipe($this->dataStore("domande_presentate_pergiorno"));

        $this->src("default")
            ->query("SELECT DATA_APERTURA
                        FROM DOMANDE D,
                            LISTE_SCAGLIONI SCA,
                            LISTE L
                        WHERE D.ID_SCAGLIONE = SCA.ID
                        AND SCA.ID_LISTA = L.ID
                        AND FLG_ANNULLATA = 1
                        AND L.ID = $idLista")
            ->pipe(new DateTimeFormat([
                "DATA_APERTURA" => [
                    "from" => "d/m/Y H:i:s",
                    "to"   => "F j, Y",
                ],
            ]))
            ->pipe(new Group([
                "by"    => [
                    "DATA_APERTURA",
                ],
                "count" => "TOTALE_PER_SCAGLIONE",
            ]))
            ->pipe(new AccumulativeColumn([
                'TOTALE_DOMANDE' => 'TOTALE_PER_SCAGLIONE',
            ]))
            ->pipe(new ColumnRename([
                "DATA_APERTURA"        => "Inizio scaglione",
                "TOTALE_PER_SCAGLIONE" => "Numero di domande",
            ]))
            ->pipe($this->dataStore("domande_presentate_perscaglione"));

        $this->src("default")
            ->query("SELECT max(DATA_ORA_PRESENTAZIONE) DATA_ORA_PRESENTAZIONE
                        FROM DOMANDE D,
                            LISTE_SCAGLIONI SCA,
                            LISTE L,
                            V_IMMATRICOLATI_ANNO_CORR IMM
                        WHERE D.ID_SCAGLIONE = SCA.ID
                        AND SCA.ID_LISTA = L.ID
                        AND D.COD_FISC = IMM.COD_FISC
                        AND L.CDS_COD = IMM.CDS_COD
                        AND L.ID = $idLista
                        GROUP BY D.COD_FISC")
            ->pipe(new DateTimeFormat([
                "DATA_ORA_PRESENTAZIONE" => [
                    "from" => "d/m/Y H:i:s",
                    "to"   => "F j, Y",
                ],
            ]))
            ->pipe(new Group([
                "by"    => [
                    "DATA_ORA_PRESENTAZIONE",
                ],
                "count" => "TOTALE_IMMATRICOLATI_PER_GIORNO",
            ]))
            ->pipe(new ColumnRename([
                "DATA_ORA_PRESENTAZIONE"          => "Giorno",
                "TOTALE_IMMATRICOLATI_PER_GIORNO" => "Numero di domande risultate immatricolate",
            ]))
            ->saveTo($domande_immatricolate)
            ->pipe($this->dataStore("domande_immatricolate"));

        $join = new Join($domande_presentate_pergiorno,$domande_immatricolate, array("Giorno"=>"Giorno"));
        $join->pipe($this->dataStore('domande_presentate_con_immatricolazioni'));
    }
}

In the view, please try to test your $this->dataStore('domande_presentate_con_immatricolazioni') with Table.

Michele Bassanini commented on Apr 29, 2020

It works! Thank you, so in general it will be useful to save the data in a variable?

KoolReport commented on Apr 29, 2020

Normally the dataStore is the end, storing result only. We are not recommend to continue piping data from a datastore. So we save the last process just before reaching datastore and perform the join of two nodes.

Michele Bassanini commented on Apr 29, 2020

Perfect, thank you for the explanation, maybe update the documentation with this suggestion.

KoolReport commented on Apr 29, 2020

We will do!

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