Query to find 1:N relation from a table
Hi,
I have a table where Material# & Item# are stored. Now Material# & Item# are related in 1:N.
e.g. (data)
MM# ITM# ..... <other attributes>
M1 I1 ..... <data for other attributes>
M1 I1 ..... <data for other attributes>
M1 I2 ..... <data for other attributes>
M2 I3 ..... <data for other attributes>
M3 I4 ..... <data for other attributes>
M3 I5 ..... <data for other attributes>
Now by somehow writing a single select query can I get the only one-2-many data. So that a person who is not aware of this relationship, will be easily able to identify.
e.g.
M1 I1
M1 I2
M3 I4
M3 I5
I am able to write the following query (please ignore the qualify keyword. I have used it because I have written this in teradata).
select subqry.itm_cd,subqry.mm_key
from
select mm_key,itm_cd,row_number() over (partition by mm_key,itm_cd order by mm_key) rn
from <my_table>
qualify rn = 1
) subqry
qualify row_number() over (partition by subqry.itm_cd order by subqry.itm_cd) > 1
order by subqry.itm_cd
This gives the output like :
M1 I2
M3 I4
But I want all the two occurrences of M1 and M3.
Regards,
Koushik Chandra
Very simple... group by mm#,itm#
SQL> create table test(mm# varchar2(3),itm# varchar2(2));
Table created.
SQL> insert into test values ('M1','I1');
1 row created.
SQL> insert into test values ('M1','I1');
1 row created.
SQL> insert into test values ('M1','I2');
1 row created.
SQL> insert into test values ('M2','I3');
1 row created.
SQL> insert into test values ('M3','I4');
1 row created.
SQL> insert into test values ('M3','I5');
1 row created.
SQL> select * from test group by mm#,itm#;
MM# IT
M2 I3
M3 I5
M3 I4
M1 I1
M1 I2
SQL> select * from test group by mm#,itm# order by 1;
MM# IT
M1 I1
M1 I2
M2 I3
M3 I4
M3 I5
SQL>HTH
Girish Sharma
Similar Messages
-
To find the lowest salary from a table
hello guys,
i need to find the lowest salary from a table. whats the easy way to do this.How do you handle ties?
If you want multiple rows returned when there are multiple people with the same salary
SELECT *
FROM(
SELECT a.*,
rank() over (order by salary desc ) rnk
FROM your_table a)
WHERE rnk = 1If you want to handle ties differently, use a different analytic function. Probably dense_rank or row_number.
Justin -
Find out the relation from vbrk and ekpo
hi
i want the relation field from vbrk and ekko or ekpo apart from kunnr and lifnr.
ple give me the fieldCheck ThisLink
Relation between the tables ekko and vbrk
Ranga -
Query to find indexes bigger in size than tables sizes
Team -
I am looking for a query to find the list of indexes in a schema or in a entire database which are bigger in size than the respective tables size .
Db version : Any
Thanks
Venkatresults are the same in my case
1 select di.owner, di.index_name, di.table_name
2 from dba_indexes di, dba_segments ds
3 where ds.blocks > (select dt.blocks
4 from dba_tables dt
5 where di.owner = dt.owner
6 and di.leaf_blocks > dt.blocks
7 and di.table_name = dt.table_name)
8* and ds.segment_name = di.index_name
SQL> /
OWNER INDEX_NAME TABLE_NAME
SYS I_CON1 CON$
SYS I_OBJAUTH1 OBJAUTH$
SYS I_OBJAUTH2 OBJAUTH$
SYS I_PROCEDUREINFO1 PROCEDUREINFO$
SYS I_DEPENDENCY1 DEPENDENCY$
SYS I_ACCESS1 ACCESS$
SYS I_OID1 OID$
SYS I_PROCEDUREC$ PROCEDUREC$
SYS I_PROCEDUREPLSQL$ PROCEDUREPLSQL$
SYS I_WARNING_SETTINGS WARNING_SETTINGS$
SYS I_WRI$_OPTSTAT_TAB_OBJ#_ST WRI$_OPTSTAT_TAB_HISTORY
SYS I_WRI$_OPTSTAT_H_OBJ#_ICOL#_ST WRI$_OPTSTAT_HISTGRM_HISTORY
SYS WRH$_PGASTAT_PK WRH$_PGASTAT
SYSMAN MGMT_STRING_METRIC_HISTORY_PK MGMT_STRING_METRIC_HISTORY
DBADMIN TSTNDX TSTTBL
15 rows selected -
To find a field value from a table
i want to get some field value from a data dictionary table.
for eg Tragr from Mara. in Fields list it shows the field is in Mara table. but in content screen it does not showing the field at all. Actually of 220 fields of mara table only 99 fields are shown. how to view all the fields of a particular field. is there any setting is there.Hi Deva,
When we execute any table by default SAP will display only few columns.(90 i guess) If we want to view all the columns of the table then do like this after executing the table i.e in ur table output.
Menupath->Settings->User parameters->choose radiobotton
Grid display Here u can see all the fields.
If u don't want to see all the fields then do like this.
Settings->listformat->Choosefields->Deselectall->Select what
ever fields u want using the search button in bottom and press enter. It will display only selected fields.
Some times we will come up with strange situations.
EG: In the field list some field will be there. But if u select that field from that table in select query it will give u syntax error saying field xxx does't exist in table yyy. This field is nothing but text table field. If u want to select this field then u have to select it from the text table. U can find the text table exist for some table or not in
Menupath->goto->text table. (In this case makt is text table for mara)
Just check below code for ur reference.
DATA: wa_mara TYPE mara.
SELECT SINGLE * INTO wa_mara FROM MARA .
Here if u go to mara table u have field MAKTX But above select will not return the field maktx even we are refering wa_mara to data base table mara. We have to explicitly select it from MAKT.
Hope this clarified all ur doubts.
Thanks,
Vinod. -
Report query not returning the field value from external table
hi
I have an issue regarding reports. I have a query having 4 fields from external table and remaining from db tables. the report query returns all the fields from the db tables and only 2 fields from external table. but the same query if I tried in plsql developer it returns all the fields values.
Can anyone please help me in this issue.
Thanks and Regards
kkDuplicate post?
value not displaying in report whereas it returns in plsql developer
value not displaying in report whereas it returns in plsql developer
Please log a SR if you do not get any reply to your thread instead of creating new one.
Thanks,
Hussein -
Finding a column name from all tables
i have 85 tables in my user, i forget a column name and its table name. how to find that particular column amongst all tables i have.
hi
You can use User_tab_columns or All_tab_columns
SQL>Select Table_Name,Column_Name From user_tab_columns
where lower(table_name) like '%emp%' or
lower(column_name) like '%dept%'
Khurram Siddiqui
[email protected]
Message was edited by:
pcbyte12 -
Finding the column names from a table.
I am on 10g
I would like to find out the columns of a table where there are null columns in a table.....this table contains about 300 + columns where i do not want to put where condition for all the columns
is there a way i can write a sql to find?
for a given table or the results set that i need to get, i will have same results for all the rows, so it
cant be like col1 is null on row1, but col2 is not null on row2 ...they are all identical....
example table, but it has 300 + cols
F_IND H_IND P_IND DMA_IND
N N
N N
N N
N N Thanksselect count(col1), count(col2), count(col3), ...
from your table;
The results with 0s are null throughout the table (or the table has no rows). -
Querying users and their resource names from SSO tables
Hi,
Any ideas how to query the SSO tables so that i can extract all users and which "Resource Access Information" (for Forms based applications) is assigned to each user.?
A bit like the solution on the link below but also with a join to resource names (for Forms) that each user has.
Querying user enabled status from SSO tables
ThanksThank you for that! I have extended this query to contain the ods.ct_uid table as follows as some users were being omitted. Also I have included a decode statement that changes the 'enabled' or 'disabled' to a 0 or 1.
SELECT
UPPER(cus.attrvalue) username,
DECODE(NVL(ena.attrvalue, 'enabled'), 'enabled', 1, 'disabled', 0) enabled
FROM
ods.ct_orclisenabled ena,
ods.ct_uid cus,
ods.ct_cn ctu
WHERE cus.entryid = ctu.entryid
AND ena.entryid (+) = ctu.entryid -
Query to find oldest man data from table
Emp tab:
f_name varchar2(30)
l_name varchar2(30)
dob date
sex varchar2(5)
I want to find the F_name of the oldest man born in year 1965 , who possess the "DA" in the f_name.How about this?
SELECT f_name
FROM (SELECT *
FROM emp
WHERE TRUNC(dob) = 1965
AND UPPER(f_name) LIKE '%DA%'
ORDER BY dob desc
WHERE ROWNUM = 1
not tested and assuming that there are no duplicate dates. Another option would be analytic functions.
C. -
Finding the missing number from the table
I have vendor table with the following column, refvendor_id, the refvendor_id is a primary key in the table. the customer use the application to enter information to the vendor table using an interface
created in Java. my question is how can I query the refvendor_id numbers that have been skipped by the customer when they enter information to the vendor table.
refvendor_id is a varchar data type.
for example
refvenodr_id
1
2
3
5
6
9
I would like to capture in my sql the refvendor_id that is skipped here.
4,7,8
select refvendor_id
from vendorI suggest to find the number values with a regular expression.
Here is a modified version, that will only use the pure number refvendor_ids
-- Test-Data:
with vendor as
select '1' refvendor_id from dual union all
select '2' refvendor_id from dual union all
select '3' refvendor_id from dual union all
select '5' refvendor_id from dual union all
select '6' refvendor_id from dual union all
select '9' refvendor_id from dual union all
select '10' refvendor_id from dual union all
select '12' refvendor_id from dual union all
select 'A' refvendor_id from dual
-- Query:
select rownum from dual connect by rownum <= (select max(to_number(refvendor_id)) from vendor where regexp_like(refvendor_id,'^[0-9]+$') )
minus
select to_number(refvendor_id) from vendor where regexp_like(refvendor_id,'^[0-9]+$')
order by 1;(I produce output in number-format, because i want numeric ordering in output)
Edited by: hm on 14.12.2010 13:32 :
Of cause you can also use your restrictions, but I can't see if that are really only numbers:
select rownum from dual connect by rownum <= (select max(to_number(refvendor_id)) from vendor where SUBSTR (refvendor_id, 1, 1) IN ('1', '2', '3', '4', '5', '6', '7', '8', '9') AND LENGTH (refvendor_id) < 5 )
minus
select to_number(refvendor_id) from vendor where SUBSTR (refvendor_id, 1, 1) IN ('1', '2', '3', '4', '5', '6', '7', '8', '9') AND LENGTH (refvendor_id) < 5 )
order by 1; -
Oracle SQL query for getting specific special characters from a table
Hi all,
This is my table
Table Name- Table1
S.no Name
1 aaaaaaaa
2 a1234sgjghb
3 a@3$%jkhkjn
4 abcd-dfghjik
5 bbvxzckvbzxcv&^%#
6 ashgweqfg/gfjwgefj////
7 sdsaf$([]:'
8 <-fdsjgbdfsg
9 dfgfdgfd"uodf
10 aaaa bbbbz#$
11 cccc dddd-/mnm
The output has to be
S.no Name
3 a@3$%jkhkjn
5 bbvxzckvbzxcv&^%#
7 sdsaf$([]:'
8 <-fdsjgbdfsg
10 aaaa bbbbz#$
It has to return "Name" column which is having special characters,whereas some special chars like -, / ," and space are acceptable.
The Oracle query has to print columns having special characters excluding -,/," and space
Can anyone help me to get a SQL query for the above.
Thanks in advance.You can achieve it in multiple ways. Here are few.
SQL> with t
2 as
3 (
4 select 1 id, 'aaaaaaaa' name from dual union all
5 select 2 id, 'a1234sgjghb' name from dual union all
6 select 3 id, 'a@3$%jkhkjn' name from dual union all
7 select 4 id, 'abcd-dfghjik' name from dual union all
8 select 5 id, 'bbvxzckvbzxcv&^%#' name from dual union all
9 select 6 id, 'ashgweqfg/gfjwgefj////' name from dual union all
10 select 7 id, 'sdsaf$([]:''' name from dual union all
11 select 8 id, '<-fdsjgbdfsg' name from dual union all
12 select 9 id, 'dfgfdgfd"uodf' name from dual union all
13 select 10 id, 'aaaa bbbbz#$' name from dual union all
14 select 11 id, 'cccc dddd-/mnm' name from dual
15 )
16 select *
17 from t
18 where regexp_like(translate(name,'a-/" ','a'), '[^[:alnum:]]');
ID NAME
3 a@3$%jkhkjn
5 bbvxzckvbzxcv&^%#
7 sdsaf$([]:'
8 <-fdsjgbdfsg
10 aaaa bbbbz#$
SQL> with t
2 as
3 (
4 select 1 id, 'aaaaaaaa' name from dual union all
5 select 2 id, 'a1234sgjghb' name from dual union all
6 select 3 id, 'a@3$%jkhkjn' name from dual union all
7 select 4 id, 'abcd-dfghjik' name from dual union all
8 select 5 id, 'bbvxzckvbzxcv&^%#' name from dual union all
9 select 6 id, 'ashgweqfg/gfjwgefj////' name from dual union all
10 select 7 id, 'sdsaf$([]:''' name from dual union all
11 select 8 id, '<-fdsjgbdfsg' name from dual union all
12 select 9 id, 'dfgfdgfd"uodf' name from dual union all
13 select 10 id, 'aaaa bbbbz#$' name from dual union all
14 select 11 id, 'cccc dddd-/mnm' name from dual
15 )
16 select *
17 from t
18 where translate
19 (
20 lower(translate(name,'a-/" ','a'))
21 , '.0123456789abcdefghijklmnopqrstuvwxyz'
22 , '.'
23 ) is not null;
ID NAME
3 a@3$%jkhkjn
5 bbvxzckvbzxcv&^%#
7 sdsaf$([]:'
8 <-fdsjgbdfsg
10 aaaa bbbbz#$
SQL> -
Handling sql query in jsp while extracing records from 3 tables
hi to one and all,
i want to implement the sql query given below in jsp in execute query statement . the query is working in sql but not when implemented using jsp. please help me in resolving this.
Query
SELECT e.Department , e.ETitle , s.basic , s.da, c.address1 , c.address2 from Employee e , Salary s , Contactdetails1 c where e.id = s.id and e.id =c.idIt would help if you gave us a little more info to go on like;
a) post your code
b) post the error message the compiler is giving you -
SQL Query to find out similar names in two tables
Hi,
I want to write a query which will return the records by matching the two table with the similar name.
that is I have two tables table1 and table2, and in table1 the column1 is having data like
ABC INC
The international Company
and in the table2 has the column1 which has the data like
ABC Corp
The Financials Corporation
So as per the requirement now my select query should return 1 row as ABC INC and ABC Corp are similer.
and both the tables have around 50,000 reords so I cannot just take the first word by using substring and tryto match both, this will work only for names like ABC INC and ABC Corp but it will not return the names like
"Instruments International Company" and "International Instruments Company"
Please can any one help me?
Regards,
ChandaI don't have access to text here so I can't check.Almost. Better to use OR than AND:
SQL> CREATE TABLE t (ID INTEGER, text VARCHAR2(2000))
2 /
Table created.
SQL> INSERT INTO t
2 VALUES (1, 'The international Company')
3 /
1 row created.
SQL> INSERT INTO t
2 VALUES (2, 'ABC INC')
3 /
1 row created.
SQL> CREATE INDEX t_txt_idx ON t
2 (text)
3 INDEXTYPE IS ctxsys.CONTEXT
4 /
Index created.
SQL> SELECT *
2 FROM t
3 WHERE contains (text,
4 REPLACE ('International Instruments Company', ' ', ' & ')
5 ) > 0
6 /
no rows selected
SQL> SELECT *
2 FROM t
3 WHERE contains (text,
4 REPLACE ('International Instruments Company', ' ', ' | ')
5 ) > 0
6 /
ID TEXT
1 The international Company
BUT than also :(
SQL> SELECT *
2 FROM t
3 WHERE contains (text,
4 REPLACE ('Company Instruments', ' ', ' | ')
5 ) > 0
6 /
ID TEXT
1 The international Company -
How to find diffrence in columns from two tables
Hello,
I have table TABLE1 and TABLE2 with same columns. One of the columns in both tables is ID
Now there are records in table TABLE1 with ID=1
and in table TABLE 2 with ID=-1
Now I want to find if there are any column differing in values in tables TABLE1 for ID=1(for TABLE1) and TABLE2 for ID = -1 (for TABLE2)
What is the best way to find the diffrence?
Thanks in advance.Hello Subhash,
Why don't you try NOT EXISTS functionality? Its quite performance effective too.
select * from table1
where NOT EXISTS
(select 1 from table 2
where table1.ID = table2.ID)
UNION
select * from table2
where NOT EXISTS
(select 1 from table 2
where table1.ID = table2.ID)You can go for UNION if required, or else you can use them separately.
Maybe you are looking for
-
I hate Mountain Lion! How do I get rid of menus?
I was hoping I'd get the Mac Book Pro with Lion, and I could wait for Mt Lion til it had a few upgrades of debugging. I have a mail program problem that I have up here already. BUT how do you unenable these little menus/options that pop up all the t
-
Error while deploy application in OC4J version 10.1.3.2
I have the OC4J version 10.1.3.2, I trying deploy the java application, but I have some problem. Somebody help-me? please, help into [email protected]. Thank you! The messange from error in console: [java] 06/07/2007 16:32:35 oracle.j2ee.rmi.
-
Sigh, this past week I tried to turn on my early '09 Mini and it wouldn't do anything. I tried resetting the PRAM, replacing the PRAM battery, and finally gave up. Took it to an Apple certified local shop (not sure if I can mention the name here) and
-
Business Object (BAPI/IDOC) for production Order
Hi All I have requirement where data is coming from legacy system to R/3 via XI to generate a production order. I am not able to get right BAPI/ IDOC for inbound Production Order. Can anyone has done similar scenario. Can u suggest me BAPI or IDOC fo
-
Problem Cloning a Managed Server
Hello, I am trying to add a managed server to an OSB CLUSTER with Weblogic Version 11.1.1.3. What I am doing is cloning an existing NODE but with a diferent Listen Port. And when I try to start it, I obtain the following error: bash-3.2$ tail -f osb_