how to outer-join Tables using ANSI sql [duplicate] - sql

This question already has answers here:
Oracle outer join syntax
(1 answer)
How do I convert a "legacy" left outer join statement in Oracle?
(1 answer)
Left Outer Join using + sign in Oracle 11g
(3 answers)
Oracle "(+)" Operator
(4 answers)
Closed 5 years ago.
I am working on 2 Tables. One contains 657 rows matching my filter criteria, and the other contains 193 records.
I successfully joined them using Oracle's "AFAIK" syntax:
select
ecp.portfolio_acct, ecp.posn_id cost_posn_id, ecp.asset_id
from MID_COST_POSITION ecp, MID_CASH_POSITION cas
where ecp.portfolio_acct = 10183306
and ecp.portfolio_acct = cas.portfolio_acct(+)
and ecp.asset_id = cas.asset_id(+)
;
However, I cannot seem to replicate these results using ANSI-SQL (or, ISO-sql):
select
ecp.portfolio_acct, ecp.posn_id cost_posn_id, ecp.asset_id
from MID_COST_POSITION ecp
LEFT OUTER JOIN MID_CASH_POSITION cas
on ecp.portfolio_acct = cas.portfolio_acct
where ecp.asset_id = cas.asset_id
and ecp.portfolio_acct = 10183306
;
I have tried various JOINs without success. In the 1st example I had to Outer Join 2 separate fields, which I do not know how to accomplish using ANSI-SQL.
I appreciate any guidance anyone can offer. Thank you!

select
ecp.portfolio_acct, ecp.posn_id cost_posn_id, ecp.asset_id
from MID_COST_POSITION ecp
LEFT OUTER JOIN MID_CASH_POSITION cas
on ecp.portfolio_acct = cas.portfolio_acct
and ecp.asset_id = cas.asset_id
Where ecp.portfolio_acct = 10183306
;
You were turning this into an inner join with the following where condition because it now has to meet that condition in the result set:
ecp.asset_id = cas.asset_id

Related

Replacing oracle specific notation (+) with LEFT JOIN and where clause [duplicate]

This question already has answers here:
How do I convert a "legacy" left outer join statement in Oracle?
(1 answer)
Assistance replacing oracle (+) joins to ANSI joins
(4 answers)
Oracle LEFT OUTER JOIN on 3+ tables - (+) Syntax versus ANSI Syntax
(3 answers)
Convert old "+" Join to SQL Server syntax?
(2 answers)
Closed 1 year ago.
Lately the (+) has been causing some issues for me, maybe it's depreciated by now.
Now I want to replace the (+) notation with LEFT JOIN, but I can't seem to get the syntax in order.
This is what I have:
SELECT x.a, y.a, z.a
FROM x, y, z
WHERE x.k = y.k(+)
AND x.p = z.p(+);
How can I replace the (+) with a LEFT JOIN?
-EDIT- I've read the "duplicated" answers, and few of them have accepted answers (no wonder why)
I could not solve my problem according to those.
The answer provided here was spot on and solved it - marked it as accepted.
I got another one that is a little bit more confusing (to me)
FROM a,b,c
WHERE b.a = '101'
AND a.a = '202'
AND b.c = a.c
AND a.d = c.d(+)
AND ROWNUM = 1;
Again, I want to replace the (+) with LEFT JOIN
The equivalent syntax would be:
SELECT x.a, y.a, z.a
FROM x LEFT JOIN
y
ON x.k = y.k LEFT JOIN
z
ON x.p = z.p;

Legacy Query Sybase to TSQL MSSQL migration with multiple left join with or operator

I would like to convert this sybase legacy sql code to ansi tsql new standard for ms sql, but I can't find the right way. Any help would be appreciated.
SELECT 3 FROM x_linea_cred, x_linea_cred_priv, x_clt_prd
WHERE x_clt_prd.r_client = #rclient AND
(x_clt_prd.nro_prod *= x_linea_cred.nro_prod or
x_clt_prd.nro_prod *= x_linea_cred_priv.nro_prod))
For a while now, the standard notation for outer join is LEFT [OUTER] JOIN. Your query should be converted to:
select 3
from x_clt_prd c
left join x_linea_cred lc on lc.nro_prod = c.nro_prod
left join x_linea_cred_priv lcp on lcp.nro_prod = c.nro_prod
where c.r_client = #rclient

Select max(date) in Firebird [duplicate]

This question already has answers here:
Select first row in each GROUP BY group?
(20 answers)
Firebird Query- Return first row each group
(4 answers)
Closed 5 years ago.
I have a table in a Firebird 2.5 Database where I have to find out the latest entry for one specific articel no. Therefore the table has a colum "date".
I tried several queries but I always get more than 1 entry for the article no.
I used the following query:
select max(lp.datum) as Wareneingangsdatum, lp.nr, lp.artikelnr, lp.belegnr, lf.nr, lf.name, lf.plz, lf.ort, lf.laenderkuerzelnr, lae.bezeichnung
from lagerprotokoll lp
left join waeinpos we on we.idnr = lp.belegposidnr
left join waein we1 on we1.nr = we.nr
left join liefrant lf on lf.nr = we1.lieferantnr
left join laender lae on lae.nr = lf.laenderkuerzelnr
where lp.belegtyp = 14
group by 2,3,4,5,6,7,8,9, 10
order by 1 desc
How can I achieve the result so that I can only get the latest entry for one specific article in the table.

SQL Server : multi-join with tuple IN clause

I'm trying to join 4 tables that have a somewhat complex relationship. Because of where this will be used, it needs to be contained in a single query, but I'm having trouble since the primary query and the IN clause query both join 2 tables together and the lookup is on two columns.
The goal is to input a SalesNum and SalesType and have it return the Price
Tables and relationships:
sdShipping
SalesNum[1]
SalesType[2]
Weight[3]
sdSales
SalesNum[1]
SalesType[2]
Zip[4]
spZones
Zip[4]
Zone[5]
spPrices
Zone[5]
Price
Weight[3]
Here's my latest attempt in T-SQL:
SELECT
spp.Price
FROM
spZones AS spz
LEFT OUTER JOIN
spPrices AS spp ON spz.Zone = spp.Zone
WHERE
(spp.Weight, spz.Zip) IN (SELECT ship.Weight, sales.Zip
FROM sdShipping AS ship
LEFT OUTER JOIN sdSales AS sales ON sales.SalesNum = ship.SalesNum
AND sales.SalesType = ship.SalesType
WHERE sales.SalesNum = (?)
AND ship.SalesType = (?));
SQL Server Management Studio says I have an error in my syntax near ',' (appropriately useless error message). Does anybody have any idea whether this is even allowed in Microsoft's version of SQL? Is there perhaps another way to accomplish it? I've seen the multi-key IN questions answered on here, but never in the case where both sides require a JOIN.
Many databases do support IN on tuples. SQL Server is not one of them.
Use EXISTS instead:
SELECT spp.Price
FROM spZones spz LEFT OUTER JOIN
spPrices spp
ON spz.Zone = spp.Zone
WHERE EXISTS (SELECT 1
FROM sdShipping ship LEFT JOIN
sdSales sales
ON sales.SalesNum = ship.SalesNum AND
sales.SalesType = ship.SalesType
WHERE spp.Weight = ship.Weight AND spz.Zip = sales.Zip AND
sales.SalesNum = (?) AND
ship.SalesType = (?)
);

How do I write the equivalent of a SQL LEFT JOIN in LINQ? [duplicate]

This question already has answers here:
LINQ to SQL Left Outer Join
(6 answers)
left outer join problem
(1 answer)
Closed 9 years ago.
I need to convert this SQL to LINQ, and would really appreciate some help.
SELECT * FROM Adx_eventSet AS es
LEFT JOIN afx_eventsponsor_eventSet AS spon
ON es.Adx_eventId = spon.adx_eventid
Ive tried this but it's not a left join and so only pulled in the one result.
from t in Adx_eventSet
join x in adx_eventsponsor_eventSet on t.Adx_eventId equals x.adx_eventid
select t
You want to use DefaultIfEmpty(). See below.
var leftJoin = from adx_event in Adx_eventSet
join adx_eventsponsor in adx_eventsponsor_eventSet
on adx_event.Adx_eventId equals adx_eventsponsor.adx_eventid into j
from adx_eventsponsor in j.DefaultIfEmpty()
select new
{
Name = adx_event.Name,
Name = adx_eventsponsor != null ? adx_eventsponsor.Name : null
};
Assuming each table has a Name property.