Oracle SQL Convert to Join Operator Style - oracle

The query works, but I was asked to rewrite it in join operator style.
SELECT pr.ProdName, pr.ProdPrice
FROM Product pr, OrderTbl ord, OrdLine ol, Customer cu
WHERE pr.ProdNo=ol.ProdNo
AND ord.OrdNo=ol.OrdNo
AND cu.CustNo=ord.CustNo
AND cu.CustNo='C2388597'
AND ord.OrdDate BETWEEN '01-Jan-04' AND '31-Jan-04';
I've just started out using Oracle, so I'm not sure how. I think I should be using INNER JOINS, but I don't know exactly how. Could someone help me, please?

You should definitly read about joins though, but here is how this cross join style can be converted to inner join
SELECT pr.ProdName, pr.ProdPrice
FROM Product pr
INNER JOIN OrdLine ol ON pr.ProdNo=ol.ProdNo
INNER JOIN OrderTbl ord ON ord.OrdNo = ol.OrdNo
INNER JOIN Customer cu ON cu.CustNo=ord.CustNo
WHERE cu.CustNo='C2388597'
AND ord.OrdDate BETWEEN '01-Jan-04' AND '31-Jan-04';

SELECT pr.ProdName, pr.ProdPrice
FROM Product pr
INNER JOIN OrdLine ol ON pr.ProdNo=ol.ProdNo
INNER JOIN OrderTbl ord ON ord.OrdNo = ol.OrdNo
INNER JOIN Customer cu ON cu.CustNo=ord.CustNo
WHERE cu.CustNo='C2388597'
AND ord.OrdDate BETWEEN '01-Jan-04' AND '31-Jan-04';
Here is for you to read on Joins:.

Related

how to use full outer join writing query on multiple tables?

I have a below query
How to use full outer join for TABLE T4 for getting all records?
WHERE
(DB.T4.AUTH_REV_NO=DB.T2.AUTH_REV_NO
AND DB.T4.AUTH_NO=DB.T2.AUTH_NO)
AND (DB.T2.AUTH_CURR_IN='Y' )
AND (DB.T3.AUTH_NO=DB.T2.AUTH_NO)
AND (DB.T3.AUTH_REV_NO=DB.T2.AUTH_REV_NO )
AND (DB.T6.FNC_ID=DB.T4.FNC_ID)
AND (DB.T7.FNC_SEG_ID=DB.T6.FNC_SEG_ID)
AND (DB.T1.SCT_ID(+)=DB.T7.SCT_ID
AND DB.T1.FNC_SEG_ID(+)=DB.T7.FNC_SEG_ID)
AND (DB.T8.NDE_ID=DB.T12.NDE_ID)
AND (DB.T7.FNC_SEG_ID=DB.T8.FNC_SEG_ID)
AND (DB.T7.SCT_ID=DB.T8.SCT_ID)
AND ((DB.T12.NDE_ID=DB.T6.NDE_STRT_ID)
OR (DB.T12.NDE_ID=DB.T6.NDE_END_ID))
AND (DB.T5.FNC_ID(+)=DB.T4.FNC_ID)
AND (T13_A4.REF_ID(+)=DB.T5.REF_TONE_TYP_ID)
AND (fne.FNC_SEG_ID=DB.T8.FNC_SEG_ID)
AND (fne.NDE_ID=DB.T8.NDE_ID)
AND (fne.SCT_ID=DB.T8.SCT_ID)
AND fnode.NDE_ID=DB.T6.NDE_STRT_ID
AND tnode.NDE_ID=DB.T6.NDE_END_ID
AND (DB.T4.REF_FNC_TYP_ID=T13_A1.REF_ID)
AND (ne_port.NDE_EQP_ID=fne.NDE_EQP_ID)
AND (ne_port.NDE_EQP_PRN_ID=ne_card.NDE_EQP_ID)
AND (ne_card.NDE_EQP_PRN_ID=ne_shelf.NDE_EQP_ID)
AND (ne_shelf.NDE_EQP_PRN_ID=ne_rack.NDE_EQP_ID)
AND (eq.EQP_ID=ne_card.EQP_ID)
AND (eq.REF_EQP_CLS_ID=T13_A2.REF_ID)
AND (DB.T3.REF_AUTH_STS_ID=T13_A3.REF_ID)
AND (DB.T3.AUTH_STS_ID
IN (SELECT MAX(DB.T3.AUTH_STS_ID) FROMDB.T3
WHERE (DB.T3.AUTH_NO,DB.T3.AUTH_REV_NO)
IN
(SELECT
DB.T3.AUTH_NO,
MAX(DB.T3.AUTH_REV_NO)
FROM
DB.T3
GROUP BY
DB.T3.AUTH_NO)
GROUP BY
DB.T3.AUTH_NO))
How to use full outer join for TABLE T4 and for COLUMN FNC_TONE_LVL_QT to get all records.
Please help.
You posted a whole lot of "joins". I'm not going to rewrite it for you, but - I'd suggest you to switch to a more recent explicit JOIN syntax which makes things somewhat simpler and easier to understand as you'd separate joins from conditions. Moreover, it allows you to outer join the same table to more than just one another table, which is impossible with the old (+) Oracle's outer join operator.
Something like this
select ...
from table_1 a left join table_2 b on a.id = b.id
full outer join table_3 c on c.id = a.id
...

multi left outer join query

I'm searching for Linq left outer join query syntax, and I'm struggling to find a good answer for that. My specific SQL query is:
Select tblBpGood.barcode, tblBPGood.Code, Name, RetailPrice, GoodGroupID, tblBPGoodGroupL.Description,
PurchaseUnitTypeID, SalesUnitTypeID, tblBPUnitTypeL.Description as SalesUnitType,
tblBPUnitTypeL_1.Description as PurchaseUnitType
from tblBPGood
left outer join tblBPGoodGroupL
on tblBPGood.GoodGroupID = tblBPGoodGroupL.ID
left outer join tblBPUnitTypeL
on tblBPGood.SalesUnitTypeID = tblBPUnitTypeL.id
left outer join tblBPUnitTypeL as tblBPUnitTypeL_1
on tblBPGood.PurchaseUnitTypeID = tblBPUnitTypeL_1.id

Oracle's OUTER JOIN (+) on string - Migration PostgreSQL

I'm migrating a client's software database from Oracle to PostgreSQL, and I have some trouble understanding a query, what it does, and consequently how to migrate it.
The query is:
SELECT *
FROM TBL1, TBL2, TBL3, TBL4
WHERE TBL3.Project_ID = TBL1.Project_ID
AND TBL2.Type_ID = TBL1.Type_ID
AND TBL4.PROPERTY_NAME(+)='Id'
AND TBL4.Entity_ID(+)=TBL1.Entity_ID
And the part I don't get, is the outer join (+) on 'Id'.
A join on a table, OK, but on a string? I've no idea of what it does.
Do someone has an idea?
Thanks.
TBL4.PROPERTY_NAME(+)='Id' means when the line was inner joined, then the value has to be 'Id', but when the line was outer joined, the condition is evaluated as true
however you should rewrite the statement to the standard as:
SELECT *
FROM TBL1
JOIN TBL2 ON TBL2.Type_ID = TBL1.Type_ID
JOIN TBL3 ON TBL3.Project_ID = TBL1.Project_ID
LEFT JOIN TBL4 ON TBL4.Entity_ID=TBL1.Entity_ID AND TBL4.PROPERTY_NAME='Id'
This is the equivalent of the following query using ANSI join syntax:
SELECT *
FROM TBL1 t1
INNER JOIN TBL2 t2 ON (t1.Type_ID = t2.Type_ID)
INNER JOIN TBL3 t3 ON (t3.Project_ID = t1.Project_ID)
LEFT JOIN TBL4 t4 ON (t4.Entity_ID = t1.Entity_ID AND t4.PROPERTY_NAME = 'Id')
You're not joining to a string, merely specifying a join condition that's based on one.

Linq query only returning 1 row

Dim ds = From a In db.Model
Join b In db.1 On a.id Equals b.ID
Join c In db.2 On a.id Equals c.ID
Join d In db.3 On a.id Equals d.ID
Join f In db.4 On a.id Equals f.ID
Select a.id, a.Ref, a.Type, a.etc
Above is my linq query. At the moment I am only getting the first row from the db returned when there are currently 60 rows. Please can you tell me where I am going wrong and how to select all records.
Thanks in advance!
UPDATE:
When I take out all the joins like so:
Dim ds = From a In db.1, b In db.2, c In db.3, d In db.4, f In db.5
Select a.id, a.Ref, a.type, b.etc, c.etc, d.etc
I get a system.outofmemory exception!
You're only going to get a row produced when all of the joins match - in other words, when there's a row from Model with an AP, an Option, a Talk and an Invoice. My guess is that there's only one of those.
LINQ does an inner join by default. If you're looking for a left outer join (i.e. where a particular row may not have an Invoice, or a Talk etc) then you need to use a group join, usually in conjunction with DefaultIfEmpty.
I'm not particularly hot on VB syntax, but this article looks like it's what you're after.

LINQ nested joins

Im trying to convert a SQL join to LINQ. I need some help in getting the nested join working in LINQ.
This is my SQL query, Ive cut it short just to show the nested join in SQL:
select distinct
txtTaskStatus as TaskStatusDescription,
txtempfirstname+ ' ' + txtemplastname as RaisedByEmployeeName,
txtTaskPriorityDescription as TaskPriorityDescription,
dtmtaskcreated as itemDateTime,
dbo.tblTask.lngtaskid as TaskID,
dbo.tblTask.dtmtaskcreated as CreatedDateTime,
convert(varchar(512), dbo.tblTask.txttaskdescription) as ProblemStatement,
dbo.tblTask.lngtaskmessageid,
dbo.tblMessage.lngmessageid as MessageID,
case when isnull(dbo.tblMessage.txtmessagesubject,'') <> '' then txtmessagesubject else left(txtmessagedescription,50) end as MessageSubject,
dbo.tblMessage.txtmessagedescription as MessageDescription,
case when dbo.tblMessage.dtmmessagecreated is not null then dbo.tblMessage.dtmmessagecreated else CAST(FLOOR(CAST(dtmtaskcreated AS DECIMAL(12, 5))) AS DATETIME) end as MessageCreatedDateTime
FROM
dbo.tblAction RIGHT OUTER JOIN dbo.tblTask ON dbo.tblAction.lngactiontaskid = dbo.tblTask.lngtaskid
LEFT OUTER JOIN dbo.tblMessage ON dbo.tblTask.lngtaskmessageid = dbo.tblMessage.lngmessageid
LEFT OUTER JOIN dbo.tblTaskCommentRecipient
RIGHT OUTER JOIN dbo.tblTaskComment ON dbo.tblTaskCommentRecipient.lngTaskCommentID = dbo.tblTaskComment.lngTaskCommentID
ON dbo.tblTask.lngtaskid = dbo.tblTaskComment.lngTaskCommentTaskId
A more seasoned SQL programmer wouldn't join that way. They'd use strictly left joins for clarity (as there is a strictly left joining solution available).
I've unraveled these joins to produce a hierarchy:
Task
Action
Message
TaskComment
TaskCommentRecipient
With associations created in the linq to sql designer, you can reach these levels of the hierarchy:
//note: these aren't outer joins
from t in db.Tasks
let actions = t.Actions
let message = t.Messages
let comments = t.TaskComments
from c in comments
let recipients = c.TaskCommentRecipients
DefaultIfEmpty produces a default element when the collection is empty. Since these are database rows, a default element is a null row. That is the behavior of left join.
query =
(
from t in db.Tasks
from a in t.Actions.DefaultIfEmpty()
from m in t.Messages.DefaultIfEmpty()
from c in t.Comments.DefaultIfEmpty()
from r in c.Recipients.DefaultIfEmpty()
select new Result()
{
TaskStatus = ???
...
}
).Distinct();
Aside: calling Distinct after a bunch of joins is a crutch. #1 See if you can do without it. #2 If not, see if you can eliminate any bad data that causes you to have to call it. #3 If not, call Distinct in a smaller scope than the whole query.
Hope this helps.
SELECT [t0].[OrderID], [t0].[CustomerID], [t0].[EmployeeID], [t0].[OrderDate], [t0].[RequiredDate], [t0].[ShippedDate], [t0].[ShipVia], [t0].[Freight], [t0].[ShipName], [t0].[ShipAddress], [t0].[ShipCity], [t0].[ShipRegion], [t0].[ShipPostalCode], [t0].[ShipCountry]
FROM [Orders] AS [t0]
LEFT OUTER JOIN ([Order Details] AS [t1]
INNER JOIN [Products] AS [t2] ON [t1].[ProductID] = [t2].[ProductID]) ON [t0].[OrderID] = [t1].[OrderID]
can be write as
from o in Orders
join od in (
from od in OrderDetails join p in Products on od.ProductID equals p.ProductID select od)
on o.OrderID equals od.OrderID into ood from od in ood.DefaultIfEmpty()
select o

Resources