SQL Question (Select rows with multiple records)
Hello Gurus,
I am learning SQL and have a question. Thanks for your time and help.
I have 2 tables TABLE_AA and TABLE_BB. Both tables have two columns ID, DATA.
TABLE_AA is connected to TABLE_BB through ID field (TABLE_AA.AA_DATA = TABLE_BB.BB_DATA)
TABLE_AA
~~~~~~~
AA_ID______AA_DATA
~~~~~~~~~~~~~~~~
1111_______XXXX
2222_______QQQQ
3333_______ZZZZZ
4444_______PPPPP
~~~~~~~
TABLE_BB
~~~~~~~
BB_ID BB_DATA
~~~~~~~~~~~~~~~~
1111_______AAAA
2222_______BBBB
3333_______CCCC
3333_______DDDD
4444_______EEEE
I am looking to get those AA_ID values that have multiple in TABLE_BB for their parent reference in TABLE_BB.
So, from the above example, the sql should return the following as AA_ID 3333 has more than 1 reference value in BB_ID
AA_ID_____BB_ID
~~~~~~~~~~
3333______CCCC
3333______DDDD
Hi,
It's working fine. !!
14:10:05 topgun>With a As
14:10:06 2 (
14:10:06 3 Select 1111 c1, 'AAAA' c2 From dual Union All
14:10:06 4 Select 2222 , 'BBBB' From dual Union All
14:10:06 5 Select 3333 , 'CCCC' From dual Union All
14:10:06 6 Select 4444 , 'DDDD' From dual
14:10:06 7 ), b As
14:10:06 8 (
14:10:06 9 Select 1111 c1,'AAAA' c2 From dual Union All
14:10:06 10 Select 2222 , 'BBBB' From dual Union All
14:10:06 11 Select 3333 , 'CCCC' From dual Union All
14:10:06 12 Select 3333 , 'DDDD' From dual Union All
14:10:06 13 Select 4444 , 'EEEE' From dual
14:10:06 14 )
14:10:06 15 Select c1,
14:10:06 16 c2
14:10:06 17 From
14:10:06 18 (
14:10:06 19 Select a.c1,
14:10:06 20 b.c2,
14:10:06 21 Count(*) over (Partition By a.c1 Order By a.c1) cnt
14:10:06 22 From a,
14:10:06 23 b
14:10:06 24 Where a.c1 = b.c1
14:10:06 25 )
14:10:06 26 Where cnt = 2;
C1 C2
3333 CCCC
3333 DDDD- Pavan Kumar N
Similar Messages
-
How to get the last (max of string) row with multiple records for same ID
Hi all,
I have a query that returns for each client ID the telephone nr, mobile phone nr, email etc...
There is also a column FLG_DEFAULT that indicates if the phone is the default one or not (N= changed).
Basically what I would like my query to return is the phone number where FLG_DEFAULT = N, if this number is NULL or if FLG_DEFAULT =N does not exist I want to return the phone nr from DEFAULT_FLG = Y.
Anyone know how I can do this in SQL? I cannot get a maximum because the format is string (e.g. +32/xxx xx xx or xxx/xx xxx xx.
Thanks!;With cte As
(Select ClientID, PhoneNumber,
Row_Number() Over(Partition By ClientID,
Order By Case When FLG_DEFAULT = 'N' And PhoneNumber Is Not Null Then 1
When DEFAULT_FLG - 'Y' Then 2 End) As rn
From YourTableName)
Select ClientID, PhoneNumber
From cte
Where rn = 1;
Replace column names and table name with your column names and table name.
Tom -
Select max date from a table with multiple records
I need help writing an SQL to select max date from a table with multiple records.
Here's the scenario. There are multiple SA_IDs repeated with various EFFDT (dates). I want to retrieve the most recent effective date so that the SA_ID is unique. Looks simple, but I can't figure this out. Please help.
SA_ID CHAR_TYPE_CD EFFDT CHAR_VAL
0000651005 BASE 15-AUG-07 YES
0000651005 BASE 13-NOV-09 NO
0010973671 BASE 20-MAR-08 YES
0010973671 BASE 18-JUN-10 NOHi,
Welcome to the forum!
Whenever you have a question, post a little sample data in a form that people can use to re-create the problem and test their ideas.
For example:
CREATE TABLE table_x
( sa_id NUMBER (10)
, char_type VARCHAR2 (10)
, effdt DATE
, char_val VARCHAR2 (10)
INSERT INTO table_x (sa_id, char_type, effdt, char_val)
VALUES (0000651005, 'BASE', TO_DATE ('15-AUG-2007', 'DD-MON-YYYY'), 'YES');
INSERT INTO table_x (sa_id, char_type, effdt, char_val)
VALUES (0000651005, 'BASE', TO_DATE ('13-NOV-2009', 'DD-MON-YYYY'), 'NO');
INSERT INTO table_x (sa_id, char_type, effdt, char_val)
VALUES (0010973671, 'BASE', TO_DATE ('20-MAR-2008', 'DD-MON-YYYY'), 'YES');
INSERT INTO table_x (sa_id, char_type, effdt, char_val)
VALUES (0010973671, 'BASE', TO_DATE ('18-JUN-2010', 'DD-MON-YYYY'), 'NO');
COMMIT;Also, post the results that you want from that data. I'm not certain, but I think you want these results:
` SA_ID LAST_EFFD
651005 13-NOV-09
10973671 18-JUN-10That is, the latest effdt for each distinct sa_id.
Here's how to get those results:
SELECT sa_id
, MAX (effdt) AS last_effdt
FROM table_x
GROUP BY sa_id
; -
I need to divide selected row into multiple rows when i navigate ADF 11g
Hi
I'm using jdeveloper 11.1.1.2.0 with ADF 11g.
I need to divide selected row into multiple rows when i navigate to other page . Scenario - in first page i'm displaying some records with columns like empno , empstatus , empworkdepts ,curdepts
Here empworkdepts gives the numeric number like no of departments work shifts 3 or 4 or 5. when i select any particular employee and fire next button to navigate next page.I have to divide the selected employee with same information into multiple times based on the empworkdepts value.
empno empstatus empworkdepts curdept
001 eds 2 TS
002 hr 1 FO
003 eds 4 TS
*004 eds 3 TS*
now i selected employee 004 , when i navigate to next page.
Empno EmpStatus EmpWorkDepts CurDept
004 eds 3 TS
004 eds 3 TS
004 eds 3 TS
i did with java code in bean .but not stable .
any help............
thanks advance.............
Edited by: user9010551 on May 5, 2010 10:48 PM
Edited by: user9010551 on May 10, 2010 11:31 PMuser9086775 wrote:
Hi Experts,
I have a requirment where i need to fetch parts of a single row into multiple rows from a singlt Query, i am sure it is possible using Pivots but just cant figure out an approach. Any help on this is highly appriciapted.
Requirment:
This is a sample set record in a table
Product Sub Product Name Age
New Car Nissan Tom 49
New Car Nissan Jack 36
Old Car Audi Sam 24
Old Car Jaguar Pint 26
Old Car Audi Smith 41
I need to be able to fetch the above data in the below fashion
Product Sub Product Name Age
New Car
Nissan
Tom 49
Jack 36
Old Car
Audi
Sam 24
Smith 41
Jaguar Pint 26Please help with ideas as to how can i achive the above without using PLSQL.
Thanks in advance!You should be doing this in the client on not in the DB. Use the reporting tool that you use to do this.
For example if you are in SQL Plus you can use the BREAK command. -
Return to selected row with refresh after update in edit form
Hi,
I created a database view.When selected a single row and click on button i can edit the row in a form for update.What i want, when doing some update and click on the commit button i return back on the initial row with a refresh to see the update done on this row.I succeeded to modify the form and commit.But when i return on the selected row my update is not visible.How can i return to the same selected row with a refresh.In oracle form i can do a go record with execute_query, but how to do this in ADF.Please someone can help so that i do not spend many days on it.
Thanks
Soodesh[click for the tutorial|http://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=6&ved=0CFQQFjAF&url=http%3A%2F%2Fandrejusb.blogspot.com%2F2013%2F03%2Fadf-rollback-and-keep-current-row.html&ei=Tnl3Ucn3BoXIrQeemICQAw&usg=AFQjCNHdYcJL8kJKymqbWBT9XDGTWmeUvQ&bvm=bv.45580626,d.bmk&cad=rja]
here you go :) -
How to isolate error with a record/segment in IDOC with multiple records
I have an IDOC with multiple records/segments (typically 1000 records/segments). Sometime XI can not process the IDOC because of some control characters in data.
1. How can I pre-processed the IDOC to remove those control characters?
Can I use XPATH expression/Java class to do it? How can I configure the XPATH expression/Java class in XI to pre-process the file?
2. Until I have answer to 1st question. I would like to find out the error is exactly for which record? What configuration can I do in XI to isolate the error is with which record/segment in IDOC?
Thanks in advance.Split the IDoc.
with in the UDF, after the validations if every thing fine, pass as successful records to success_MT and pass it to target system using Branching in BPM.
if errors found in the record, then store the error records in Hash table with in UDF, get the IDoc number, frame as a string and raise alert.
U have to do this in the context of IDoc.
If U wanna get the IDoc Number, Segment Name and field name for every failure, U can pass the expected error field name as constant to UDF, frame the sentence in the UDF like -> <b>IDoc 1234321 segment SEG001 field FLD03 has a special character *</b>.
If U wanna pass this string to source/target, U can do in error messages branch in BPM.
U must use BPM for splitting the IDoc, since it is multi-mapping.
reg.,
Yallabandi. -
Creating Select List with multiple columns
I want to create a select list with multiple columns. I followed the demo application model described in the by Denes Kubicek (Currently my reference for APEX !!)
The code is as follows:
CREATE OR REPLACE FUNCTION getcrops(p_cropid IN NUMBER)
RETURN VARCHAR2
IS
v_cropid VARCHAR2 (400);
v_fcode VARCHAR2 (400);
v_product VARCHAR2 (400);
v_var VARCHAR2 (400);
v_fname VARCHAR2 (400);
v_acres VARCHAR2 (400);
v_style_start VARCHAR2 (400);
v_style_end VARCHAR2 (400);
v_return VARCHAR2 (400);
BEGIN
FOR c IN (select "CROP"."CROPID" as "CROP ID",
"CROP"."FIELDCODE" as "FIELD CODE",
"CARROTPRODUCTLIST"."CARROTPRODUCTNAME" as "PRODUCT",
"VARIETYLIST"."VARIETYNAME" as "VARIETY",
"FIELD"."FIELDNAME" as "FIELD NAME",
"CROP"."SIZEINACRES" as "ACRES"
from "FIELD" "FIELD",
"CARROTPRODUCTLIST" "CARROTPRODUCTLIST",
"VARIETYLIST" "VARIETYLIST",
"CROP" "CROP"
where "CROP"."CARROTPRODUCTTYPE"="CARROTPRODUCTLIST"."CARROTPRODUCTID"
and "CROP"."VARIETYID"="VARIETYLIST"."VARIETYLISTID"
and "CROP"."FIELDID"="FIELD"."FIELDID")
LOOP
v_cropid := TO_CHAR (c.'CROP ID', 'FML999G999G999G999G990');
v_fcode := c.'FIELD CODE';
v_product := c.'PRODUCT';
v_var := c.'VARIETY';
v_fname :=c.'FIELD NAME';
v_acres :=c.'ACRES';
FOR i IN 1 .. 12 - LENGTH (c."CROP ID")
LOOP
v_cropid := v_cropid || ' ';
END LOOP;
FOR i IN 1 .. 12 - LENGTH (c.'FIELD CODE')
LOOP
v_fcode := v_fcode || ' ';
END LOOP;
FOR i IN 1 .. 12 - LENGTH (c.'PRODUCT')
LOOP
v_product := v_product || ' ';
END LOOP;
FOR i IN 1 .. 12 - LENGTH (c.'VARIETY')
LOOP
v_var := v_var || ' ';
END LOOP;
FOR i IN 1 .. 12 - LENGTH (c.'FIELD NAME')
LOOP
v_fname := v_fname || ' ';
END LOOP;
FOR i IN 1 .. 12 - LENGTH (c.'ACRES')
LOOP
v_acres := v_acres || ' ';
END LOOP;
v_return := v_cropid || v_fcode || v_product || v_var || v_fname || v_acres;
END LOOP;
RETURN v_return;
END;
I created this anonymous Pl/SQL function at a application level ..Then I followed up to create a select list with a function inside. However I could not create select list with the command suggested in the demo which is
select getcrops(cropid) d, cropid r from crop;
APEX (2.1) returns a error message. What am I missing ? Should the function be called from somewhere else after creating a regular select list..? Where the functions (Pl/SQL) should be ideally stored for application level access..? and for across application level access ? How can I check the function works good. Help is appreciated.Still really one column.
If they need to be independent then you've got to have three selection lists.
% -
Flat File with multiple record types (OWB 10.2.0.2)
Hi!
I`m using OWB 10.2.0.2 and I`m trying to load a flat file with multiple record types, using SQL LOADER.
In the flat file editor in the Record tab, I`ve set the type values and the corresponding record names like this:
Type Value Record Name
======== ===========
T TRAILER
0 DETAILS
1 DETAILS
2 DETAILS
When using this flat file in a mapping to load the data in a staging table, the generated code looks like this:
INTO TABLE TRAILER
TRUNCATE
REENABLE DISABLED_CONSTRAINTS
WHEN (1:1) = 'T'
INTO TABLE DETAILS
APPEND
REENABLE DISABLED_CONSTRAINTS
WHEN (1:1) = '0,1,2'
The above clause (WHEN (1:1) = '0,1,2') is wrong as I expect one "INTO TABLE..." clause for each record type.
Could this be a bug or am I doing something wrong?
Thanks a lot for your help,
YorgosWe`re using two target tables, one for the trailer record and the other for the details records.
We are facing this problem from the moment we upgraded from OWB 10.1 to OWB 10.2.0.2, so we think it must be something with the way the sql loader code is generated in the new version.
As our data sources are mainly flat files coming from mainframes, this is a huge problem for us. We even asked an expert in DW from Oracle to help us on this, but still haven`t found a solution.
Is there any workaround for this or should we forget sql loader and go with an external tables + custom PL/SQL code solution?
Your help is greatly appreciated Jean-Pierre.
Regards,
Yorgos -
Handling Change of IT with multiple records in P&F
Good Morning all,
Has anyone done this yet? What is the approach to change an infotype with multiple records using P&F; For example IT0021, Subtype 2 (Child). I'm assuming that we will need to display all the records in a tbale first in the Form. Then, create a button or radio button for each row. On click, we can then make changes to the record. Any ideas? Thanks
RegardsWelcome to the forum. You can loop through the records like following.
go_block('A');
First_Record;
Loop
variable := :A.Item_Name;
Exit when :System.Last_Record = 'TRUE';
Next_Record;
End Loop;But what happens when u run the above code, the variable will have the last record's item value.
So how you are trying to get all the record's Item value into one item?
Are you going to concatenate? What's the logic you are planning?
Regards
Sankar MN -
How can I convert string to the record store with multiple records in it?
Hi Everyone,
How can I convert the string to the record store, with multiple records? I mean I have a string like as below:
"SecA: [Y,Y,Y,N][good,good,bad,bad] SecB: [Y,Y,N][good,good,cant say] SecC: [Y,N][true,false]"
now I want to create a record store with three records in it for each i.e. SecA, SecB, SecC. So that I can retrieve them easily using enumerateRecord.
Please guide me and give me some links or suggestions to achieve the above thing.
Best RegardsHi,
I'd not use multiple records for this case. Managing more records needs more and redundant effort.
Just use single record. Create ByteArrayOutputStream->DataOutputStream and put multiple strings via writeUTF() (plus any other data like number of records at the beginning...), then save the byte array to the record...
It's easier, it's faster (runtime), it's better manageable...
Rada -
File to RFC with multiple records using BPM Scenario Error...!!!
Hello Guru's,
I have done the File to RFC with multiple records using BPM scenario as per the Materiel available in the sdn.sap. This involves BAPI (BAPI_MATERIEL_AVAILABILITY). I have done exactly the same what is their in the materiel. SXI_CACHE is also giving return value " 0 ". File is getting deleted from the source directory, but no file in target directory. SXMB_MONI is also showing no error (black Flag). BPM is also error free. Checked the interfaces also.
Can any one tell me what mistake would i have done.
Thanks in advance.Hi,
There is one similar discussion I found,
FTP TO RFC using BPM
Thanks
Swarup -
Selection list with multiple columns
How can i create a selection list with multiple columns?
Still really one column.
If they need to be independent then you've got to have three selection lists.
% -
Select list with multiple columns
I want to create a select list with multiple column option. What are the options. Any developments in apex 3.0.1 ? Thanks in advance. I tried the tutorials by Kubicek.
I am kind of stuck to see the function entry inside LOV definition returns a error.Still really one column.
If they need to be independent then you've got to have three selection lists.
% -
Selecting rows with null values in an sql 3 table
Hi everyone I use oracle database 11g and have the following SQL statement:
SELECT Oprema.Opr_Id, Oprema.Datum_Nabavke, Oprema.Datum_Zaduzenja, Oprema.Dobavljac, Oprema.Jedinica_Mjere,
coalesce ((Oprema.Zaduzio), 0), Oprema.Vrijednost, Oprema.Kolicina_Nabavna, Oprema.Kolicina_Otpisana, Oprema.Kolicina_Trenutna, Oprema.Status, Oprema.Konto,
Oprema.KontoIsp, Oprema.Broj_Naloga, Oprema.Sifra_Objekta, Dobavljaci.Naziv AS DobNaz, coalesce(to_char(Uposlenici.Prezime), 'hm'), Objekti.Objekat_ID
FROM OPREMA Oprema, UPOSLENICI Uposlenici, DOBAVLJACI Dobavljaci, OBJEKTI Objekti
WHERE Oprema.Dobavljac=Dobavljaci.Dob_Id AND Oprema.Zaduzio=Uposlenici.UposlenikId AND Oprema.Sifra_Objekta=Objekti.Objekat_IDWhat I need is to show all the rows in the database. The problem is that some of the foreign keys Zaduzio and Objekti are NULL. This means the databse won't show those and only shows me rows where both have values. In all of these rows except for 3 only one of the two foreign keys has a value.
So what I need is an sql that would show me all 3. As you can see I tried with Coallesce but it didn't work, still only 3 rows show.
I also tried with UNION
SELECT Oprema.Opr_Id, Oprema.Datum_Nabavke, Oprema.Datum_Zaduzenja, Oprema.Dobavljac, Oprema.Jedinica_Mjere,
Oprema.Zaduzio, Oprema.Vrijednost, Oprema.Kolicina_Nabavna, Oprema.Kolicina_Otpisana, Oprema.Kolicina_Trenutna, Oprema.Status, Oprema.Konto,
Oprema.KontoIsp, Oprema.Broj_Naloga, Oprema.Sifra_Objekta, Dobavljaci.Naziv AS DobNaz, Uposlenici.Prezime, Objekti.Objekat_ID
FROM OPREMA Oprema, UPOSLENICI Uposlenici, DOBAVLJACI Dobavljaci, OBJEKTI Objekti
WHERE Oprema.Dobavljac=Dobavljaci.Dob_Id AND Oprema.Zaduzio=Uposlenici.UposlenikId AND Oprema.Sifra_Objekta=Objekti.Objekat_IDBut then the rows that have both Zaduzio and Objekti values are multiplied and selected twice.
So any ideas on how to do this?Hi,
If those foreign keys are NULL, then an inner join won't produce any results. Maybe you need to make it an outer-join, like this:
SELECT Oprema.Opr_Id
, Oprema.Datum_Nabavke
, Oprema.Datum_Zaduzenja
, Oprema.Dobavljac
, Oprema.Jedinica_Mjere
, COALESCE ( Oprema.Zaduzio
, 0
) AS zaduzio_or_0
, Oprema.Vrijednost
, Oprema.Kolicina_Nabavna
, Oprema.Kolicina_Otpisana
, Oprema.Kolicina_Trenutna
, Oprema.Status
, Oprema.Konto,
Oprema.KontoIsp
, Oprema.Broj_Naloga
, Oprema.Sifra_Objekta
, Dobavljaci.Naziv AS DobNaz
, COALESCE ( TO_CHAR (Uposlenici.Prezime)
, 'hm'
) AS prezime_or_hm
, Objekti.Objekat_ID
FROM OPREMA
JOIN DOBAVLJACI ON Oprema.Dobavljac = Dobavljaci.Dob_Id
LEFT OUTER JOIN UPOSLENICI ON Oprema.Zaduzio = Uposlenici.UposlenikId
LEFT OUTER JOIN OBJEKTI ON Oprema.Sifra_Objekta = Objekti.Objekat_ID
I hope this answers your question.
If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all the tables involved, and the results you want from that data.
Simplify the problem. For example, if you really need to display 18 columns, but 15 of them have nothing to do with the problem, then post a question that just includes e or 4 of those columns.
Explain, using specific examples, how you get those results from that data.
Always say what version of Oracle you're using (e.g. 11.2.0.2.0).
See the forum FAQ {message:id=9360002} -
Report Record Row with multiple color conditionaly
Dear Friend,
i am using Apex 3.2 ver.
i want to display report record row with colour .
i want to display reoport row color blue if INVOICE_TYPE column is 'I' and RED if INVOICE_TYPE is 'C' .
select
am.ID,
am.INVOICE_NO,
am.INVOICE_NO as invoice,
am.SBU_CODE,
am.INVOICE_TYPE,
am.INVOICE_DATE,
am.AGENT_CODE,
am.NO_OF_PAX,
am.ADDL_FIELD5
from "AR_INVOICE_MAS" amHow can i disply row record conditionaly color ful .
Thanks
Edited by: Vedant on Oct 10, 2012 11:05 PM
Edited by: Vedant on Oct 10, 2012 11:14 PMDear Jary,
Thanks to reply me.
i have follow your link and font color display but bg color not display.
now i have change in code
select
am.ID,
case when am.INVOICE_TYPE = 'C' then 'BLUE'
when am.USER_ENTER_AMT = sum(al.NET_AMT) then 'RED'
end the_color,
am.INVOICE_NO,
am.INVOICE_NO as invoice,
am.SBU_CODE,
am.INVOICE_TYPE,
am.INVOICE_DATE,
am.AGENT_CODE,
am.NO_OF_PAX,
am.ADDL_FIELD5,
NVL(sum(al.NET_AMT),0) AS SUM,
NVL(am.USER_ENTER_AMT,0.00) AS CONTROLSUM
from "AR_INVOICE_MAS" am ,AR_INVOICE_DTL al WHERE am.invoice_no=al.invoice_no(+)
group by am.ID,am.invoice_no,am.SBU_CODE,am.invoice_type,am.invoice_date,am.agent_code,
AM.NO_OF_PAX, am.ADDL_FIELD5,am.USER_ENTER_AMT Here i want to display in red below condition but it's not working.
when am.USER_ENTER_AMT = sum(al.NET_AMT) then 'RED'How can i display Record in RED which record does not varify below condition
when am.USER_ENTER_AMT = sum(al.NET_AMT) then 'RED'
How can i do this.
Thanks
Maybe you are looking for
-
As the title above, is there any solutions for that? I tried restarting my Ipad and i've double checked the auto lock/unlock button is on.
-
Need to hide tabs in CRM 5.0 SAP GUI for a business transaction
Hi all, I've created a custom complaint transaction in CRM 5.0, and I need to hide some tabs and fields when I create it or edit it via SAP GUI. I've searched through the help, and I figured out that probably I have to use transaction CRMV_SSC, bu
-
Adobe Acrobat Pro XI from Private party, Is it an authorized copy?
A private party offered to sell me a copy of Adobe Acrobat Pro XI. How do I tell if it is an authorized copy? How do I register it with Adobe?
-
Looking for a free app to convert audible audio format to mp3 or other so that I can listen to my audio book streamed to my xbox. Thank you.
-
Test question on NPS in Server 2012
Hello! "There's a domain member server with NPS and DHCP. You need to log all DHCP clients that have their firewalls disabled. Which three actions and in what sequence should you perform? a) Create a connection request policy b) Create a network poli