473,802 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_LEADSOU RCE A4,
LEADSOURCE A5
WHERE A4.LEADSOURCEID = A5.LEADSOURCEID
AND A5.DESCRIPTION = 'some_descripti on' )
AND A1.CONTACTID IN
(SELECT A2.CONTACTID
FROM TICKET A2,
ENROLLHX A3,
EVENT A7
WHERE A3.STATUS IN ('R', 'Confirmed')
AND A2.TICKETID = A3.EVXEVTICKETI D
AND A3.EVENTID = A7.EVENTID
AND A7.CODE IN
('AHS00','AHS01 ','AHS02','AHS0 3','AHS04','AHS 98','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=A 6.ADDRESSID)
JOIN
( SELECT C.CONTACTID
FROM CONTACT C
JOIN CONTACT_LEADSOU RCE A4 ON (C.CONTACTID
= A4.CONTACTID)
JOIN LEADSOURCE A5 ON (A4.LEADSOURCEI D =
A5.LEADSOURCEID
AND A5.DESCRIPTION =
'some_descripti on' )) 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','AHS0 3','AHS04','AHS 98','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 2222
"ColdCanuck " <co**@canuck.ca > wrote in message
news:2lZGc.2076 $iw3.1593@clgrp s13...
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=A 2.ADDRESSID)
LEFT JOIN CONTACT_LEADSOU RCE A3 ON
(A1.CONTACTID=A 3.CONTACTID)
JOIN TICKET A5 ON (A3.CONTACTID=A 5.CONTACTID)
JOIN ENROLLHX A6 ON (A5.TICKETID=A6 .TICKETID)
JOIN EVENT A7 ON (A6.EVENTID=A7. EVENTID)
LEFT JOIN LEADSOURCE A4 ON
(A3.LEADSOURCEI D=A4.LEADSOURCE ID)
WHERE A4.DESCRIPTION = 'some_descripti on'
AND A7.CODE IN
('AHS00','AHS01 ','AHS02','AHS0 3','AHS04','AHS 98','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=A 6.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****@sommarsk og.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_LEADSOU RCE A4
JOIN LEADSOURCE A5 ON A4.LEADSOURCEID = A5.LEADSOURCEID
WHERE A5.DESCRIPTION = 'some_descripti on' )
AND A1.CONTACTID IN
(SELECT A2.CONTACTID
FROM TICKET A2
JOIN ENROLLHX A3 ON A2.TICKETID = A3.EVXEVTICKETI D
JOIN EVENT A7 ON A3.EVENTID = A7.EVENTID
WHERE A3.STATUS IN ('R', 'Confirmed')
AND A7.CODE IN
('AHS00','AHS01 ','AHS02','AHS0 3','AHS04','AHS 98','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_LEADSOU RCE A4 ON A1.CONTACTID = A4.CONTACTID
JOIN LEADSOURCE A5 ON A4.LEADSOURCEID = A5.LEADSOURCEID AND
A5.DESCRIPTION = 'some_descripti on'
JOIN TICKET A2 ON A1.CONTACTID ON A2.CONTACTID
JOIN ENROLLHX A3 ON A2.TICKETID = A3.EVXEVTICKETI D AND A3.STATUS IN ('R',
'Confirmed')
JOIN EVENT A7 ON A3.EVENTID = A7.EVENTID AND A7.CODE IN
('AHS00','AHS01 ','AHS02','AHS0 3','AHS04','AHS 98','AHS99')
ORDER BY A1.LASTNAME ASC

John

"ColdCanuck " <co**@canuck.ca > wrote in message
news:2lZGc.2076 $iw3.1593@clgrp s13...
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_LEADSOU RCE A4,
LEADSOURCE A5
WHERE A4.LEADSOURCEID = A5.LEADSOURCEID
AND A5.DESCRIPTION = 'some_descripti on' )
AND A1.CONTACTID IN
(SELECT A2.CONTACTID
FROM TICKET A2,
ENROLLHX A3,
EVENT A7
WHERE A3.STATUS IN ('R', 'Confirmed')
AND A2.TICKETID = A3.EVXEVTICKETI D
AND A3.EVENTID = A7.EVENTID
AND A7.CODE IN
('AHS00','AHS01 ','AHS02','AHS0 3','AHS04','AHS 98','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=A 6.ADDRESSID)
JOIN
( SELECT C.CONTACTID
FROM CONTACT C
JOIN CONTACT_LEADSOU RCE A4 ON (C.CONTACTID = A4.CONTACTID)
JOIN LEADSOURCE A5 ON (A4.LEADSOURCEI D =
A5.LEADSOURCEID
AND A5.DESCRIPTION =
'some_descripti on' )) 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','AHS0 3','AHS04','AHS 98','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
2465
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 users.user_surname What this does is return the users who are not in group 1. linkage contains user_id, group_id pairs of users who are in what groups. What I
4
2182
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: delete from adminpages where parentid IN ( select DISTINCT A.id from adminpages AS A where A.name='galeries' );
3
6419
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, Employees.LastName, TerritoryID, Employees.EmployeeID, RegionID, ProductID from Employees
2
2382
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 92?
2
2255
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 trouble making it work in SQL Server 2000. I am not a database newbie, but I can't seem to figure this one out so I am turning to the newsgroup. I am thinking that some of the SQL Gurus out there have done this very thing a thousand times before...
3
3136
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 don't know how to include them. Can you help?
2
1627
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 = 'enclosure' and link_detail.x_id = 3 and link.idx = link_detail.linkidx and A.strandid = link.x_id_a and B.strandid = link.x_id_z would someone please convert this to a more efficient query using inner
10
2257
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 suggested I redesigned my database to use more tables of info. Before I had 1 table for image information, now I have 9 and it's getting very, very confusing... especially as I don't know much about joins...! I am using ASP, MySQL, IIS and VBScript. ...
3
17977
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 loop from eg asp, over either fields (one statement per field), or over records (a query with a loop which for each row does a select from one table, update other table with the selected values.)
0
9562
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10309
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10289
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10068
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9119
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6840
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5496
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2968
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.