Select unique rows from two tables...
Hi,
I have two tables, replies1 and replies2.
SQL> desc replies
Name Null? Type
URN VARCHAR2(36)
ADDRESS VARCHAR2(18)
FILESIZE NUMBER
AS_NUM VARCHAR2(6)
SQL> desc replies2
Name Null? Type
URN VARCHAR2(36)
ADDRESS VARCHAR2(18)
AS_NUM VARCHAR2(6)
Both of the tables have no primary keys, but I have indixes on (urn, addrss) combination on both....
I am trying to select the unique rows with (urn, address) from replies2, and then find the matching size from replies...
I am using the following query:
select distinct replies2.urn, replies2.address, replies.filesize from replies2, replies where replies2.AS_NUM like 'XYZ' and replies.urn = replies2.urn;
I cannot figure out why it won't work. the way I understand it is that, distinct will give all distinct combination of all column names that follow, which is what I want...
I know it is wrong, because the query:
select count(*) from replies2 where AS_NUM like 'XYZ' returns less number of rows than the above query.
Any help would be greatly appreciated.
Thank you
Oz.
Thanks a lot Mohan for your reply.
urn is not a unique key. Several rows could have the same (urn, address) pair in both tables. What I want is retrieve all (urn, address) rows from one table, and find the size from the other table to make a (urn, address, size). I want all unique combinations of (urn, address) to appear in the output.
AS_NUM is an empty column in replies... It would've been a lot easier if it wasn't, since then I'll just say: select distinct urn, address, filesize from replies where AS_NUM like 'XYZ';
I will try your query though and let u know how it goes. It takes quite a while to run since my tables are huge.
Similar Messages
-
Need of SQL query in selecting distinct values from two tables
hi,
I need a query for selecting distinct values from two tables with one condition.
for eg:
there are two tables a & b.
in table a there are values like age,sex,name,empno and in table b valuses are such as age,salary,DOJ,empno.
here what i need is with the help of empno as unique field,i need to select distinct values from two tables (ie) except age.
can anybody please help me.
Thanks in advance,
RatheeshNot sure what you mean either, but perhaps this will start a dialog:
SELECT DISTINCT a.empno,
a.name,
a.sex,
b.salary,
b.doj
FROM a,
b
WHERE a.empno = b.empno;Greg -
How to compare two rows from two table with different data
how to compare two rows from two table with different data
e.g.
Table 1
ID DESC
1 aaa
2 bbb
3 ccc
Table 2
ID DESC
1 aaa
2 xxx
3 ccc
Result
2Create
table tab1(ID
int ,DE char(10))
Create
table tab2(ID
int ,DE char(10))
Insert
into tab1 Values
(1,'aaa')
Insert
into tab1 Values
(2,'bbb')
Insert
into tab1 Values(3,'ccc')
Insert
into tab1 Values(4,'dfe')
Insert
into tab2 Values
(1,'aaa')
Insert
into tab2 Values
(2,'xx')
Insert
into tab2 Values(3,'ccc')
Insert
into tab2 Values(6,'wdr')
SELECT
tab1.ID,tab2.ID
As T2 from tab1
FULL
join tab2 on tab1.ID
= tab2.ID
WHERE
BINARY_CHECKSUM(tab1.ID,tab1.DE)
<> BINARY_CHECKSUM(tab2.ID,tab2.DE)
OR tab1.ID
IS NULL
OR
tab2.ID IS
NULL
ID column considered as a primary Key
Apart from different record,Above query populate missing record in both tables.
Result Set
ID ID
2 2
4 NULL
NULL 6
ganeshk -
To Select the data from two table one is transp table and onther is cluster
Hi All,
I want to select the data from two tables
Here i am giving with an example.
Fileds: kunnr belnr from bseg. table bseg
fields: adrnr from kna1 table: kna1.
Know i want to put these into one internal table based on kunnr and belnr.
Thanks in advance.
RameshHi,
U cant use joins on cluster table and BSEG is a cluster table so use FOR ALL ENTRIES for taht
refer this code
*& Form sub_read_bsak
text
--> p1 text
<-- p2 text
FORM sub_read_bsak.
*--Select data from BSAK Table
SELECT lifnr
augdt
augbl
gjahr
belnr
xblnr
blart
dmbtr
mwskz
mwsts
sgtxt
FROM bsak
INTO CORRESPONDING FIELDS OF TABLE it_bsak
WHERE belnr IN s_belnr
AND augdt IN s_augdt.
IF sy-subrc EQ 0.
*--Sort table by accounting document and vendor number
SORT it_bsak BY belnr lifnr.
ENDIF.
ENDFORM. " sub_read_bsak
*& Form sub_read_bseg
text
--> p1 text
<-- p2 text
FORM sub_read_bseg.
IF NOT it_bsak[] IS INITIAL.
*--Select data from BSEG table
SELECT belnr
gjahr
shkzg
kostl
hkont
ebeln
ebelp
FROM bseg
INTO CORRESPONDING FIELDS OF TABLE it_bseg
FOR ALL ENTRIES IN it_bsak
WHERE belnr EQ it_bsak-belnr
AND gjahr EQ it_bsak-gjahr
AND shkzg EQ 'S'.
IF sy-subrc EQ 0.
*--Sort table by accounting document
SORT it_bseg BY belnr.
ENDIF.
ENDIF.
ENDFORM. " sub_read_bseg -
Select a row from a table and throw an url
Hi experts,
I'm trying to select a row from a table in Visual Composer. What I need to do it's to click over this row and throw an Url. At the moment, I've added a column with a Pushbutton with the formula to show the link; it works fine. But I need to do it without buttons just selecting the row.
Is there anyboby who knows about that?
Thanks a lot.
BelenHi
To do this you will have to use a data store and a guard condition on the line which is triggered by the select action. You need to store a value in the data store each time a select action is performed. The guard condition should check for this and only perform the action when the value in the data store is greater than 0.
Jarrod Williams -
Select multiple rows from dual table
Is it possible to select multiple rows from dual table using a single select statement.
i.e., i want the out put to be
column_name
1
2
3
4
Edited by: vidya.ramachandra on Dec 14, 2009 8:24 AMAside from the fact you're responding to an old thread...
1002424 wrote:
While using CONNECT BY, I see it always leave behind one row.
Suppose I have a condition based on which I have to generate constant rows like
SELECT 1 FROM DUAL WHERE ROWNUM < N;
Here if N = 0, still it gives out single row.... you are obviously doing something wrong in your code elsewhere, because that SQL statement does not always return a single row...
SQL> SELECT 1 FROM DUAL WHERE ROWNUM < 0;
no rows selected
SQL> -
Easy Question:Select many rows from a table and execute BAPI for these rows
Hi Experts,
I have created one WD project. The WD project fetches some records of backend using BAPI and displays in a table. I have to select some rows from the table and then execute BAPI for selected rows.
How I can select multiple records from the table and then execute another BAPI for selected rows.
Regards,
GaryHi,
In the Node which you binded to the table create one more attribute of type boolean.
For example your node is as below:
//Table Node
TableNode
> Att1
> Att2
> isSelected(boolean) - Newly created attribute for this requirement.
//Result Node contains the elements selected in TableNode
ResultNode
>Att1
>Att2
Now in the table create one more Column with Checkbox as tablecell editor. Now bind this boolean attribute to that check box.
Now in the code you can get the selected rows by user as below:
for(int i=0;i<TableNode().size();i++)
if(wdContext.nodeTableNode().getTableNodeElementAt(i).getIsSelected()==true)
IPrivateTestView.IResultNode element=wdContext.createResultNodeElement();
element.setAtt1(wdContext.nodeTableNode().getTableNodeElementAt(i).getAtt1());
element.setAtt2(wdContext.nodeTableNode().getTableNodeElementAt(i).getAtt2());
wdContext.nodeResultNode().addElement(element);
Regards,
Charan -
How NOT to restrict no of rows from two tables
I have two identical tables Invoice and Payment. The only difference is Invoice_id,Invoice_Amt and Payment_id,Payment_Amt columns that shows different ids and amounts. The bank_ids, names, account_types are same. Invoice table has 3 rows and Payment has 2. Simply meaning that there were 3 invoices generated but the bank received 2 payments. I want to show Invoice_Amt and Payment_Amt using sql query. But its giving me total 6 rows. Whereas, I want 3 from Invoice and 2 rows from Payment table to show side-by-side.
CREATE TABLE invoice
( invoice_id NUMBER
bank_id NUMBER,
bank_name VARCHAR2(256),
invoice_amount NUMBER);
----Invoice table has 3 rows showing 3 Invoice Amts
CREATE TABLE payment
( payment_id NUMBER
bank_id NUMBER,
bank_name VARCHAR2(256),
payment_amount NUMBER);
----Payment table has 2 rows showing 2 Payments
After executing this sql statement below, I get 6 rows:
select inv.invoice_amount,pymt.payment_amount from invoice inv,payment pymt where inv.bank_id=pymt.bank_id;
How can I show 3 rows for Invoice and 2 for Payment..?
Thank you.Hi,
So you want
the 1st invoice to appear side-by-side with the 1st payment,
the 2nd invoice to appear side-by-side with the 2nd payment,
the nth invoice to appear side-by-side with the nth payment.
But, if there are an uneqaul numner of payments and invoices, all the payments and all the invoices must still be shown, alone on a row if necessary.
That sounds like a job for FULL OUTER JOIN.
Use the analytic ROW_NUMBER fucntion to determine which is the 1st, 2nd, ..., nth row in each table, for each bank.
WITH invoice_plus_r_num AS
SELECT bank_id, bank_name
, invoice_amount
, ROW_NUMBER () OVER ( PARTITION BY bank_id, bank_name
ORDER BY invoice_id
) AS r_num
FROM invoice
, payment_plus_r_num AS
SELECT bank_id, bank_name
, payment_amount
, ROW_NUMBER () OVER ( PARTITION BY bank_id, bank_name
ORDER BY payment_id
) AS r_num
FROM payment
SELECT NVL (i.bank_id, p.bank_id) AS bank_id
, NVL (i.bank_name, p.bank_name) AS bank_name
, i.invoice_amount
, p.payment_amount
FROM invoice_plus_r_num i
FULL OUTER JOIN payment_plus_r_num p ON i.bank_id = p.bank_id
AND i.bank_name = p.bank_name
AND i.r_num = p.r_num
ORDER BY bank_id -- you can use column aliases here
, bank_name
, NVL (i.r_num, p.r_num)
;You mentioned something about accounts, but didn't include that in the CREATE TABLE statements. You'll probably want to add that wherever I used bank_id and bank_name, above.
Are invoce and payment actually views, rather than tables? If not, you should have a separate bank table, and only include the bank_id in the other tables.
I hope this answers your question.
If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables, and also post the results you want from that data.
Explain, using specific examples, how you get those results from that data.
Always say which version of Oracle you're using.
Edited by: Frank Kulash on Feb 3, 2011 12:32 PM -
How to return mismatched rows from two tables?
I have created two tables in the same database as below which gives the row
count of tables across all databases before and after an DB restore
operation.
create table before_restore(db_name varchar(100),table_name
varchar(1000),row_count int) insert into before_restore exec sp_msforeachdb 'USE
[?]; select ''?'' as database_name,o.name,max(i.rowcnt ) From [?].sys.objects o
inner join [?].sys.sysindexes i on o.object_id=i.id where o.type=''U'' group by
o.name'
create table after_restore(db_name varchar(100),table_name
varchar(1000),row_count int) insert into after_restore exec sp_msforeachdb 'USE
[?]; select ''?'' as database_name,o.name,max(i.rowcnt ) From [?].sys.objects o
inner join [?].sys.sysindexes i on o.object_id=i.id where o.type=''U'' group by
o.name'
I want to compare these two tables and it should only return me rows that
have changed after the restore operation is complete.
Eg:
Table xyz has rowcount of 100 before restore and the restore was not proper
and after restore count is 50. So it should return me the result set as
below;
Db_name Table_Name row_count_before_restore row_count_after_restore
abc xyz 100
50
Thanks!!!Something like below perhaps? Btw, I recommend using catalog view and dynamic management views instead of the old system tables (now called compatibility views)...
SELECT
COALESCE(a.db_name, b.db_name) AS db_name
,COALESCE(a.table_name, b.table_name) AS table_name
,a.row_count AS row_count_before_restore
,b.row_count AS row_count_after_restore
FROM before_restore AS b FULL OUTER JOIN after_restore AS a ON b.db_name = a.db_name AND b.table_name = a.table_name
WHERE b.row_count <> a.row_count OR b.db_name IS NULL OR a.db_name IS NULL
Tibor Karaszi, SQL Server MVP |
web | blog -
Select Distinct rows from 3 tables
I need to retrive information from 3 different tables by applying a specific condition.
I have the following query which works fine for retrieving desired data from 2 tables.
SELECT
a.userId
FROM userGeneral
a inner
join userSpecific
b on a.userId
= b.userId
WHERE
a.userActive
= 1
userId
111
222
333
444
Now I have a third table called userMgr which may contain multiple records for each userId with a corresponding mgrId value. It has a primary key identity column called userRecId and I would like to fetch the
mgrId value corresponding to the MAX(userRecId) for the matching userId.
userRecId | userId | mgrId
1 | 111 | 123
2 | 111 | 234
3 | 111 | 345
4 | 333 | 345
5 | 333 | 456
The resultset should be as follows after joining all the 3 tables.
userId | mgrId
111 | 345
222 | NULL
333 | 456
444 | NULL
Can anyone please help with this query.I need to retrive information from 3 different tables by applying a specific condition.
I have the following query which works fine for retrieving desired data from 2 tables.
SELECT
a.userId
FROM userGeneral
a inner
join userSpecific
b on a.userId
= b.userId
WHERE
a.userActive
= 1
userId
111
222
333
444
Now I have a third table called userMgr which may contain multiple records for each userId with a corresponding mgrId value. It has a primary key identity column called userRecId and I would like to fetch the
mgrId value corresponding to the MAX(userRecId) for the matching userId.
userRecId | userId | mgrId
1 | 111 | 123
2 | 111 | 234
3 | 111 | 345
4 | 333 | 345
5 | 333 | 456
The resultset should be as follows after joining all the 3 tables.
userId | mgrId
111 | 345
222 | NULL
333 | 456
444 | NULL
Can anyone please help with this query.
try this:
select userId,userRecId,
from userMgr,
(SELECT a.userId, max(a.userRecId)
FROM userGeneral a inner join userSpecific b on a.userId = b.userId
group by a.userId
WHERE a.userActive = 1) table1(userId,userRecId)
where userMgr.userRecId = table1.userRecId -
How do I create a Search Help which selects some rows from a table?
Well, I'm trying to be specific.
I have a parameter with a search help that shows columns BUKRS and BUTXT for ALL rows form the table T001. What I need is restrict the search help to show only three Values: BUKRS = 1000 BUKRS = 2000 BUKRS = 3000. This is like a search help with a SELECT sentence. How do I do it?Hi.,
you can go through thomas sir's suggestion. But my suggestion is If you are working in a custom Report , dont change it at Table level or Domail Level., Instead, use Function Module for Custom F4 help.,
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_bukrs.
CALL FM F4IF_INT_TABLE_VALUE_REQUEST.
check this help for your reference: http://help.sap.com/saphelp_nw04/helpdata/en/9f/dbaac935c111d1829f0000e829fbfe/content.htm.
If you are using Standard Report Check for Exits.
hope this helps u.,
Thanks & Regards,
Kiran -
How to select x numbered row from two tables
Hi,
I have one performance problem...
Here is sample scenario...
TableA
A1 A2 A3
a1 a12 a13
a2 a22 a23
a3 a32 a33
TableB
B1 B2 B3
b1 b12 b13
b2 b22 b23
I want result like
A1 B1 A2 B2
a1 b1 a12 b12
a2 b2 a22 b22
I have written it like
select
A1,
B1,
A2,
B2
from
(select A1, A2, rownum rnA from TableA) a,
(select B1, B2, rownum rnB from TableB) b
where rnA = rnB
but suppose TableA has 2500000 rows and
TableB has 500 rows then for 500 rows I have to
wait for all 2500000 rows scanning.
Is there any smart solution..?
I have created indexes for on columns A1, (A1,A2,A3), B1, (B1,B2,B3).
Curious:)
Rushang Kansara
Message was edited by:
Rushang KansaraHere is explain plan
SELECT STATEMENT, GOAL = FIRST_ROWS Cost=5 Cardinality=67 Bytes=3618
HASH JOIN Cost=5 Cardinality=67 Bytes=3618
VIEW Object owner=SFMFG Cost=2 Cardinality=82 Bytes=2214
COUNT
TABLE ACCESS FULL Object owner=SFMFG Object name=TABLEA Cost=2 Cardinality=82 Bytes=1148
VIEW Object owner=SFMFG Cost=2 Cardinality=82 Bytes=2214
COUNT
TABLE ACCESS FULL Object owner=SFMFG Object name=TABLEB Cost=2 Cardinality=82 Bytes=1148
New to sql tunning Here why cardinality goes to 67 and cost to 5???
Thank you for your time.. :) -
Distinct rows from two tables linked by a third one
Hi,
I need to write an SQL sentence to bring a group of DISTINCT rows, each row in a form of: A.X, A.Y, C.IDBOLETA. Tables A and C are linked through the table C, as follow:
Table A: INDUSTRIAGEO
NIT VARCHAR2(20) NOT NULL,
CORRELAIGEO NUMERIC(2) NOT NULL,
NOMBREAGENCIA VARCHAR2(255) NOT NULL,
DIRECCION VARCHAR2(255),
X NUMERIC(12,8) NOT NULL,
Y NUMERIC(12,8) NOT NULL,
CONSTRAINT PKGEOINDUST PRIMARY KEY(NIT, CORRELAIGEO),
CONSTRAINT FKGEOINDUST FOREIGN KEY(NIT) REFERENCES INDUSTRIA(NIT)
Table B: ANALISIS
IDANALISIS NUMERIC(12) NOT NULL,
NIT VARCHAR2(20) NOT NULL,
CORRELAIGEO NUMERIC(2) NOT NULL,
FECHA DATE NOT NULL,
IDINSPECTOR NUMERIC(4) NOT NULL,
CONSTRAINT PKANALISIS PRIMARY KEY(IDANALISIS),
CONSTRAINT FKANALGEOI FOREIGN KEY(NIT, CORRELAIGEO) REFERENCES INDUSTRIAGEO(NIT, CORRELAIGEO),
CONSTRAINT FKANALINSP FOREIGN KEY(IDINSPECTOR) REFERENCES INSPECTOR(IDINSPECTOR)
Table C: ANALISISBOLETA
IDANALISIS NUMERIC(12) NOT NULL,
CORRELAANALISIS NUMERIC(2) NOT NULL,
IDBOLETA NUMERIC(12) NOT NULL,
IDCATEGORIAINDUSTRIA NUMERIC(3) NOT NULL,
CONSTRAINT PKANALBOLE PRIMARY KEY (IDANALISIS, CORRELAANALISIS),
CONSTRAINT UKANALBOLE UNIQUE (IDBOLETA),
CONSTRAINT FKANALBOLE FOREIGN KEY(IDANALISIS) REFERENCES COPS.ANALISIS(IDANALISIS),
CONSTRAINT FKANALCATI FOREIGN KEY(IDCATEGORIAINDUSTRIA) REFERENCES COPS.CATEGORIAINDUSTRIA(IDCATEGORIAINDUSTRIA)
Thanks for your help!!!
MarioPerhaps
select distinct a.x,a.y,c.idboleta
from industriaego a,
cops.analisis b,
analisisboleta c
where a.nit = b.nit
and a.correlaigeo = b.correlaigeo
and b.idanalisis = c.idanalisisRegards
Etbin
Edited by: Etbin on 4.5.2010 21:42
the copy/paste elf did it again: b.correlaigeo instead of b.a.correlaigeo (as in the OP's comment below) -
Select Distinct rows from multiple tables
Table 1 is a List of Vendors - VID, PID, VName, VAddress, VPhone
Table 2 is a list of Products - PID, PName, PPrice, PWeight, PColor
I need to produce a list of unique PID's showing the following fields - PID, VName, PName, PColor
So, Here is my failed attempt:
SELECT P.PID, V.VName, P.PName, P.PColor
FROM Products P INNER JOIN Vendors V ON P.PID = V.PID
GROUP BY P.PIDIf you post DDL, sample data and your desired output based on that sample data, someone can probably create a query that does what you want.
And I have to tell you that Table 1 is not a list of vendors unless you have a system where a vendor provides one and only one product - something that is unusual. If your system is one where a vendor should provide any number of products (and if a product
can be provided by any number of vendors), you have some fundamental schema issues to correct.
Lastly, there are sticky posts at the top of the forum that provide suggestions for posting questions. Please have a look - help your readers help you by providing sufficient information. Phrases such as "failed" or "does not work"
do not provide any useful detail. And one good rule of thumb - any time you feel you need to use (or say) distinct in a query is an indication that something somewhere is wrong. It could be a schema issue, a misunderstanding of the schema or the
goal, an incorrect query, etc. There are few instances where distinct is needed in a well-defined and implemented system, IMO. -
Newbie question: Select one row from table in PL/SQL
Hi,
I want to select one row from the table Employee where Emplyoyee Number is say 200. This is a simple SQL query, but I don't know the equivalent PL/SQL format. I will have 3 out params here - Id itself, Name, Salary. I will then have to populate a java resultset object from these out params.
Later, I'll have to use cursors to retrieve more than one row.
Thanks for any help.Perhaps something like
CREATE OR REPLACE PROCEDURE get_employee( l_id IN OUT employee.id%TYPE,
l_name OUT employee.name%TYPE,
l_salary OUT employee.salary%TYPE )
AS
BEGIN
SELECT name, salary
INTO l_name, l_salary
FROM employee
WHERE id = l_id;
END;Justin
Distributed Database Consulting, Inc.
http://www.ddbcinc.com/askDDBC
Maybe you are looking for
-
Upgrade 11.5.9 32 bit to 12.1.3 64 bit
Hi, Planning for the upgrade of present environment. 11.5.9 with 9.2.0.3 Database running on single node 32 bit proposed system is 12.1.3 and 11g database Single node 64bit. Can any one suggest the upgrade path to follow. what i came to know by readi
-
How to increase line-size in Function module
Hi All, How to increase line-size in function module.. I am displaying a list in function module.The output is of width more than 150.. Regards, Srinivas
-
Hi ... I am attempting to create an iMovie that will include photos and video footage. I read that the best presentation quality for the photos would be achieved creating a slideshow in iPhoto and exporting the slideshow, and importing the resulting
-
How do I get the HP Laserjet P3005 to work in Vista?
I have tried every driver that is listed in the download drivers area. I have connected the printer via usb, it says the generic driver won't work. Can not get the printer to recognize. Now, when I plug the printer in to the usb port, it shows up in
-
TEM - Cost Transfer Posting to CO(Business Event Cost)
Dear All, Kindly suggest solution for the following issue I have maintained all the cost items with cost elements I have booked an empolyee for a business event with resources I have maintained receivers cost center in SEMIN SCOST (T77S0) I have cre