Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PL/SQL - How to return single row from a joined table

Tags:

sql

oracle

plsql

this might be quite simple I'm just not seeing the wood for the trees at the moment. In Oracle I'm selecting records from table A that joins to table B based on the primary key of table A. However table B can have multiple records matching the primary key of table A. This is causing my query to return duplicate rows from table A. Below is a cut down version of my query:

TableA                TableB
_______               _________
1, Sec1                2, 11/01/2011
2, Sec2                2
3, Sec3                5, 10/01/2011
4, Sec4                6, 10/01/2011

Select A.SecID, A.SecName, B.DateSent from tableA A   
  inner join tableB B on A.SecID = B.SecID

This is returning 2 records for Sec2 - how can I get it to return only 1 record for Sec2? I've tried using distinct and unique but still get the same results.

like image 368
Ciarán Bruen Avatar asked Jan 28 '11 14:01

Ciarán Bruen


People also ask

How can I get just one row in SQL?

To return only the first row that matches your SELECT query, you need to add the LIMIT clause to your SELECT statement. The LIMIT clause is used to control the number of rows returned by your query. When you add LIMIT 1 to the SELECT statement, then only one row will be returned.

Which join is used to return rows?

A CROSS join returns all rows for all possible combinations of two tables. It generates all the rows from the left table which is then combined with all the rows from the right table. This type of join is also known as a Cartesian product(A*B).

What is the use of select 1 from dual?

In your case, SELECT 1 FROM DUAL; will simply returns 1 . You need it because the INSERT ALL syntax demands a SELECT clause but you are not querying the input values from a table. Dual is not a temporary table.

Which type of join returns rows from two table only if there is a match?

Outer joins are joins that return matched values and unmatched values from either or both tables. There are a few types of outer joins: LEFT JOIN returns only unmatched rows from the left table, as well as matched rows in both tables.


2 Answers

SELECT  secid, secname
FROM    tableA
WHERE   secid IN
        (
        SELECT  secid
        FROM    tableb
        )

If you need a record from tableB as well:

SELECT  secid, secname, datesent
FROM    (
        SELECT  a.secid, a.secname, b.datesent, ROW_NUMBER() OVER (PARTITION BY a.secid ORDER BY b.datesent DESC) AS rn
        FROM   tableA a
        JOIN   tableB b
        ON     b.secid = a.secid
        )
WHERE   rn = 1

ORDER BY clause controls which of the multiple records on b will you get.

like image 63
Quassnoi Avatar answered Sep 22 '22 16:09

Quassnoi


You can use a GROUP function to select only one row:

SELECT A.SecID, A.SecName, max(B.DateSent) DateSent
  FROM tableA A   
  JOIN tableB B on A.SecID = B.SecID
 GROUP BY A.SecID, A.SecName
like image 33
Vincent Malgrat Avatar answered Sep 20 '22 16:09

Vincent Malgrat