Filter sql query results using static values from "Select list" page item in Oracle APEX 5.0 - oracle-apex

I'm looking for a way to filter and display records using static values from a "Select list" page item. I have created a bar chart in APEX 5.0 using the following query:
select to_char(to_date(time_stamp,'YYYY-MM-DD-HH24:MI:SS'),'YYYY-MM-DD-HH24:MI:SS') as label, col2 as value from table1 where :P5_NEW_1 = col1 ;
The time_stamp column of table1 is of datatype varchar2 in my database and contains the date values in the format YYYY-MM-DD-HH24:MI:SS
e.g., values like below are stored in the time_stamp column
2015-08-26-10:17:15
2015-08-26-13:17:15
2015-09-17-12:45:54
2015-09-17-14:12:32
2015-10-06-10:01:42
2015-10-06-11:01:28
2015-10-06-05:01:28
and so on...
I have added a "Select list" item named "interval" on my form that contains a pre-populated list of values like 1hr, 6hrs.
Now I want to modify the above query such that following should happen:
For the value of 1hr selected from the drop down list, the query should check the time_stamp column and display the records for the last one hour
(i.e., records which fall in range to_char(sysdate - 1/24 ,'YYYY-MM-DD-HH24:MI:SS') to to_char(sysdate ,'YYYY-MM-DD-HH24:MI:SS')
And for the value of 6hrs selected from the drop-down list, the query should display records for the last 6 hours.
(i.e., records which fall in range to_char(sysdate - 6/24 ,'YYYY-MM-DD-HH24:MI:SS' to to_char(sysdate ,'YYYY-MM-DD-HH24:MI:SS') )
How do I modify my SQL query to add the above condition in my current SQL query, using static values from page item select list?

Hi you can try this:
SELECT to_char(to_date(time_stamp,'YYYY-MM-DD-HH24:MI:SS'),'YYYY-MM-DD-HH24:MI:SS') AS label, col2 AS VALUE
FROM table1
WHERE :P5_NEW_1 = col1 AND
to_date(time_stamp,'yyyy-mm-dd hh24:mi:ss') BETWEEN
(SYSDATE - (to_number(:SELECT_LIST_ID,'99')/24)) AND SYSDATE;
Just make sure that the static values has a return value equal to hr itself
For example, if static value is equal to 1HR then return value should be 1
:SELECT_LIST_ID is the select list id which holds the static values for the interval.
Hope this helps.

Related

Not Able to filter the Table Power BI

I am learning power BI , for one of my requirement i want to filter table based on the selected value from the slicer and show it in different slicer.
So, here there are 4 slicer and based on selection of one slicer i have to populate the data for the 2nd.
My Table Looks Like
id Name ParentId
1 A null
2 A.1 1
3 A.1.B.1 2
So, i have only 1 table where i have to search the element by id -> parentId and then populate it in the next slicer.
e.g: if We select A then in the next slicer we should show A.1 since , id --> 1(A) = ParentId --> 1(A.1)
I tried to create separate table and then link the id with parentid in the mapping section , this concept is working but not the problem is .
If we select A then in 2nd combo A.1 and A.2 is displaying , but as we click on A.1 on the 2nd combo and then try to click on the elements on 1st combo here in our case A , then the filter is not working properly .
If appending the elements from previous selection + New selection
e.g:
Slicer 1 Slicer 2
A -- (1,2,3)
B -- (4,5,6)
Now , after clicking on A[1st Slicer] it shows (1,2,3) [2nd Slicer]
After clicking on 2 [2nd Slicer] --> showing some elements in [3rd
slicer]
But, now again click on B [1st slicer] --> [4,5,6,2] (Wrong value)
since we selected 2 its appending with the new selection only if we
click on the 2nd slicer.
So, as an alternate solution I tried to filter the selected value which is measure from the table and then show it in the list.
My expression:
Table = FILTER(TableA, TableA[id] == Tableb[selectedId] )
Tableb[selectedId] --> is measure
Table = FILTER(TableA, TableA[id] == "8DE04141-E5B6-49E1-814A-ADB4C6FF5DCF" ) --> selected Id
1st statement is not showing any value but the 2nd giving me the result when i am hard coding value , please suggest me what i can do here.
i want to filter table based on the selected value from the slicer and show it in different slicer.
You don't need DAX for that. You just set up the correct relationships and set filters and slicers in the report.

SQLite how to limit the number of records

I want to limit the number of records in my SQLite table to for example 100 records, and then when I INSERT the 101th record, the first record (the oldest) be removed from the table. In other word, I want to prevent the table from growing more than 100 records and always have the last 100 records. Is there any setting or query with SQLite or should I handle it manually?
thanks in advance
You can do it with a trigger.
Say that your table is this:
CREATE TABLE tablename (
id INTEGER PRIMARY KEY,
name TEXT,
inserted_at TEXT DEFAULT (strftime('%Y-%m-%d %H:%M:%f', 'now'))
);
In the column inserted_at you will have the timestamp of the insertion of each row.
This is not necessary if you declared the column id as:
id INTEGER PRIMARY KEY AUTOINCREMENT
because in this case you could identify the 1st inserted row by the minimum value of the id.
Now create this trigger:
CREATE TRIGGER keep_100_rows AFTER INSERT ON tablename
WHEN (SELECT COUNT(*) FROM tablename) > 100
BEGIN
DELETE FROM tablename
WHERE id = (SELECT id FROM tablename ORDER BY inserted_at, id LIMIT 1);
-- or if you define id as AUTOINCREMENT
-- WHERE id = (SELECT MIN(id) FROM tablename);
END;
END;
Every time that you insert a new row, the trigger will check if the table has more than 100 rows and if it does it will delete the 1st inserted row.
See the demo (for max 3 rows).

Referencing single value of another column in Power Query Editor

I would like to get a single value from "table2.MappedValue" for every record in table1 in Power Query Editor,
I have two tables, that have a many to one relationship, table2 is just a mapping table:
table1: ID | Values
table2: ID | MappedValue
when I try Table.Column(#"table2","MappedValue"), I get a list and not a single value.
I can do that from Table tools-> New Column, but I was wondering if that is possible in Power Query Editor.
You can do this by merging queries. In the query editor go to Home tab and select table1 click on merge and merge with table2. Next step is to expand your new column by selecting the dubble arrow in the column and select the column you want.

How to copy highest value name as new column value in power bi?

I'm trying to create a new column which is filled by the highest value name and condition is today date and if suppose two values are the same then we have to pick the first one value name.
My table is
My Expected table is
Condition is
select screen name has new column value from the most viewed screen by using a count value, selected customer count should be 1 group by id and start date
Best approach here would be that you add a blank query in the query editor. Make the blank query the same table as your first table.
=FirstTableName
Call the blank query SecondTable for example.
Add a filter with the current date.
Group your table by Screen, by count and by the start date. (I think you need a count value that the grouping will work)
Add a sort order for the column count (descanding).
Delete all rows expect the first row.
The result is now one row with the highest value in the count column on todays date.
Join this one row now with your first table. The column screen and the start date are the join columns and expand the screen column from your joined table.
This gives you the result like your second picture.

Add column with hardcoded values in PowerBI

It's probably extremely simple but I can't find an answer. I have created a new column and I would like to use the DAX syntax to fill the column with hardcoded values.
I can write this: Column = 10 and I will get a column of 10s but let's say my table has 3 rows and I would like to insert a column with [10, 17, 155]. How can I do that?
Try using DATATABLE function
Table = DATATABLE("Column Name",INTEGER,{{10},{17},{155}})
You can also put more columns with their own data if you want to, check this
https://learn.microsoft.com/en-us/dax/datatable-function
Assuming your table has a primary key column, say, ID, you could create a new table with just the column you want to manually input.
ID Value
---------
1 10
2 17
3 155
You can create this table either through the Enter Data button or create it using the DAX DATATABLE function as #Deltapimol suggests.
Once you have this table you can create a relationship to your existing table in the data model at which point you can either use this new table in your report to get the values you need or if you really need them in the existing table for some reason, you can pull them over using the RELATED function in a calculated column.
Table1 = GENERATESERIES(1, 3)
Table2 = DATATABLE(
"ID", INTEGER,
"Value" INTEGER,
{{1, 10},{2, 17},{3, 155}}
)
Now you can create a relationship from Table1 to Table2[ID] and then define a calculated column on Table1 as follows:
ValueFromTable2 = RELATED(Table2[Value])
If you don't want to create a relationship, then you could use the LOOKUPVALUE function instead in a calculated column on Table11.