Examples of QueryAPI Data Transfer Object Supported Functions Mapping to Expression Editor

This page provides examples of how common Expression Editor functions are represented in the QueryAPI Data Transfer Object (QueryApiDTO).

SUM

This example shows how to get SUM of Bonus by Channel from the Transaction Datamart.

JSON Code

JSON
JSON
{
    "data": {
        "query": {
            "kind": "pipeline",
            "stages": [
                {
                    "kind": "source",
                    "table": {
                        "kind": "datamart",
                        "datamartUniqueName": "datamart_transaction",
                        "currency": "EUR"
                    },
                    "columns": [
                        {
                            "kind": "selectable",
                            "alias": "Bonus",
                            "expression": {
                                "kind": "columnReference",
                                "column": "Bonus",
                                "source": "table"
                            }
                        },
                        {
                            "kind": "selectable",
                            "alias": "Channel",
                            "expression": {
                                "kind": "columnReference",
                                "column": "Channel",
                                "source": "table"
                            }
                        }
                    ]
                },
                {
                    "kind": "aggregate",
                    "columns": [
                        {
                            "kind": "selectable",
                            "alias": "Channel",
                            "expression": {
                                "kind": "columnReference",
                                "column": "Channel",
                                "source": "previousStage"
                            }
                        },
                        {
                            "alias": "m1",
                            "kind": "selectable",
                            "expression": {
                                "kind": "function",
                                "name": "sum",
                                "arguments": [        // Sum of Bonus
                                    {
                                        "kind": "columnReference",
                                        "column": "Bonus",
                                        "source": "previousStage"
                                    }
                                ]
                            }
                        }
                    ],
                    "dimensions": [         // Group by Channel
                        {
                            "kind": "columnReference",
                            "column": "Channel",
                            "source": "previousStage"
                        }
                    ]
                }
            ]
        }
    }
}

Highlighted Structure

Highlighted structure

{
"data": {
"query": {
"kind": "pipeline",
"stages": [
{
"kind": "source",
"table": {
"kind": "datamart",
"datamartUniqueName": "datamart_transaction",
"currency": "EUR"
},
"columns": [
{
"kind": "selectable",
"alias": "Bonus",
"expression": {
"kind": "columnReference",
"column": "Bonus",
"source": "table"
}
},
{
"kind": "selectable",
"alias": "Channel",
"expression": {
"kind": "columnReference",
"column": "Channel",
"source": "table"
}
}
]
},
{
"kind": "aggregate",
"columns": [
{
"kind": "selectable",
"alias": "Channel",
"expression": {
"kind": "columnReference",
"column": "Channel",
"source": "previousStage"
}
},
{
"alias": "m1",
"kind": "selectable",
"expression": {
"kind": "function",
"name": "sum",
"arguments": [ // Sum of Bonus
{
"kind": "columnReference",
"column": "Bonus",
"source": "previousStage"
}
]
}
}
],
"dimensions": [ // Group by Channel
{
"kind": "columnReference",
"column": "Channel",
"source": "previousStage"
}
]
}
]
}
}
}

ADD

This example shows how to create a column with value = SUM(Bonus) + SUM(Base) group by Channel.

QueryAPI Data Transfer Object

QueryApiDTO
JSON
{
    "data": {
        "query": {
            "kind": "pipeline",
            "stages": [
                {
                    "kind": "source",
                    "table": {
                        "kind": "datamart",
                        "datamartUniqueName": "datamart_transaction",
                        "currency": "EUR"
                    },
                    "columns": [
                        {
                            "kind": "selectable",
                            "alias": "Bonus",
                            "expression": {
                                "kind": "columnReference",
                                "column": "Bonus",
                                "source": "table"
                            }
                        },
                        {
                            "kind": "selectable",
                            "alias": "Base",
                            "expression": {
                                "kind": "columnReference",
                                "column": "Base",
                                "source": "table"
                            }
                        },
                        {
                            "kind": "selectable",
                            "alias": "Channel",
                            "expression": {
                                "kind": "columnReference",
                                "column": "Channel",
                                "source": "table"
                            }
                        }
                    ]
                },
                {
                    "kind": "aggregate",
                    "columns": [
                        {
                            "kind": "selectable",
                            "alias": "Channel",
                            "expression": {
                                "kind": "columnReference",
                                "column": "Channel",
                                "source": "previousStage"
                            }
                        },
                        {
                            "alias": "m1",
                            "kind": "selectable",
                            "expression": {
                                "kind": "function",
                                "name": "plus",
                                "arguments": [
                                    {
                                        "kind": "function",
                                        "name": "sum",
                                        "arguments": [
                                            {
                                                "kind": "columnReference",
                                                "column": "Bonus",
                                                "source": "previousStage"
                                            }
                                        ]
                                    },
                                    {
                                        "kind": "function",
                                        "name": "sum",
                                        "arguments": [
                                            {
                                                "kind": "columnReference",
                                                "column": "Base",
                                                "source": "previousStage"
                                            }
                                        ]
                                    }
                                ]
                            }
                        }
                    ],
                    "dimensions": [
                        {
                            "kind": "columnReference",
                            "column": "Channel",
                            "source": "previousStage"
                        }
                    ]
                }
            ]
        }
    }
}

SUM(columnAfromPreviousStep + ColumnBfromPreviousStep)

This example shows how to create SUM(Bonus) by Channel and Region as m1, SUM(Base) by Channel and Region as m2. Then Select SUM(m1 + m2).

JSON Code

JSON
JSON
{
    "data": {
        "query": {
            "kind": "pipeline",
            "stages": [
                {
                    "kind": "source",
                    "table": {
                        "kind": "datamart",
                        "datamartUniqueName": "datamart_transaction",
                        "currency": "EUR"
                    },
                    "columns": [
                        {
                            "kind": "selectable",
                            "alias": "Bonus",
                            "expression": {
                                "kind": "columnReference",
                                "column": "Bonus",
                                "source": "table"
                            }
                        },
                        {
                            "kind": "selectable",
                            "alias": "Base",
                            "expression": {
                                "kind": "columnReference",
                                "column": "Base",
                                "source": "table"
                            }
                        },
                        {
                            "kind": "selectable",
                            "alias": "Channel",
                            "expression": {
                                "kind": "columnReference",
                                "column": "Channel",
                                "source": "table"
                            }
                        },
                        {
                            "kind": "selectable",
                            "alias": "Region",
                            "expression": {
                                "kind": "columnReference",
                                "column": "Region",
                                "source": "table"
                            }
                        }
                    ]
                },
                {
                    "kind": "aggregate",
                    "columns": [
                        {
                            "kind": "selectable",
                            "alias": "Channel",
                            "expression": {
                                "kind": "columnReference",
                                "column": "Channel",
                                "source": "previousStage"
                            }
                        },
                        {
                            "kind": "selectable",
                            "alias": "Region",
                            "expression": {
                                "kind": "columnReference",
                                "column": "Region",
                                "source": "previousStage"
                            }
                        },
                        {
                            "alias": "m1",
                            "kind": "selectable",
                            "expression": {
                                "kind": "function",
                                "name": "sum",
                                "arguments": [
                                    {
                                        "kind": "columnReference",
                                        "column": "Bonus",
                                        "source": "previousStage"
                                    }
                                ]
                            }
                        },
                        {
                            "alias": "m2",
                            "kind": "selectable",
                            "expression": {
                                "kind": "function",
                                "name": "sum",
                                "arguments": [
                                    {
                                        "kind": "columnReference",
                                        "column": "Base",
                                        "source": "previousStage"
                                    }
                                ]
                            }
                        }
                    ],
                    "dimensions": [
                        {
                            "kind": "columnReference",
                            "column": "Channel",
                            "source": "previousStage"
                        },
                        {
                            "kind": "columnReference",
                            "column": "Region",
                            "source": "previousStage"
                        }
                    ]
                },
                {
                    "kind": "aggregate",
                    "columns": [
                         {
                            "kind": "selectable",
                            "alias": "Channel",
                            "expression": {
                                "kind": "columnReference",
                                "column": "Channel",
                                "source": "previousStage"
                            }
                        },
                        {
                            "kind": "selectable",
                            "alias": "Region",
                            "expression": {
                                "kind": "columnReference",
                                "column": "Region",
                                "source": "previousStage"
                            }
                        },
                        {
                            "alias": "m1",
                            "kind": "selectable",
                            "expression": {
                                "kind": "function",
                                "name": "sum",
                                "arguments": [
                                    {
                                        "kind": "function",
                                        "name": "plus",
                                        "arguments": [
                                            {
                                                "kind": "columnReference",
                                                "column": "m1",
                                                "source": "previousStage"
                                            },
                                            {
                                                "kind": "columnReference",
                                                "column": "m2",
                                                "source": "previousStage"
                                            }
                                        ]
                                    }
                                ]
                            }
                        }
                    ],
                    "dimensions": [
                        {
                            "kind": "columnReference",
                            "column": "Channel",
                            "source": "previousStage"
                        },
                        {
                            "kind": "columnReference",
                            "column": "Region",
                            "source": "previousStage"
                        }
                    ]
                }
            ]
        }
    }
}

Highlighted Structure

Highlighted structure

{
"data": {
"query": {
"kind": "pipeline",
"stages": [
{
"kind": "source",
"table": {
"kind": "datamart",
"datamartUniqueName": "datamart_transaction",
"currency": "EUR"
},
"columns": [
{
"kind": "selectable",
"alias": "Bonus",
"expression": {
"kind": "columnReference",
"column": "Bonus",
"source": "table"
}
},
{
"kind": "selectable",
"alias": "Base",
"expression": {
"kind": "columnReference",
"column": "Base",
"source": "table"
}
},
{
"kind": "selectable",
"alias": "Channel",
"expression": {
"kind": "columnReference",
"column": "Channel",
"source": "table"
}
},
{
"kind": "selectable",
"alias": "Region",
"expression": {
"kind": "columnReference",
"column": "Region",
"source": "table"
}
}
]
},
{
"kind": "aggregate",
"columns": [
{
"kind": "selectable",
"alias": "Channel",
"expression": {
"kind": "columnReference",
"column": "Channel",
"source": "previousStage"
}
},
{
"kind": "selectable",
"alias": "Region",
"expression": {
"kind": "columnReference",
"column": "Region",
"source": "previousStage"
}
},
{
"alias": "m1",
"kind": "selectable",
"expression": {
"kind": "function",
"name": "sum",
"arguments": [
{
"kind": "columnReference",
"column": "Bonus",
"source": "previousStage"
}
]
}
},
{
"alias": "m2",
"kind": "selectable",
"expression": {
"kind": "function",
"name": "sum",
"arguments": [
{
"kind": "columnReference",
"column": "Base",
"source": "previousStage"
}
]
}
}
],
"dimensions": [
{
"kind": "columnReference",
"column": "Channel",
"source": "previousStage"
},
{
"kind": "columnReference",
"column": "Region",
"source": "previousStage"
}
]
},
{
"kind": "aggregate",
"columns": [
{
"kind": "selectable",
"alias": "Channel",
"expression": {
"kind": "columnReference",
"column": "Channel",
"source": "previousStage"
}
},
{
"kind": "selectable",
"alias": "Region",
"expression": {
"kind": "columnReference",
"column": "Region",
"source": "previousStage"
}
},
{
"alias": "m1",
"kind": "selectable",
"expression": {
"kind": "function",
"name": "sum",
"arguments": [
{
"kind": "function",
"name": "plus",
"arguments": [
{
"kind": "columnReference",
"column": "m1",
"source": "previousStage"
},
{
"kind": "columnReference",
"column": "m2",
"source": "previousStage"
}
]
}
]
}
}
],
"dimensions": [
{
"kind": "columnReference",
"column": "Channel",
"source": "previousStage"
},
{
"kind": "columnReference",
"column": "Region",
"source": "previousStage"
}
]
}
]
}
}
}

IFNULL

This example shows how to select the Channel, if its value is null then show [N/A].

QueryAPI Data Transfer Object

QueryApiDTO
JSON
{
    "data": {
        "query": {
            "kind": "pipeline",
            "stages": [
                {
                    "kind": "source",
                    "table": {
                        "kind": "datamart",
                        "datamartUniqueName": "datamart_transaction",
                        "currency": "EUR"
                    },
                    "columns": [
                        {
                            "kind": "selectable",
                            "alias": "Channel",
                            "expression": {
                                "kind": "function",
                                "name": "coalesce",
                                "arguments": [
                                    {
                                        "kind": "columnReference",
                                        "column": "Channel",
                                        "source": "table"
                                    },
                                    {
                                        "kind": "literal",
                                        "type": "string",
                                        "value": "[N/A]"
                                    }
                                ]
                            }
                        }
                    ]
                },
                {
                    "kind": "distinct"
                }
            ]
        }
    }
}

NULLIF

This example shows how to divide ListPrice by Quantity. In case Quantity is 0 then convert it to null, so divide by null will return null instead of throwing exception divide by 0.

QueryAPI Data Transfer Object

QueryApiDTO
JSON
{
    "data": {
        "query": {
            "kind": "pipeline",
            "stages": [
                {
                    "kind": "source",
                    "table": {
                        "kind": "datamart",
                        "datamartUniqueName": "datamart_transaction",
                        "currency": "EUR"
                    },
                    "columns": [
                        {
                            "kind": "selectable",
                            "alias": "ListPrice/Quantity",
                            "expression": {
                                "kind": "function",
                                "name": "div",
                                "arguments": [
                                    {
                                        "kind": "columnReference",
                                        "column": "ListPrice",
                                        "source": "table"
                                    },
                                    {
                                        "kind": "function",
                                        "name": "nullIf",
                                        "arguments": [
                                            {
                                                "kind": "columnReference",
                                                "column": "Quantity",
                                                "source": "table"
                                            },
                                            {
                                                "kind": "literal",
                                                "type": "integer",
                                                "value": 0
                                            }
                                        ]
                                    }
                                ]
                            }
                        }
                    ]
                },
                {
                    "kind": "distinct"
                }
            ]
        }
    }
}

CUMCONTRIB

This example shows how to use window function to calculate Cumulative Contribution of Bonus by Channel.

  1. Select SUM(Bonus) by Channel.

  2. Use window function to calculate the Cumulative of each Channel value and div by Total value:

    SUM(SUM(bonus)(n) + SUM(bonus)(n-1)) + … + SUM(bonus)(0)) / TOTAL(SUM(Bonus))
    

JSON Code

JSON
JSON
{
    "data": {
        "query": {
            "kind": "pipeline",
            "stages": [
                {
                    "kind": "source",
                    "table": {
                        "kind": "datamart",
                        "datamartUniqueName": "datamart_transaction",
                        "currency": "EUR"
                    },
                    "columns": [
                        {
                            "kind": "selectable",
                            "alias": "Bonus",
                            "expression": {
                                "kind": "columnReference",
                                "column": "Bonus",
                                "source": "table"
                            }
                        },
                        {
                            "kind": "selectable",
                            "alias": "Channel",
                            "expression": {
                                "kind": "columnReference",
                                "column": "Channel",
                                "source": "table"
                            }
                        }
                    ]
                },
                {
                    "kind": "aggregate",
                    "columns": [
                        {
                            "kind": "selectable",
                            "alias": "Channel",
                            "expression": {
                                "kind": "columnReference",
                                "column": "Channel",
                                "source": "previousStage"
                            }
                        },
                        {
                            "alias": "SUMBonus",
                            "kind": "selectable",
                            "expression": {
                                "kind": "function",
                                "name": "sum",
                                "arguments": [
                                    {
                                        "kind": "columnReference",
                                        "column": "Bonus",
                                        "source": "previousStage"
                                    }
                                ]
                            }
                        }
                    ],
                    "dimensions": [
                        {
                            "kind": "columnReference",
                            "column": "Channel",
                            "source": "previousStage"
                        }
                    ]
                },
                {
                    "kind": "selectColumns",
                    "columns": [
                        {
                            "kind": "selectable",
                            "alias": "Channel",
                            "expression": {
                                "kind": "columnReference",
                                "column": "Channel",
                                "source": "previousStage"
                            }
                        },
                        {
                            "kind": "selectable",
                            "alias": "SUMBonus",
                            "expression": {
                                "kind": "columnReference",
                                "column": "SUMBonus",
                                "source": "previousStage"
                            }
                        },
                        {
                            "alias": "CUMCONTRIBBonus",
                            "kind": "selectable",
                            "expression": {
                                "kind": "function",
                                "name": "div",
                                "arguments": [
                                    {
                                        "kind": "windowFunction",
                                        "frame": {
                                            "type": "ROWS",
                                            "start": {
                                                "type": "unbounded",
                                                "direction": "PRECEDING"
                                            },
                                            "end": {
                                                "type": "currentRow"
                                            },
                                            "exclusion": "NONE"
                                        },
                                        "partitions": [],
                                        "orders": [
                                            {
                                                "kind": "order",
                                                "direction": "asc_nulls_first",
                                                "expression": {
                                                    "kind": "columnReference",
                                                    "source": "previousStage",
                                                    "column": "Channel"
                                                }
                                            }
                                        ],
                                        "function": {
                                            "kind": "function",
                                            "name": "sum",
                                            "arguments": [
                                                {
                                                    "kind": "columnReference",
                                                    "source": "previousStage",
                                                    "column": "SUMBonus"
                                                }
                                            ]
                                        }
                                    },
                                    {
                                        "kind": "windowFunction",
                                        "frame": {
                                            "type": "ROWS",
                                            "start": {
                                                "type": "unbounded",
                                                "direction": "PRECEDING"
                                            },
                                            "end": {
                                                 "type": "unbounded",
                                                "direction": "FOLLOWING"
                                            },
                                            "exclusion": "NONE"
                                        },
                                        "partitions": [],
                                        "function": {
                                            "kind": "function",
                                            "name": "sum",
                                            "arguments": [
                                                {
                                                    "kind": "columnReference",
                                                    "source": "previousStage",
                                                    "column": "SUMBonus"
                                                }
                                            ]
                                        }
                                    }
                                ]
                            }
                        }
                    ]   
                }
            ]
        }
    }
}

Highlighted Strucure

Highlighted structure

{
"data": {
"query": {
"kind": "pipeline",
"stages": [
{
"kind": "source",
"table": {
"kind": "datamart",
"datamartUniqueName": "datamart_transaction",
"currency": "EUR"
},
"columns": [
{
"kind": "selectable",
"alias": "Bonus",
"expression": {
"kind": "columnReference",
"column": "Bonus",
"source": "table"
}
},
{
"kind": "selectable",
"alias": "Channel",
"expression": {
"kind": "columnReference",
"column": "Channel",
"source": "table"
}
}
]
},
{
"kind": "aggregate",
"columns": [
{
"kind": "selectable",
"alias": "Channel",
"expression": {
"kind": "columnReference",
"column": "Channel",
"source": "previousStage"
}
},
{
"alias": "SUMBonus",
"kind": "selectable",
"expression": {
"kind": "function",
"name": "sum",
"arguments": [
{
"kind": "columnReference",
"column": "Bonus",
"source": "previousStage"
}
]
}
}
],
"dimensions": [
{
"kind": "columnReference",
"column": "Channel",
"source": "previousStage"
}
]
},
{
"kind": "selectColumns",
"columns": [
{
"kind": "selectable",
"alias": "Channel",
"expression": {
"kind": "columnReference",
"column": "Channel",
"source": "previousStage"
}
},
{
"kind": "selectable",
"alias": "SUMBonus",
"expression": {
"kind": "columnReference",
"column": "SUMBonus",
"source": "previousStage"
}
},
{
"alias": "CUMCONTRIBBonus",
"kind": "selectable",
"expression": {
"kind": "function",
"name": "div",
"arguments": [
{
"kind": "windowFunction",
"frame": {
"type": "ROWS",
"start": {
"type": "unbounded",
"direction": "PRECEDING"
},
"end": {
"type": "currentRow"
},
"exclusion": "NONE"
},
"partitions": [],
"orders": [
{
"kind": "order",
"direction": "asc_nulls_first",
"expression": {
"kind": "columnReference",
"source": "previousStage",
"column": "Channel"
}
}
],
"function": {
"kind": "function",
"name": "sum",
"arguments": [
{
"kind": "columnReference",
"source": "previousStage",
"column": "SUMBonus"
}
]
}
},
{
"kind": "windowFunction",
"frame": {
"type": "ROWS",
"start": {
"type": "unbounded",
"direction": "PRECEDING"
},
"end": {
"type": "unbounded",
"direction": "FOLLOWING"
},
"exclusion": "NONE"
},
"partitions": [],
"function": {
"kind": "function",
"name": "sum",
"arguments": [
{
"kind": "columnReference",
"source": "previousStage",
"column": "SUMBonus"
}
]
}
}
]
}
}
]
}
]
}
}
}