I want to create a chart from a join sql query between 2 tables in superset.
for example , I go to SQL Lab and execute this query :
select film, count("film") from rental r, payment p where r.rental_id=p.rental_id group by("film") order by count("film") limit 20;
This returns me a result but how to insert in a chart?
How to create chart from SQL query ?
In order to visualize the results from a query executed in SQL Lab, you first need to click on Explore (underneath the Results tab).
Once you are in exploration mode, you can change the "Visualization Type", under "Datasource & Chart Type".
Related
Is it possible to add dynamic pivot columns in the APEX interactive grid query?
PL/SQL function body returning SQL query:
DECLARE
l_piv_col VARCHAR2(1000) := NVL(:P1_DEP_PIVOT,'10 DEP1');
l_statement VARCHAR2(4000):=
q'~
select rownum row_num,JOB,MGR,DEP1,DEP2,DEP3
from
(select JOB,
MGR,
SAL,
DEPTNO
from EMP
)
src
--PIVOT (sum(sal) for deptno IN (10 DEP1,20 DEP2,null DEP3)) Piv
PIVOT (sum(sal) for deptno IN (##PIV_COL##)) Piv
~';
BEGIN
RETURN REPLACE(l_statement,'##PIV_COL##',l_piv_col);
--RETURN l_statement;
END;
P1_DEP_PIVOT -> (10 DEP1, 20 DEP2, null DEP3)
In the pivot deptno in, I need to use the dynamic columns.
sum(sal) for deptno IN (##PIV_COL##)
I am not sure, how to add the dynamic pivot columns in APEX Interactive grid?
Any help is greatly appreciated.
Thanks
Thanks for the reproducible case.
Classic Report is the only APEX Component which supports the query returning a changing set of result columns. Interactive Reports and Interactive Grid do not support this.
In classic report, pick the region source as PL/SQL function body returning SQL query and enable the Generic Columns switch. Then configure the maximum amount of columns, and you're good to go.
There are a couple of workarounds that I can think of if you really need an interactive grid (IG).
Store the results of the query in a collection, create an IG on top of the collection and have a server side condition to hide the columns that should not be displayed
Make sure your query always returns a fixed number of columns and have page item (to manage hide/show) for each of the columns. If a column should not be in the pivot, hide it based on the page item.
I am using PowerBI Desktop (2.96.1061.0) to connect to a local MS SQL server so I can prepare some visualizations. It is important to mention that all data connections (Tables, SQL queries) are using the DirectQuery option.
It's been quite a smooth experience so far. No issues at all. Now I am trying to get some new data, again, through a direct SQL query:
SELECT BillId, string_agg(PGroupName, ', ')
FROM
(SELECT bm.ImportedBillsId as BillId, pg.Name as PGroupName
FROM [BillMp] bm
JOIN [Mps] m on bm.ImportersId = m.Id
JOIN [PGroups] pg on m.PoliticalGroupId = pg.Id
GROUP BY bm.ImportedBillsId, pg.Name) t
GROUP BY BillId
but for some reason, it is not letting me re-create the model and apply the new changes. No matter that the import wizard is able to visualize the actual data prior to the update. This is the error that I am getting:
I have also tried to import only the data from the internal/nested query
SELECT bm.ImportedBillsId as BillId, pg.Name as PGroupName
FROM [BillMp] bm
JOIN [Mps] m on bm.ImportersId = m.Id
JOIN [PGroups] pg on m.PoliticalGroupId = pg.Id
GROUP BY bm.ImportedBillsId, pg.Name
and process (according to this article) the other/outer query through PowerBI but I am still getting the same error.
I have a superset dashboard wherein i have a filter box containing the name of various dimensions such as area, district, country and so on. I want to generate a run time query to compute the value of a specific metric group by each of these dimensions. The user will select a value of the name of each dimension from the filter box and the dash should run the query for example:
SELECT
SUM(REVENUE),
{{dim}}
FROM mytable
GROUP BY {{dim}}
WHERE dim can be 'area' or 'district' or 'country'
How can I achieve this using Jinja templating ? Looking for any examples that would be helpfull ?
You need to use different column names in Filter and your chart.
An example of the same is here:
https://github.com/apache/superset/discussions/15272
I have an interactive grid which has a dynamic action to fetch returns from another table
begin
for c in (select
REW_SIZE,
IN_STOCK,
DESCRIPTION,
FINANCIAL_YEAR_ID
into
:REW_SIZE,
:IN_STOCK,
:DESCRIPTION,
:FINANCIAL_YEAR_ID
from
T_SORDER_PROFOMA_REWINDING
where so_id = :so_id)
loop
:REW_SIZE := c.REW_SIZE;
:IN_STOCK :=c.IN_STOCK;
:DESCRIPTION:=c.DESCRIPTION;
:FINANCIAL_YEAR_ID:=c.FINANCIAL_YEAR_ID;
end loop;
end;
When I had simple select into query, it gave "exact fetch returns more than requested number of rows" thn I applied the above code but it returns only 2nd row. I have 2 rows in the table for this ID.
For what I understand, you want to get some extra data columns. Your best shot is to create a view to join these table and deliver the columns you need. If you need editing, you will have to create an instead of trigger on the view as well, to do a correct dml on one or both tables.
create view which joins both tables
create instead of trigger (if you need editing)
query of the Interactive Grid should be on the view
This solution adds the extra data columns to the source of the IG, which is better and easier in my opinion.
I have created a bar chart on APEX 5.0 where my query looks like below:
select col1 as label,col2 as value from table1 where :P3_NEW_1 = col3;
here: P3_NEW_1 the page item I have created of type "Select List".
The list of values in "Select List" page item are pre-populated in the dropdown using the "Shared component" type I have created and so far this worked fine and I am able to display the results of above query by passing the value through the page item select list.
Now I need to add 2 data pickers on the same Apex page such that I should now be able to filter the results using date picker through dynamic action.
I know that using an additional button I can create a dynamic action but my problem is how do I modify the above query such that following should happen.
During the page load, once I select a particular value from the "Select List", it should display records based on the value selected from dropdown
This I already achieved using above sql query for the bar chart.
Once the bar char is displayed, I should next be able to filter and display the results using date ranges through date pickers.
Here my problem is my bar chart query is same but I need to now pass the start_date and end_date to the same above sql query which but I am not sure how to achieve this. If I add a button for dynamic action I need to select the report region which is the "bar chart" region and here my query needs modification.
Once the bar chart display the results, at the next step how do I filter the results by having the date filters with dynamic action on the same region where bar chart was displayed. How to achieve this?
You can change the query to something like this:
SELECT COL1 AS LABEL,
COL2 AS VALUE
FROM TABLE1
WHERE :P3_NEW_1 = COL3
AND (:P3_START_DATE IS NULL
OR TO_DATE(TIME_STAMP,'YYYY-MM-DD-HH24:MI:SS') BETWEEN :P3_START_DATE AND NVL(:P3_END_DATE,SYSDATE));
Where :P3_Start_date and :P3_End_date are the Start and End date pickers and TIME_STAMP is your column where you store the date.
After modifying the query you can simply add a button where at Behavior>Action select Submit Page.
This way when you click the button, the page will be submitted and chart refreshed.
If you want to take your chart to the next level you can do a partial refresh on it. Here is a short video tutorial of partial refresh on a report but you can apply the same login on your chart.