Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Need help ANSI-92'ing a subquery join Expand / Collapse
Author
Message
Posted Wednesday, June 26, 2013 8:04 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, September 3, 2014 1:26 PM
Points: 8, Visits: 277
I'm working on converting some old Sybase DBs to SQL, and am stuck on one query. Here's the original:

/* original
SELECT A.recid,
A.id,
A.procid,
A.pstatus,
B.pname,
B.pdesc,
LEN(pname) AS plen,
CASE
WHEN C.seq IS NULL THEN 0
ELSE 1
END AS DISABLED
FROM lprocs A,
pmaster B,
pmap C
WHERE A.procid = B.procid
AND A.id = @id
AND C.procid =* A.procid
AND C.typeid =* (
SELECT D.typeid
FROM loan D
WHERE D.id = A.id
)
ORDER BY pname
*/

And here's my (weak) attempt
/* my attempt - obviously produces a different query plan
SELECT A.recid,
A.id,
A.procid,
A.pstatus,
B.pname,
B.pdesc,
LEN(pname) AS plen,
CASE
WHEN C.seq IS NULL THEN 0
ELSE 1
END AS DISABLED
FROM lprocs A
JOIN pmaster B
ON A.procid = B.procid
LEFT OUTER JOIN pmap C
ON C.procid = A.procid
WHERE
A.id = @id
AND C.typeid in (
SELECT D.typeid
FROM loan D
WHERE D.id = A.id
)
ORDER BY pname
*/

Please help, thanks!
Post #1467927
Posted Thursday, June 27, 2013 1:06 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 2:01 PM
Points: 2,304, Visits: 2,772
CleverSQLUserID (6/26/2013)
- obviously produces a different query plan


The query plan is not the indication that your new-style code is false or correct. Your resultset must be the same with both queries. Your second query looks good to me, but I must admit I'm not familiar with the old-style code.
Will this code produce the same resultset?
SELECT A.recid
, A.id
, A.PROCID
, A.pstatus
, B.pname
, B.pdesc
, LEN(pname) AS plen
, CASE
WHEN C.seq IS NULL
THEN 0
ELSE 1
END AS DISABLED
FROM lprocs A
INNER JOIN pmaster B
ON A.PROCID = B.PROCID
LEFT OUTER JOIN pmap C
ON C.PROCID = A.PROCID
AND C.typeid IN (
SELECT D.typeid
FROM loan D
WHERE A.id = D.id
)
WHERE A.id = @id
ORDER BY pname



** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
Post #1467974
Posted Thursday, June 27, 2013 8:24 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:34 PM
Points: 13,481, Visits: 12,342
IIRC =* is a right join, not a left join. *= is a left join.

SELECT A.recid,
A.id,
A.PROCID,
A.pstatus,
B.pname,
B.pdesc,
LEN(pname) AS plen,
CASE WHEN C.seq IS NULL THEN 0 ELSE 1 END AS DISABLED
FROM lprocs A,
inner join master B on A.PROCID = B.PROCID
right join pmap C on C.PROCID = A.PROCID
and C.typeid = (SELECT D.typeid FROM loan D WHERE D.id = A.id) --be careful here, this subquery could return more than 1 row
WHERE A.id = @id
ORDER BY pname

Be careful with that subquery, the subquery may return more than 1 row.

As a side note, it is a pet peeve of mine to use aliases a,b,c,d. They don't mean anything and I find actually make it more difficult to work with. You have to constantly keep referring to the from section of your query to remember which table is which.

Something like this. It is much easier to know what table any given column belongs to.

SELECT p.recid,
p.id,
p.PROCID,
p.pstatus,
m.pname,
m.pdesc,
LEN(pname) AS plen,
CASE WHEN m.seq IS NULL THEN 0 ELSE 1 END AS DISABLED
FROM lprocs p,
inner join [master] m on p.PROCID = m.PROCID]
inner join loan l on l.id = p.id
right join pmap m on m.PROCID = p.PROCID
and m.typeid = l.typeid
WHERE p.id = @id
ORDER BY pname



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1468184
Posted Thursday, June 27, 2013 8:33 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, September 3, 2014 1:26 PM
Points: 8, Visits: 277
Thanks for the assistance. I agree with your take on using a/b/c as aliases, but I was tasked with "get it working, don't get it pretty". I thought that replacing an outer join with an "IN" was risky, but I think it's riskier to replace it with an "=", since, like you said, the query could return more than one result. I'll have to ask the business unit if that's by design, but is there not a way to maintain the join relationship?
Post #1468191
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse