473,386 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

need help with converting old style subselect to ANSI joins?

Help!

I'm trying to understand the new ANSI join syntax (after many years of
coding using the old style). I am now working with an application that only
understands ANSI syntax so I am struggling.

My first (old style syntax) SQL statement below produces 60 rows:

SELECT A1.CONTACTID, A1.LASTNAME, A1.FIRSTNAME, A1.ACCOUNT,
A6.CITY, A6.STATE, A1.WORKPHONE, A1.FAX, A1.EMAIL
FROM CONTACT A1,
ADDRESS A6
WHERE A1.ADDRESSID=A6.ADDRESSID
AND A1.CONTACTID IN
(SELECT A4.CONTACTID
FROM CONTACT_LEADSOURCE A4,
LEADSOURCE A5
WHERE A4.LEADSOURCEID = A5.LEADSOURCEID
AND A5.DESCRIPTION = 'some_description' )
AND A1.CONTACTID IN
(SELECT A2.CONTACTID
FROM TICKET A2,
ENROLLHX A3,
EVENT A7
WHERE A3.STATUS IN ('R', 'Confirmed')
AND A2.TICKETID = A3.EVXEVTICKETID
AND A3.EVENTID = A7.EVENTID
AND A7.CODE IN
('AHS00','AHS01','AHS02','AHS03','AHS04','AHS98',' AHS99'))
ORDER BY A1.LASTNAME ASC

I am trying to convert this to the newer ANSI sytax. My second SQL statement
below produces 67 rows (duplicates):

SELECT A1.CONTACTID, A1.LASTNAME, A1.FIRSTNAME, A1.ACCOUNT,
A6.CITY, A6.STATE, A1.WORKPHONE, A1.FAX, A1.EMAIL
FROM CONTACT A1
JOIN ADDRESS A6 ON (A1.ADDRESSID=A6.ADDRESSID)
JOIN
( SELECT C.CONTACTID
FROM CONTACT C
JOIN CONTACT_LEADSOURCE A4 ON (C.CONTACTID
= A4.CONTACTID)
JOIN LEADSOURCE A5 ON (A4.LEADSOURCEID =
A5.LEADSOURCEID
AND A5.DESCRIPTION =
'some_description' )) AS C1 ON C1.CONTACTID = A1.CONTACTID
JOIN
(SELECT C2.CONTACTID
FROM CONTACT C2
JOIN TICKET A2 ON (C2.CONTACTID =
A2.CONTACTID)
JOIN ENROLLHX A3 ON (A2.TICKETID =
A3.TICKETID AND A3.STATUS in ('R', 'Confirmed'))
JOIN EVENT A7 ON (A3.EVENTID = A7.EVENTID

AND A7.CODE IN ('AHS00','AHS01','AHS02','AHS03','AHS04','AHS98',' AHS99')))
AS C3 ON C3.CONTACTID = A1.CONTACTID
Can anyone shed some light on what I am missing?

cheers,
Norm

Jul 20 '05 #1
3 2200
"ColdCanuck" <co**@canuck.ca> wrote in message
news:2lZGc.2076$iw3.1593@clgrps13...
Help!

I did manage to figure it out. The key was using DISTINCT.

FWIW, my SQL ended up being :

SELECT DISTINCT A1.CONTACTID, A1.LASTNAME, A1.FIRSTNAME, A1.ACCOUNT,
A2.CITY,
A2.STATE, A1.WORKPHONE, A1.FAX, A1.EMAIL
FROM CONTACT A1
JOIN ADDRESS A2 ON (A1.ADDRESSID=A2.ADDRESSID)
LEFT JOIN CONTACT_LEADSOURCE A3 ON
(A1.CONTACTID=A3.CONTACTID)
JOIN TICKET A5 ON (A3.CONTACTID=A5.CONTACTID)
JOIN ENROLLHX A6 ON (A5.TICKETID=A6.TICKETID)
JOIN EVENT A7 ON (A6.EVENTID=A7.EVENTID)
LEFT JOIN LEADSOURCE A4 ON
(A3.LEADSOURCEID=A4.LEADSOURCEID)
WHERE A4.DESCRIPTION = 'some_description'
AND A7.CODE IN
('AHS00','AHS01','AHS02','AHS03','AHS04','AHS98',' AHS99')
ORDER BY A1.LASTNAME ASC
Jul 20 '05 #2
ColdCanuck (co**@canuck.ca) writes:
I am trying to convert this to the newer ANSI sytax. My second SQL
statement below produces 67 rows (duplicates):

SELECT A1.CONTACTID, A1.LASTNAME, A1.FIRSTNAME, A1.ACCOUNT,
A6.CITY, A6.STATE, A1.WORKPHONE, A1.FAX, A1.EMAIL
FROM CONTACT A1
JOIN ADDRESS A6 ON (A1.ADDRESSID=A6.ADDRESSID)
JOIN
( SELECT C.CONTACTID
...


You should have left those IN clauss alone. Or rewritten them as EXISTS
clauses. If you ADDRESS with the subquery, a row from ADDRESS is included
for every time it matches the derived table. You only want it included
once if it matches at all. Whether it matches 1 or ten times is irrelevant
to you.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #3
Hi

The join syntax only requires the method that the two tables are being
related to each other, the rest of the where clause can stay the same:

SELECT A1.CONTACTID, A1.LASTNAME, A1.FIRSTNAME, A1.ACCOUNT,
A6.CITY, A6.STATE, A1.WORKPHONE, A1.FAX, A1.EMAIL
FROM CONTACT A1
JOIN ADDRESS A6 ON A1.ADDRESSID=A6.ADDRESSID
WHERE A1.CONTACTID IN
(SELECT A4.CONTACTID
FROM CONTACT_LEADSOURCE A4
JOIN LEADSOURCE A5 ON A4.LEADSOURCEID = A5.LEADSOURCEID
WHERE A5.DESCRIPTION = 'some_description' )
AND A1.CONTACTID IN
(SELECT A2.CONTACTID
FROM TICKET A2
JOIN ENROLLHX A3 ON A2.TICKETID = A3.EVXEVTICKETID
JOIN EVENT A7 ON A3.EVENTID = A7.EVENTID
WHERE A3.STATUS IN ('R', 'Confirmed')
AND A7.CODE IN
('AHS00','AHS01','AHS02','AHS03','AHS04','AHS98',' AHS99'))
ORDER BY A1.LASTNAME ASC

Without checking I think this may be the same!

SELECT A1.CONTACTID, A1.LASTNAME, A1.FIRSTNAME, A1.ACCOUNT,
A6.CITY, A6.STATE, A1.WORKPHONE, A1.FAX, A1.EMAIL
FROM CONTACT A1
JOIN ADDRESS A6 ON A1.ADDRESSID=A6.ADDRESSID
JOIN CONTACT_LEADSOURCE A4 ON A1.CONTACTID = A4.CONTACTID
JOIN LEADSOURCE A5 ON A4.LEADSOURCEID = A5.LEADSOURCEID AND
A5.DESCRIPTION = 'some_description'
JOIN TICKET A2 ON A1.CONTACTID ON A2.CONTACTID
JOIN ENROLLHX A3 ON A2.TICKETID = A3.EVXEVTICKETID AND A3.STATUS IN ('R',
'Confirmed')
JOIN EVENT A7 ON A3.EVENTID = A7.EVENTID AND A7.CODE IN
('AHS00','AHS01','AHS02','AHS03','AHS04','AHS98',' AHS99')
ORDER BY A1.LASTNAME ASC

John

"ColdCanuck" <co**@canuck.ca> wrote in message
news:2lZGc.2076$iw3.1593@clgrps13...
Help!

I'm trying to understand the new ANSI join syntax (after many years of
coding using the old style). I am now working with an application that only understands ANSI syntax so I am struggling.

My first (old style syntax) SQL statement below produces 60 rows:

SELECT A1.CONTACTID, A1.LASTNAME, A1.FIRSTNAME, A1.ACCOUNT,
A6.CITY, A6.STATE, A1.WORKPHONE, A1.FAX, A1.EMAIL
FROM CONTACT A1,
ADDRESS A6
WHERE A1.ADDRESSID=A6.ADDRESSID
AND A1.CONTACTID IN
(SELECT A4.CONTACTID
FROM CONTACT_LEADSOURCE A4,
LEADSOURCE A5
WHERE A4.LEADSOURCEID = A5.LEADSOURCEID
AND A5.DESCRIPTION = 'some_description' )
AND A1.CONTACTID IN
(SELECT A2.CONTACTID
FROM TICKET A2,
ENROLLHX A3,
EVENT A7
WHERE A3.STATUS IN ('R', 'Confirmed')
AND A2.TICKETID = A3.EVXEVTICKETID
AND A3.EVENTID = A7.EVENTID
AND A7.CODE IN
('AHS00','AHS01','AHS02','AHS03','AHS04','AHS98',' AHS99'))
ORDER BY A1.LASTNAME ASC

I am trying to convert this to the newer ANSI sytax. My second SQL statement below produces 67 rows (duplicates):

SELECT A1.CONTACTID, A1.LASTNAME, A1.FIRSTNAME, A1.ACCOUNT,
A6.CITY, A6.STATE, A1.WORKPHONE, A1.FAX, A1.EMAIL
FROM CONTACT A1
JOIN ADDRESS A6 ON (A1.ADDRESSID=A6.ADDRESSID)
JOIN
( SELECT C.CONTACTID
FROM CONTACT C
JOIN CONTACT_LEADSOURCE A4 ON (C.CONTACTID = A4.CONTACTID)
JOIN LEADSOURCE A5 ON (A4.LEADSOURCEID =
A5.LEADSOURCEID
AND A5.DESCRIPTION =
'some_description' )) AS C1 ON C1.CONTACTID = A1.CONTACTID
JOIN
(SELECT C2.CONTACTID
FROM CONTACT C2
JOIN TICKET A2 ON (C2.CONTACTID =
A2.CONTACTID)
JOIN ENROLLHX A3 ON (A2.TICKETID =
A3.TICKETID AND A3.STATUS in ('R', 'Confirmed'))
JOIN EVENT A7 ON (A3.EVENTID = A7.EVENTID

AND A7.CODE IN ('AHS00','AHS01','AHS02','AHS03','AHS04','AHS98',' AHS99')))
AS C3 ON C3.CONTACTID = A1.CONTACTID
Can anyone shed some light on what I am missing?

cheers,
Norm


Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Mattias Nordstrom | last post by:
Hi. I have a query that looks like this: SELECT user_id, user_given_names, user_surname, user_cast FROM users WHERE user_id NOT IN (SELECT user_id FROM linkage WHERE group_id=1) ORDER BY...
4
by: Yossi Naggar | last post by:
Hello to everyone, I am an experienced user in MSSQL Server. Lately I have been started using MySQL. I managed to create my database and tables. When I wanted to execute the following query:...
3
by: Prem | last post by:
Hi, I am having many problems with inner join. my first problem is : 1) I want to know the precedance while evaluating query with multiple joins. eg. select Employees.FirstName,...
2
by: Sonal Jain | last post by:
i have a query which goes like this:- select a.col1,b.col2,c.col2 from tab1 a,tab2 b,tab3 c where a.col1 *= b.col1 and a.col2 *= c.col2 and b.col3 = c.col3 how do I write this query in SQL...
2
by: Preston Landers | last post by:
Hello all. I am trying to write a query that "just" switches some data around so it is shown in a slightly different format. I am already able to do what I want in Oracle 8i, but I am having...
3
by: **Developer** | last post by:
I need to paste an RTF non-ANSI string into a RichTextBox. I know how to do that if the string were all ANSI characters. But the text contains Unicode characters that have no ANSI equivalent. I...
2
by: gkellymail | last post by:
the following query works fine: select link.idx, link.x_table, link.x_id_a, link.x_id_z, a.strandid, b.strandid from link_detail, link, strand A, strand B where link_detail.x_table =...
10
by: marting | last post by:
Before I throw my new expensive laptop out of the window! I'm stuck on getting my joins right with the correct amount in a column count. After speaking with someone a few weeks back, they...
3
by: Leif Neland | last post by:
This is not supported in MSsql. update T1 set (theUpdatedValue, theOtherValue) = (select theTop, theValue from T2 where T2.theKey = T1.theID) Is there a workaround? Other than doing it in a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.