Solved

Complex SQL query

Posted on 2004-04-28
13
353 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 125 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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how the fundamental information of how to create a table.

730 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