?
Solved

Complex SQL query

Posted on 2004-04-28
13
Medium Priority
?
359 Views
Last Modified: 2008-02-01
Hi,
I have 2 tables:

Clients
Clients_Addresses

I want run a select query with where clause that pulls up clients that DO and DO NOT have addresses associated with them in Clients_Addresses table. Here is what I have so far but it only pulls up clients that have addresses and I want to pull up the ones that don't as well:

select distinct (C.ClientID), COALESCE(CLN.ClientLName, '') + ', ' + COALESCE(CLN.ClientFName, '') + ' '
+ COALESCE(CLN.ClientMName, '') As ClientName, C.DOB, COALESCE(A.StNum, '')+' '+COALESCE(A.StName, '')
+','+COALESCE(A.UnitNum, '')+','+COALESCE(A.ExtraAddrLine, '')+','+COALESCE(A.City, '')+','
+COALESCE(CT.LongDesc, '')+','+COALESCE(A.Zip, '') As AddressCol
from CLIENT C, CLIENT_NAME CLN, ADDRESS A, ADDRESS_OWNER AO, CODE_TABLE CT, CDSS_CASE CDC
where((C.ClientID = CLN.ClientID)
and (C.ClientID = AO.OwnerID)
and AO.AddrType = 889
And AO.OwnerTypeCD = 880 and AO.AddrID = A.AddrId and A.StateCD *= CT.CodeID)
AND [CLN].[ClientLName] like 'bush'
order by C.ClientID

Thanks a lot
0
Comment
Question by:ebtech
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 4
13 Comments
 
LVL 1

Expert Comment

by:1markmc
ID: 10943942
Try something like this.  Note there is a join condition missing at the <<>>

select distinct
            (C.ClientID),       
            COALESCE(CLN.ClientLName, '') + ', ' + COALESCE(CLN.ClientFName, '') + ' '
                  + COALESCE(CLN.ClientMName, '')
                  As ClientName,
            C.DOB, COALESCE(A.StNum, '')+' '+COALESCE(A.StName,             '')
                  +','+COALESCE(A.UnitNum, '')+','+COALESCE(A.ExtraAddrLine, '')+','+COALESCE(A.City, '')+','
                  +COALESCE(CT.LongDesc, '')+','+COALESCE(A.Zip, '')
                  As AddressCol
      from
                  CLIENT C
            join
                  CLIENT_NAME CLN
            on
                  C.clientid = CLN.clientid
            left outer join
                  ADDRESS_OWNER AO
            on
                  AO.OwnerID = c.ClientID
            join
                  ADDRESS A
            on
                  A.AddrID = AO.AddrID                   
            join
                  CODE_TABLE CT
            on
                  A.StateCD = CT.CodeID
            join
                  CDSS_CASE CDC
            on
                  <<JOIN CONDITION MISSING>>
      where
            (

            AO.AddrType = 889
            And
            AO.OwnerTypeCD = 880 and A.StateCD *= CT.CodeID)
            AND
            [CLN].[ClientLName] like 'bush'
      order by
            C.ClientID
0
 

Author Comment

by:ebtech
ID: 10944000
What's the <<JOIN CONDITION MISSING>> supposed to be?
0
 
LVL 1

Expert Comment

by:1markmc
ID: 10944030
there should be a key field in cdss_case  that matches a  key field in one of the previously joined tables.  It would be something like

CDSS_CASE.CaseID = C.CaseID

or it could be

CDSS_CASE.ClientID = C.ClientID
0
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 

Author Comment

by:ebtech
ID: 10944128
Thanks.
I have actually removed the whole :
          join
               CDSS_CASE CDC
          on
               <<JOIN CONDITION MISSING>>
Since I am not even looking in that table. Once I run this query I get an error:

Server: Msg 1016, Level 15, State 3, Line 28
Outer join operators cannot be specified in a query containing joined tables.

Here it what I have so far:
select distinct
          (C.ClientID),      
          COALESCE(CLN.ClientLName, '') + ', ' + COALESCE(CLN.ClientFName, '') + ' '
               + COALESCE(CLN.ClientMName, '')
               As ClientName,
          C.DOB, COALESCE(A.StNum, '')+' '+COALESCE(A.StName,           '')
               +','+COALESCE(A.UnitNum, '')+','+COALESCE(A.ExtraAddrLine, '')+','+COALESCE(A.City, '')+','
               +COALESCE(CT.LongDesc, '')+','+COALESCE(A.Zip, '')
               As AddressCol
     from
               CLIENT C
          join
               CLIENT_NAME CLN
          on
               C.clientid = CLN.clientid
          left outer join
               ADDRESS_OWNER AO
          on
               AO.OwnerID = c.ClientID
          join
               ADDRESS A
          on
               A.AddrID = AO.AddrID                
          join
               CODE_TABLE CT
          on
               A.StateCD *= CT.CodeID
     where
          (AO.AddrType = 889 And AO.OwnerTypeCD = 880)
          AND [CLN].[ClientLName] like 'bush'
     order by
          C.ClientID
0
 
LVL 7

Expert Comment

by:ChrisFretwell
ID: 10944168
This is your original query written with the ansi join syntax, your solution follows. I could find no join for cdss_case (CDC ) and no columns so I removed it. (from BOL In earlier versions of Microsoft® SQL Server™ 2000, left and right outer join conditions were specified in the WHERE clause using the *= and =* operators. In some cases, this syntax results in an ambiguous query that can be interpreted in more than one way. SQL-92 compliant outer joins are specified in the FROM clause and do not result in this ambiguity)
You original with ansi join syntax
select distinct (C.ClientID), COALESCE(CLN.ClientLName, '') + ', ' + COALESCE(CLN.ClientFName, '') + ' '
+ COALESCE(CLN.ClientMName, '') As ClientName, C.DOB, COALESCE(A.StNum, '')+' '+COALESCE(A.StName, '')
+','+COALESCE(A.UnitNum, '')+','+COALESCE(A.ExtraAddrLine, '')+','+COALESCE(A.City, '')+','
+COALESCE(CT.LongDesc, '')+','+COALESCE(A.Zip, '') As AddressCol
from CLIENT C
join  CLIENT_NAME CLN on C.ClientID = CLN.ClientID
join ADDRESS_OWNER AO on C.ClientID = AO.OwnerID and AO.AddrType = 889 And AO.OwnerTypeCD = 880
join ADDRESS A on AO.AddrID = A.AddrId
left join CODE_TABLE CT on A.StateCD = CT.CodeID
where [CLN].[ClientLName] like 'bush'
order by C.ClientID

What I think you want:
select distinct (C.ClientID), COALESCE(CLN.ClientLName, '') + ', ' + COALESCE(CLN.ClientFName, '') + ' '
+ COALESCE(CLN.ClientMName, '') As ClientName, C.DOB, COALESCE(A.StNum, '')+' '+COALESCE(A.StName, '')
+','+COALESCE(A.UnitNum, '')+','+COALESCE(A.ExtraAddrLine, '')+','+COALESCE(A.City, '')+','
+COALESCE(CT.LongDesc, '')+','+COALESCE(A.Zip, '') As AddressCol
from CLIENT C
join CLIENT_NAME CLN on C.ClientID = CLN.ClientID
left join ADDRESS_OWNER AO on C.ClientID = AO.OwnerID and AO.AddrType = 889 And AO.OwnerTypeCD = 880
join ADDRESS A on AO.AddrID = A.AddrId
left join CODE_TABLE CT on A.StateCD = CT.CodeID
where [CLN].[ClientLName] like 'bush'
order by C.ClientID
0
 

Author Comment

by:ebtech
ID: 10944206
It still does not work, it does not bring back records that don't have addresses, only the ones that do.
0
 
LVL 7

Accepted Solution

by:
ChrisFretwell earned 500 total points
ID: 10944216
then this should, I wasnt sure if you had an address owner that you would have an address.

select distinct (C.ClientID), COALESCE(CLN.ClientLName, '') + ', ' + COALESCE(CLN.ClientFName, '') + ' '
+ COALESCE(CLN.ClientMName, '') As ClientName, C.DOB, COALESCE(A.StNum, '')+' '+COALESCE(A.StName, '')
+','+COALESCE(A.UnitNum, '')+','+COALESCE(A.ExtraAddrLine, '')+','+COALESCE(A.City, '')+','
+COALESCE(CT.LongDesc, '')+','+COALESCE(A.Zip, '') As AddressCol
from CLIENT C
join CLIENT_NAME CLN on C.ClientID = CLN.ClientID
left join ADDRESS_OWNER AO on C.ClientID = AO.OwnerID and AO.AddrType = 889 And AO.OwnerTypeCD = 880
left join ADDRESS A on AO.AddrID = A.AddrId
left join CODE_TABLE CT on A.StateCD = CT.CodeID
where [CLN].[ClientLName] like 'bush'
order by C.ClientID
0
 
LVL 1

Expert Comment

by:1markmc
ID: 10944218
select distinct
          (C.ClientID),      
          COALESCE(CLN.ClientLName, '') + ', ' + COALESCE(CLN.ClientFName, '') + ' '
               + COALESCE(CLN.ClientMName, '')
               As ClientName,
          C.DOB, COALESCE(A.StNum, '')+' '+COALESCE(A.StName,           '')
               +','+COALESCE(A.UnitNum, '')+','+COALESCE(A.ExtraAddrLine, '')+','+COALESCE(A.City, '')+','
               +COALESCE(CT.LongDesc, '')+','+COALESCE(A.Zip, '')
               As AddressCol
     from
               CLIENT C
          join
               CLIENT_NAME CLN
          on
               C.clientid = CLN.clientid
          left outer join
               ADDRESS_OWNER AO
          on
               AO.OwnerID = c.ClientID
          join
               ADDRESS A
          on
               A.AddrID = AO.AddrID                
          join
               CODE_TABLE CT
          on
               A.StateCD = CT.CodeID
     where
          (AO.AddrType = 889 And AO.OwnerTypeCD = 880)
          AND [CLN].[ClientLName] like 'bush'
     order by
          C.ClientID
0
 
LVL 1

Expert Comment

by:1markmc
ID: 10944226
Removed the * from the equality in the join condition for code_table
0
 
LVL 7

Expert Comment

by:ChrisFretwell
ID: 10944228
Mark, your code has one tiny flaw, you left join to address owner, but then run explicit where clauses on the values in the table, so it will only return them back for full matches. These ao. where's need to go up to the join for AO
0
 
LVL 7

Expert Comment

by:ChrisFretwell
ID: 10944244
and I think the * = was there becuase the join to code_table needs to be a left join
0
 

Author Comment

by:ebtech
ID: 10944247
Thanks a lot ChrisFretwell, it worked!!!
Great job.
0
 
LVL 7

Expert Comment

by:ChrisFretwell
ID: 10944251
Glad to help!
Do you understand what the change was? Does it make sense? I know that wasnt your purpose in asking, but learning is always good.
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question