Complex SQL query

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
ebtechAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
ChrisFretwellConnect With a Mentor Commented:
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
 
1markmcCommented:
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
 
ebtechAuthor Commented:
What's the <<JOIN CONDITION MISSING>> supposed to be?
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
1markmcCommented:
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
 
ebtechAuthor Commented:
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
 
ChrisFretwellCommented:
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
 
ebtechAuthor Commented:
It still does not work, it does not bring back records that don't have addresses, only the ones that do.
0
 
1markmcCommented:
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
 
1markmcCommented:
Removed the * from the equality in the join condition for code_table
0
 
ChrisFretwellCommented:
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
 
ChrisFretwellCommented:
and I think the * = was there becuase the join to code_table needs to be a left join
0
 
ebtechAuthor Commented:
Thanks a lot ChrisFretwell, it worked!!!
Great job.
0
 
ChrisFretwellCommented:
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
All Courses

From novice to tech pro — start learning today.