I'm building a page in APEX. In that page I have 2 items, "text field with autocomplete" Both of them are getting the LOV from the same table but different column. text_field_1 getting the id_1; text_field_2 getting the id_2
So If I input/select a value from text_field_1 I can set a value automatically to text_field_2 or vice versa. I can do one is to one relationship, like if I enter "bca" in text_field_2, I can set the correct value for text_field_1 which is "2"
But I'm having a problem for the one is to many relationship. If I enter "1" in text_field_1, It will return 2 values which are "abc,cba" I'm trying to figure it out on how I list the abc and cba so that I can still choose what value I want from the two. Please someone help me with this?
This is my sample table
id_1
id_type_1
id_2
id_type_2
datetime
1
employee_id
abc
employee_code
01-01-1000 00:00:00
1
employee_id
cba
employee_code
02-01-1000 11:00:00
2
employee_id
bca
employee_code
01-01-1000 12:00:00
3
employee_id
acd
employee_code
01-02-1000 14:00:00
Thank you so much in advance. I really need this
If you use list of values, then that's exactly what its name says - a list of values. They are supposed to return more than a single row, that's perfectly normal and expected situation.
Therefore, your LOV1 (which is a "source" of e.g. P1_ITEM_1) should
select distinct id_1 as display_value,
id_1 as return_value
from your_table
while LOV2 would then have P1_ITEM_1 as a parent LOV item and
select id_2 as display_value,
id_2 as return_value
from your_table
where id_1 = :P1_ITEM_1
To answer what you asked:
if I select id_1 = 1 then I will get the 'abc' OR if I select the other id_1 = 1, I will get 'cba'
Can you do that? Sure; the question is which one of abc or cba you want. What we usually do is to select any of them using min or max aggregate function, e.g.
select max(id_2)
from your_table
where id_1 = :P1_ITEM_1
(you'd get cba as a result in this case).
Related
I have a table like this:
ID
CODE
VALUE
1
A
123
1
A
456
1
C
123
1
B
789
2
A
344
2
B
344
I would like to see all cases that for the same ID we have different values for different codes.
Result:
ID 1, CODE B VALUE 789 NOT FOUND
ID 1, CODE A VALUE 456 NOT FOUND
In summary I need to see all diferences the I didn't find on column VALUE based in the ID key BUT NOT FOR THE SAME "CODE" COLUMN.
Anyone know how to achive that?
Thanks a lot!
Provided no duplicate rows exist, i.e. (ID, CODE, VALUE) is unique you can find VALUEs associated with exactly one CODE each with
select ID, max(CODE) CODE, VALUE
from tbl
group by ID, VALUE
having count(*) = 1
I have a table below (that also has several other columns, but for the purpose of this example, I'll exclude them) where I only want to include the very first instance for each person (unique_id) by date, which is in DATETIME format.
In the past I've used something like:
SELECT
*,
least(min(date_event), min(date_event)) as min_date
FROM
table
GROUP BY
unique_id ,issue, date_event, age_at_event
However, this is still returning multiple records for each person, rather than just the very first instance?
unique_id
issue
date_event
age_at_event
1234
issue_a
2016-04-01T00:00:00
6
1234
issue_a
2016-04-01T00:00:00
6
1234
issue_b
2018-04-01T00:00:00
8
5678
issue_a
2019-09-01T00:00:00
2
5678
issue_a
2021-09-01T00:00:00
4
65431
issue_c
2019-09-01T00:00:00
1
1234
issue_a
2022-09-01T00:00:00
12
You can use the qualify function to implement what you're looking for.
With the sample data you provided the following query:
select *
from sample_data
qualify row_number() over (partition by unique_id order by date_event) = 1
produces this:
I am trying something like:
select name from myTable where id in (1056,1066,1069,1080,1404,1406,1407,3018)
But this is acting like OR condition and if I use AND between these values it does not give any result as it is trying to match in one cell.
Sample Data:
Name ID : abc 1 |abc 2| abc 3| def 2| fgh 3
My expected output is:
Name: abc
I want to find all the distinct names
having all corresponding ID's
Any pointer on this?
It seems you are confused on Logic, if you use AND then condition becomes false because there will not be any single row with ID having 1056 AND 1066.
for eg. suppose you have this table
ID name level
----------------
1056 abc bce
1066 bcd def
select name from myTable where id =1056 AND id=1066
now evaluate this with first row replacing id
1056 =1056 AND 1056=1066 (True AND False) is False
1066 =1056 AND 1066=1066 (False AND True) is False
So condition never becomes true so no rows are selected
Update
As per as your updated question, write like this
select distinct name from myTable where id in (1056,1066,1069,1080,1404,1406,1407,3018)
Ew. Multiple values in the same field?
select name from myTable
where id like '%1056%'
or id like '%1066%'
or id like '%1069%'
etc
Logically there is no output of your query, but if you want to use multiple ANDs once without writing writing AND multiple times, you can use Row Subqueries:
select name
from myTable
where (id,id,id,id,id,id,id,id)=(1056,1066,1069,1080,1404,1406,1407,3018)
is equal to:
select name
from myTable
where id =1056
AND id =1066
AND id =1069
AND id =1080
AND id =1404
AND id =1406
AND id =1407
AND id =3018
For the problem of this type, the best solution I could is use divide feature of Relational Algebra.
It will give the expected result.
Sample Example:-
http://coronet.iicm.tugraz.at/Dbase1/scripts/rdbh06.htm
I am working on an Oracle 11g database query that needs to retrieve a list of the highest NUM value between duplicated rows in a table.
Here is an example of my context:
ID | NUM
------------
1 | 1111
1 | 2222
2 | 3333
2 | 4444
3 | 5555
3 | 6666
And here is the result I am expecting after the query is executed:
NUM
----
2222
4444
6666
I know how to get the GREATEST value in a list of numbers, but I have absolutely no guess on how to group two lines, fetch the biggest column value between them IF they have the same ID.
Programmaticaly it is something quite easy to achieve, but using SQL it tends to be a litle bit less intuitive for me. Any suggestion or advise is welcomed as I don't even know which function could help me doing this in Oracle.
Thank you !
This is the typical use case for a GROUP BY. Assuming your Num field can be compared:
SELECT ID, MAX(NUM) as Max
FROM myTable
GROUP BY ID
If you don't want to select the ID (as in the output you provided), you can run
SELECT Max
FROM (
SELECT ID, MAX(NUM) as Max
FROM myTable
GROUP BY ID
) results
And here is the SQL fiddle
Edit : if NUM is, as you mentioned later, VARCHAR2, then you have to cast it to an Int. See this question.
The most efficient way I would suggest is
SELECT ids,
value
FROM (SELECT ids,
value,
max(value)
over (
PARTITION BY ids) max_value
FROM test)
WHERE value = max_value;
This requires that the query maintain a single value per id of the maximum value encountered so far. If a new maximum is found then the existing value is modified, otherwise the new value is discarded. The total number of elements that have to be held in memory is related to the number of ids, not the number of rows scanned.
See this SQLFIDDLE
How to select the column value based on three criteria. I want each criteria display in each column. Is it possible?
Database is Ms-Access
My Table is:
ID Name Key Keyer_Type
1 xxx 565 Beginner
8 yyy 456 Beginner
1 xxx 4589 Skill
9 zzz 89 Beginner
2 uuu 8789 Expert
8 yyy 5689 Skill
Using distinct and groupby clause I select the unique vaue in ID no and corresponding name.
I want to select the key based on beginner, skill, expert on separeate column.
My need my output as:
ID Name Beginner Skill Expert#
1 xxx 565 4589
8 yyy 456 5689
9 zzz 89
2 uuu 8789
How to display the Beginner, skill, expert as a separate column from Keying level column.
Not sure it is exactly what you need, but I think this is close to the answer you need:
TRANSFORM First(TableName.Key)
SELECT TableName.ID, TableName.Name
FROM TableName
GROUP BY TableName.ID, TableName.Name
PIVOT TableName.Keyer_Type;
Edit as per the comment:
This will return a grouping on the ID, and the first corresponding name.
TRANSFORM First(Table2.Key)
SELECT Table2.ID,First( Table2.Name)
FROM Table2
GROUP BY Table2.ID
PIVOT Table2.Keyer_Type;
But it is not clear to me what the meaning of Name is.