How to create a parameter and use inside a custom sql query in Tableau - tableau-api

I have a list of countries.
I need to create a parameter that can be used inside a custom sql.
For Eg:
List of countries
Argentina,Brazil,Colombia,Ecuador...
The requirement is
User should be able to select multiple countries
Based on that selection, need to run a custom sql that uses SQL IN query or multiple OR.
If user select Argentina and Brazil
Custom sql will be like
select * from players where countries in ('Argentina','Brazil)'
I tried with Set and Filters. But how can I create a parameter based on the selection of filters/Sets?

Parameters are single select only. So in your scenario it is not possible. You could create multiple parameters but that would mean the user would need to pick a country for each otherwise your query would not work. Here are instructions for creating and using parameters in custom sql. https://help.tableau.com/current/pro/desktop/en-us/customsql.htm#use-parameters-in-a-custom-sql-query

Related

How to filter prometheus series based on the results of another query in grafana dashboard?

I am using Grafana 9.3.1 for monitoring of our system. Among other things, I am trying to monitor the remaining FUP of a phone number for each unit we operate.
Basically, we intend to use two data sources.
Database mapping of the unit ID to its phone number (e.g. "unit_id=123, phone_number="00 123456789")
Prometheus time series remaining_fup{phone_number="00 123456789"}. However, remaining_fup is a 3rd party data and does not include unit_id.
In my unit-detail dashboard I have unit_id variable which indicates which unit FUP should be displayed (among other things depending on unit_id)
My original approach was this:
Create a mixed datasource dashboard
Add database datasource as data A. SELECT phone_number FROM units WHERE unit_id='$unit_id'
Add prometheus datasource remaining_fup and filter it based on A.phone_number: remaining_fup{phone_number="${A.phone_number}"}
Unfortunatelly such use of A isn't supported. I used to hope for applying some transformation like Merge or Join by field and then Filter but with no success. After a lot of googling and trying I feel hopeless.
Could you help please? Is such filter even possible? Thanks!
TL;DR: In grafana dashboard I want to query one datasource in order to obtain a value which I subsequently want to use in another datasource query.
1.) Create variable - name phone_number, type: Query and query your database datasource SELECT phone_number FROM units WHERE unit_id='$unit_id'. You can hide this variable if you don't want it to be visible for the dashboard users.
2.) Variable phone_number may have multiple values, so use advance variable formatting to create valid regex query syntax for your prometheus datasource, e.g.
remaining_fup{phone_number=~"${phone_number:pipe}"}
Of course this queries are just examples and they may need some (syntax) tweaking for the use case. Main idea: don't use 2 queries, but one variable and one query (where you use that variable).

How to create queries dynamically based on filters selected in PostgreSQL?

I have a UI where products of various categories will be listed. There will be filters on various attributes which users can select to filter out the products. How should I create a query which will change dynamically based on the selection of filters? Further, once the result set is ready, only few rows to be displayed at a time to the user e.g. 10 rows at a time; value of which again will be decided by the user.
You could write a PL/pgSQL function that takes a JSON with filter fields and values and returns matching rows. But there are libraries for doing that, why not use one of them. What is your UI built on?
On pagination, see Five ways to paginate in Postgres

Sails 1.0 Group By

Now that groupBy is deprecated, how can I mimic a SQL command like SELECT COUNT(*) FROM table GROUP BY xxx using the Waterline ORM ?
This page recommends using .sum() and .avg() but these methods are for number-type columns. Here I want to be able to count the rows of grouped columns, whatever type it is.
I think for specific groupBy query, you've got two choices.
The first one is a 2 step action.
1) Select all the unique element "group by field" you've got in the database.
2) Then count the record for each unique group by field element.
The second one is to use .sendNativeQuery() wich allow you to send a native SQL query to the datastore (you can use this only if you use a real SQL server and not the embedded Sails.JS database)
sendNativeQuery() Documentation

Changing a DB View dynamically according the current user-group

we are currently digging into Amazon Redshift and testing different functionalities.
One of our basic requirements is that we will define different user groups which in turn will be granted access to different views.
One way to go about this would be to implement one view seperately for each user-group. However, since we have a lot of user-groups that share almost the exact same need for information, I'm looking for a way to implement this more dynamically in Redshift.
For instance, let's say I have a user group called users_london and another one called users_berlin. Both will have access to a view called v_employee_master_data which contains the columns employee_name, employee_job_title and employee_city.
Both groups share the same scope of information with one exception - the column employee_city.
In essence, the view should be pre-filtered for a certain value in the column employee_city according to the currently logged-in user-group.
In SQL - something like this:
For the usergroup users_london:
SELECT * FROM v_employee_master_data WHERE employee_city = 'London';
For the usergroup users_berlin:
SELECT * FROM v_employee_master_data WHERE employee_city = 'Berlin';
Now to make the connection back to Amazon Redshift. Does the underlying DB runtime provide an out-of-the-box functionality to somehow catch the currently logged user-group as a form of global variable and alter the SQL-statement according to the value of that variable?
It is possible to do:
get current user
select current_user
find what group it belongs to
select groname from pg_group where current_user_id = any(grolist);
Extract city and capitalize it:
select initcap(substring(groname from 'users_(.*)')) from pg_group where current_user_id = any(grolist);
Now you have your city based on the "user". So just inject it in the view
... WHERE employee_city = initcap(substring(groname from 'users_(.*)') ...

Using all input controls for use in one report

I have a report that uses a query of input controls. It is a drop-down menu of specific ID's (ie. 28, 13, 30...) I want to know if it is possible to have my report go through each of these parameters and populate the report one page at a time for each of them.
Here is an example of the different parameters I want the report to loop through:
Do you want to create a page for all Partner IDs? Or just for the Partner IDs that a user selects? Either is possible.
Using all Partner IDs is probably easier. Modify your report query which probably has something like WHERE partner_id = $P{partnerID}. Remove the where clause. Add grouping and sorting to the query to group on partner id. Each group can start on a new page. And now you don't need a parameter at all.
If you want the user to select the Partner IDs, then you need to use a multi-select input control rather than the single-select input control you're using now. Update your query to use something like this: WHERE $X{IN, partner_id, partnerID}. Add grouping and sorting as above.