How to change Select List values based on other Select List value using oracle apex? - oracle-apex-5

I have two select lists in my page, Region and Country.
Country should display values based on the value selected from the Region list. But the problem is, it's displaying blank in the Country list, even after selecting a value from the Region list.
Region table Country table
Id Group_Id
Name Name
Id
For Region, I have written the query as follows:
Select Name d,Id r from Regions
The query for Country as:
Select Name d,Id r from Country where Group_Id = :P3_REGION
How can I get the correct values?

P3_REGION should be entered as Cascading LOV Parent Item(s) of the Country SelectList.

Related

SQL Function Get Column By Reference

Let's say there are 3 tables: Genders, Countries and Users. Users has among others a column named Gender and also a column named Country.
I want to add a new entry to Users and select one of the columns Male/Female {M, F} and a country from the rows of Countries dynamically just for testing purposes.
insert into dbo.[Users] (Gender, UserName, Country)
select Genders.Male, 'newbie', Countries.(the one which matches column CountryId) FROM Genders, Countries
I want to achieve this: 'M', 'newbie', MyCountry
After applying the suggestion from 'zip' I get the result 2 rows affected, same number as the external tables I'm referencing. The Query added 2 rows, so I guess I am missing the WHERE conditions.
The Genders Table is meant as a Property and will have only one row that I manually added.
Table Genders; 1 row
Male : uniqueidentifier
Female : uniqueidentifier
Countries is a table with many rows, so I want to select one matching the criteria of one of it's column values, say the value of column CountryId.
You can get a random value using:
insert into dbo.[Users] (Gender, UserName)
select top (1) g.Male, 'newbie'
from Genders.Male
order by newid();
I figuored the solution out. Thank you all who responded for pointing me in the right direction.
INSERT INTO dbo.[Users] (Gender, UserName, Country)
SELECT Genders.Male, 'newbie', Countries.CountryId
FROM Genders, Countries
WHERE Countries.ISO2 = '##'
'##' = 2 letter ISO of country.
Explanation:
1. INSERT INTO dbo.[Users] = Add a new row to Table "Users"
2. (Gender, UserName, Country) = Includes the columns in table "Users". If a column is not specified and it had a property of not allowing Nulls, an exception would be thrown!
3. FROM Genders, Countries = Include the external tables associated to look for
4. WHERE Countries.ISO2 = '##' = This condition allows you to precisely select which column of which row you seek. So I searched for a certain row where a column named ISO2 had a value '##' and looked then for the CountryId value in the same row (entry). There may be cases when multiple rows have identical value so you would have to further specify the conditions by using AND or OR oeprators.
When a table does not contain more than 1 row, I should not have to apply WHERE Conditions. It will select the default one existing. But because the Countries Table had multiple items (3 Rows), leaving the WHERE clause out, caused to add 3 times the same User, with each row having a distinct CountryId!

SQL: Select attribute from row given primary key

New to SQL, I need to be able to retrieve an attribute from a row given a primary key. Take this table, for example:
Lets say I'm given the CustomerID 3. What would the SQL query be if I wanted to retrieve the Country associated with that ID?
(table is from https://www.w3schools.com/sql/sql_distinct.asp)
select country from my_table where customerID in(3);
Simply:
SELECT Country FROM YourTable WHERE CustomerID = 3;

Count only a specific subset of elements in a Postgres DB

I have a table with some identifiers that repeat themselves like
id
-------
djkfgh
kdfjhw
efkujh
dfsggs
djkfgh
djkfgh
efkujh
I also have a list of id's of interest, say ["djkfgh","dfsggs"]. I would like to count only those values that appear in the list, rather than all the distinct values of the column.
Select count(id) from table where id IN(subset);

select to check if a column does not contain a value in a provided array

I would like to select to check if any of the countries I have in my string array is not contained in the country table in the psql database.
Therefore I have a list of country names ARRAY['Country1','country2'.....]
and I have a table of country and I want a query to select countries that are not in this string that I will provide to the where clause
Something like this
SELECT name from country where name not in (ARRAY['Country1','country2'.....])
You were nearly there:
SELECT name
from country
where name <> ALL (ARRAY['Country1','country2'.....])

How to "filter" duplicate item in select statement

I have 2 tables : "Hotels" and "Area".
"Hotels" table has the following columns: INDEX, NAME, AREA
Sample data :
(1,'hotel bla", 4)
"Area" tbale has the following columns: INDEX, NAME, CODE
Sample data :
(4,'TEL-AVIV','TLV')
This means the "hotel bla" is in tel aviv
I need to create a list of hotels that have the same name and area ("duplicate hotels")
for examaple:
Hotels has 3 recoreds:
1,'hotel a',1
2,'hotel a',1
3,'hotel b',2
4,'hotel b',2
5,'hotel c',1
Area has 2 records
1,'tel-aviv','TLV'
2,'haifa','HAF'
The output should be something like:
'hotel a','1'
'hotel b,'2'
update:
If I have only 1 record of the hotel in the table I don't want to return it.
All your answer will also return it
see the fixed examaple
You need to use the distinct keyword to get only distinct records.
select distinct name, area from hotels
This would give you only unique rows if you only need name and area columns.
But if you need the ID column as well you can try
select id, max(name) name, max(area) as area from hotels group by name,area;
or
select id, first(name) name, first(area) as area from hotels group by name,area;
depending on what DBMS you're using
select name, area
from hotels
group by name, area
having count(1) > 1;
This will give you hotels having same name and belonging to same area.
Demo at sqlfiddle.