Solved

Complex SQL query

Posted on 2004-04-28
13
341 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
  • 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
 

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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now