Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 conditional statement needed?

Author  Topic 

tacec
Starting Member

8 Posts

Posted - 2008-05-28 : 12:43:40
Hopefully someone can make some sense of this. I'm not even sure how I would search for help on this in existing threads....

I'm attempting to pull a distinct list of clients including their address and certification status. Each client could possibly have multiple addresses listed under many different Address Types. I only want to see the client's Home address (CH), unless they don't have a home address listed, therefore i want to see the Alternate address (CA). If they have both listed, i only want to see the CH address.

This is what i have so far:

SELECT Distinct c.ClientCode, c.ClientId, c.dateofbirth, c.FirstName, c.LastName,
ISNULL(a.AddrType,ca.AddrType) AddrType,
ISNULL(a.StreetNo,ca.StreetNo) StreetNo,
ISNULL(a.OnStreet,ca.OnStreet) OnStreet,
ISNULL(a.Unit,ca.Unit) Unit,
ISNULL(a.City,ca.City) City,
ISNULL(a.Phone,ca.Phone) Phone,
ISNULL(cl.Ldate, 19990101) LDate
FROM dbo.AS_Clients AS c
LEFT JOIN dbo.CertificationLog AS cl ON cl.ClientId = c.ClientId and cl.LDescription = @description
LEFT JOIN dbo.AS_ClientFundingSource AS cfs ON cfs.ClientId = c.ClientId
LEFT JOIN dbo.AS_Address AS a ON a.AddrId = c.ClientId a.AddrType = 'CH'
LEFT JOIN dbo.AS_Address AS ca ON a.AddrId = c.ClientId a.AddrType = 'CA'
WHERE cfs.FundingSourceId = 11


The certificationlog table is just filtering out a specific record (@description) and the fundingsourceid is to specify clients with a specific fundingsource.

The problem with this is, if the client actually has just a CA address, instead of showing that as their address, it shows as NULL.

Anyone have any ideas on this? Any help would be greatly appreciated!
Thanks!

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-05-28 : 13:18:26
May be this:-

SELECT Distinct c.ClientCode, c.ClientId, c.dateofbirth, c.FirstName, c.LastName,
ISNULL(a.CHAddrType,a.CAAddrType) AddrType,
ISNULL(a.CHStreetNo,a.CAStreetNo) StreetNo,
ISNULL(a.CHOnStreet,a.CAOnStreet) OnStreet,
ISNULL(a.CHUnit,a.CAUnit) Unit,
ISNULL(a.CHCity,a.CACity) City,
ISNULL(a.CHPhone,a.CAPhone) Phone,
ISNULL(cl.Ldate, 19990101) LDate
FROM dbo.AS_Clients AS c
LEFT JOIN dbo.CertificationLog AS cl ON cl.ClientId = c.ClientId and cl.LDescription = @description
LEFT JOIN dbo.AS_ClientFundingSource AS cfs ON cfs.ClientId = c.ClientId
LEFT JOIN
(SELECT AddrId,
MAX(CASE WHEN AddrType = 'CH' THEN AddrType ELSE NULL END) AS CHAddrType,
MAX(CASE WHEN AddrType = 'CH' THEN StreetNo ELSE NULL END) AS CHStreetNo,
MAX(CASE WHEN AddrType = 'CH' THEN OnStreet ELSE NULL END) AS CHOnStreet,
MAX(CASE WHEN AddrType = 'CH' THEN Unit ELSE NULL END) AS CHUnit,
MAX(CASE WHEN AddrType = 'CH' THEN City ELSE NULL END) AS CHCity,
MAX(CASE WHEN AddrType = 'CH' THEN Phone ELSE NULL END) AS CHPhone,
MAX(CASE WHEN AddrType = 'CA' THEN AddrType ELSE NULL END) AS CAAddrType,
MAX(CASE WHEN AddrType = 'CA' THEN StreetNo ELSE NULL END) AS CAStreetNo,
MAX(CASE WHEN AddrType = 'CA' THEN OnStreet ELSE NULL END) AS CAOnStreet,
MAX(CASE WHEN AddrType = 'CA' THEN Unit ELSE NULL END) AS CAUnit,
MAX(CASE WHEN AddrType = 'CA' THEN City ELSE NULL END) AS CACity,
MAX(CASE WHEN AddrType = 'CA' THEN Phone ELSE NULL END) AS CAPhone,
FROM dbo.AS_Address
GROUP BY AddrId)
AS a ON a.AddrId = c.ClientId
WHERE (cfs.FundingSourceId = 11 OR cfs.FundingSourceId IS NULL)
Go to Top of Page

tacec
Starting Member

8 Posts

Posted - 2008-05-28 : 16:00:42
That worked great!

Thank you so much!!!
Go to Top of Page
   

- Advertisement -