How to check a checkbox in Apex 18 when its value is present in other table - oracle

I have a checkbox with list of values from table a. I need them to be checked if are present in a column of table b. Do you have any ready solution please colleagues ?
Thx
Adam

See this similar question for some background info.
When you're working with a multi-value checkbox item, you want your Source Query to select a colon-delimited list of the values that you want to be checked in your checkbox. (This is true for multi-value Application Express page items in general.) E.g.
select listagg(my_column, ':') within group (order by my_column)
from TableB
where my_column is the name of the column in TableB that your values are stored in.
If you have a lot of values in TableB (enough that the listagg() above returns more than 4000 characters), you'll need a fancier query, but for most cases it'll work fine.

Related

Does ordered hint in oracle also decides the order of rows in which they are fetched?

I read that 'The ORDERED hint causes Oracle to join tables in the order in which they appear in the FROM clause.'
But does it also fetch the rows in specific order?
For example: If I have ordered hint on column emp_code which has values as 'A','B' and 'C'[lets consider that more than 2 tables are joined to get emp_code ].
Will the output always have the specific order of rows? For example will 'A' always be the first row and 'C' be the last? does it decides the order of rows? and if yes then how?
No. The only thing that controls the order of rows in the final result set is the use of the ORDER BY clause in the SELECT statement. Hints are to influence the access plan chosen by the optimizer, not ordering of the result set.
select emp_id,
emp_name
from emp
order by emp_id -- <this is the only thing that controls the order of rows in the result set
;

Oracle Apex 18 shuttle item show last selected values on right side

I have implemented Shuttle List Successfully Using Dynamic Action with Below Code
declare
tab apex_application_global.vc_arr2;
begin
tab := apex_util.string_to_table (:P14_NEW_1);
for i in 1..tab.count loop
insert into xxtest (COL1, COL2)
values (:P14_NEW, tab(i));
end loop;
commit;
end;
Problem is that each the time user open the forms it don't show the last selected values to Right Hand Side, I didn't understand this logic how can i show selected items as saved In table to Right side when page loads
You did the first part of job - stored selected values into a table.
The next step is to retrieve them back. In order to do that,
navigate to shuttle item's source property
set its type to "SQL Query (return colon separated values)
SQL query should look like this:
select listagg(col2, ':') within group (order by null)
from xxtest
where ... --> include condition, if there is any - I believe it should
set "Used" property to "Always, replacing any existing value in session state
Run the page; shuttle item should now have populated right hand side.
As of the where clause: I don't know what is the purpose of doing what you're doing, but - if you don't distinguish rows stored into the xxtest table, all users will use the same recordset and overwrite each other's data. If xxtest.col1 represents username (so that wouldn't be :P14_NEW but :APP_USER instead), you should use it in WHERE clause. Otherwise, consider doing that.

How to arrange data in SQL?

I am using Oracle SQL Plus.
Please refer to screenshot for reference.
After deletion of data of column sno=4, I want the output of sno to be displayed in serial order ie 1,2,3,4 instead of 1,2,3,5.
Please suggest the SQL query to achieve same.
try:
select ROWNUM as SNO, NAME,DOJ from company order by SNO
If you want the numbering to be persistent, you will need to update the SNO for all the remaining records in the table.
What you have done, by deleting the row is say that "row number 4 no longer exists". If you want to shuffle the remaining rows up, you will need to perform the following:
DELETE FROM company WHERE name='Flipkart';
UPDATE company SET sno=sno-1 WHERE sno>=4;
An alternative, if you don't want to alter the contents of the table itself.. But you want it to display correctly (specifically for this example)
SELECT
CASE
WHEN sno <= 4 THEN sno
ELSE sno-1
END AS sno,
name,
doj
FROM company
ORDER BY sno;

ORA-00979 not a Group By function error

Iam trying to select 2 values from a Table, Employee emp_name, emp_location grouping by emp_location, iam aware that the columns which are in group by function needs to be in select clause, but i would like to know whether is there any other way to get these value in a single query.
My intention is to select only one employee per location based on age.
sample query
select emp_name,emp_location
from Employee
where emp_age=25
group by emp_location
please help in this regard.
Thanks a lot for all the guys who have responded for this question. I will try to learn these windows functions as these are very handy.
The reason why this works in MySQL and not in Oracle, is because in Oracle, as well most other databases, you either need to specify a field (or expression) in the group by clause, or it has to be an aggregation which combines the values of all values in the group into a single one. For instance, this would work:
select max(emp_name),emp_location
from Employee
where emp_age=25
group by emp_location
However, it's may not the best solution. It will work if you want just the name, but you'll get into trouble when you want to have multiple fields for an employee. In that case max won't do the trick. In the query below, you might get a first name that doesn't match the last name.
select max(emp_firstname), max(emp_lastname), emp_location
from Employee
where emp_age=25
group by emp_location
On solution for this, is using a window function (analytical function). With those, you can generate a value for each record, without immediately reducing the number of records. For instance, with a windowed max function, you could select the max age for people named John, and display that value next to every John in the result, even if they don't have that age.
Some functions, like rank, dense_rank and row_number can be used to generate a number for each employee, which you can then use to filter by. In the example below, I created such a counter per location (partition by), and ordered by, in this case name and id. You can specify other fields as well, for instance if you want one name per age per location, you specify both age and location in partition by. If you want the oldest employee of each location, you can remove where emp_age=25 and order by emp_age desc instead.
select
*
from
(select
emp_name, emp_location,
dense_rank() over (partition by emp_location order by emp_name, emp_id) as emp_rank
from Employee
where emp_age=25)
where
emp_rank = 1
ORA-00979 not a Group By function error
Only aggregate functions and columns specified in the GROUP BY clause are allowed in the SELECT clause.
In that regard, Oracle follows the SQL standard closely. But, as you noticed in your comment, some other RDBMS are less strict than Oracle regarding that point. For example, to quote MySQL's documentation (emphasis mine):
MySQL extends the use of GROUP BY so that the select list can refer to nonaggregated columns not named in the GROUP BY clause. [...]
However, this is useful primarily when all values in each nonaggregated column not named in the GROUP BY are the same for each group. The server is free to choose any value from each group, so unless they are the same, the values chosen are indeterminate.
So, in the recommended use case, adding the extra columns to the GROUP BY clause will lead to the same result.
select emp_name,emp_location
-- ^^^^^^^^
-- this is *not* part of the ̀`GROUP BY` clause
from Employee
where emp_state=25
group by emp_location
Maybe are you looking for:
...
group by emp_location, emp_name
select emp_name,emp_location
from Employee
where emp_age=25
group by emp_name,emp_location
or
select max(emp_name) emp_name,emp_location
from Employee
where emp_age=25
group by emp_location

inline view query

I wanted to delete some records which i added recently, from this table mytemp, please tell me what is wrong with this query,
data in selected column had been populated using cursor
DELETE FROM (SELECT ROWNUM RM, S from mytemp) where rm > 20;
error is:
ORA-01732: data manipulation operation not legal on this view
Edited for accuracy...
Here's the description of the error you are getting:
http://ora-01732.ora-code.com/
An attempt was made to use an UPDATE, INSERT, or DELETE statement on a
view that contains expressions or functions or was derived from more
than one table. If a join operation was used to create the view or the
view contains virtual columns derived from functions or expressions,
then the view may only be queried.
So it looks like an updateable view can be substituted for a table, as long as it doesn't join more than one table or use virtual columns. In your case, the problem is the virtual ROWNUM column.
It's the rownum>20 statement.
ROWNUM>x, where x values greater than a positive integer are always false.
select * from ANYTABLE where rownum>(ANY POSITIVE INTEGER)
doesn't return any record.
The first row fetched is assigned a ROWNUM of 1 and makes the condition false. The second row to be fetched is now the first row and is also assigned a ROWNUM of 1 and makes the condition false. All rows subsequently fail to satisfy the condition, so no rows are returned.
Check THIS for further info.
You can do the following:
delete from (select amount from TABLE t where t.amount=1000)
but it's the same as
delete from TABLE where amount=1000

Resources