Welcome to Dream.In.Code
Click Here
Getting Help is Easy!

Join 117,527 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,982 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



SQL 2005 Conditional Select Question

 
Reply to this topicStart new topic

SQL 2005 Conditional Select Question, Multiple values to one Row

Mr_Mom
post 19 Jun, 2008 - 10:18 AM
Post #1


New D.I.C Head

*
Joined: 21 May, 2008
Posts: 28


My Contributions


Hello,

I am trying to modify a VS 2005 Report to include all Phone Numbers for a client. Currently it only includes Home. The below is the SQL in the Stored procedure for the Dataset used. After much trial and error I got ,what I thought, ws close with the below except that with Top 1 on (which was put there by the original developer) I get the Home Number but I get NULL for the expected Cell Number. If I remove the Top 1 I get two rows returned that are Identical except that in one Home Number is filled and Cell Number is NULL and the other is the opposite.

I am trying to figure out how to get all of the phone numbers for one client (PersonID) returned but maintain one row.

Please let me know how I can help you help me and thank you for any guidance!


CODE

    SELECT TOP 1 CMO.dbo.tblAdminMaintItem.ItemDescription as TargetGroupDesc,
        dbo.tblPlan.*,
        Connect.dbo.tblPerson.*,
        Connect.dbo.tblAddress.*,
        (CASE WHEN Connect.dbo.tblPhone.PhoneTypeID = 'H' THEN Connect.dbo.tblPhone.PhoneNumber ELSE NULL END) AS 'Home Phone Number',
        (CASE WHEN Connect.dbo.tblPhone.PhoneTypeID = 'B' THEN Connect.dbo.tblPhone.PhoneNumber ELSE NULL END) AS 'Business Phone Number',
        (CASE WHEN Connect.dbo.tblPhone.PhoneTypeID = 'C' THEN Connect.dbo.tblPhone.PhoneNumber ELSE NULL END) AS 'Cell Phone Number',
        (CASE WHEN Connect.dbo.tblPhone.PhoneTypeID = 'O' THEN Connect.dbo.tblPhone.PhoneNumber ELSE NULL END) AS 'Other Phone Number',
        CMO.dbo.tblCMOClientInfo.MentalHealth,
        tblDemographics.FirstName + ' ' + tblDemographics.LastName as SWName,
        tblDemographics.Phone as SWPhone,
        tblDemographics_1.FirstName + ' ' + tblDemographics_1.LastName as NurseName,
        tblDemographics_1.Phone as NursePhone,
        tblSupportWorker.FirstName + ' ' + tblSupportWorker.LastName as ESWName,
        tblSupportWorker.Phone as ESWPhone,
        Connect.dbo.tblPersonInsurance.MedicaidNumber,
        Connect.dbo.tblPersonInsurance.MedicareNumber
    FROM  dbo.tblPlan LEFT OUTER JOIN
                   Connect.dbo.tblPerson ON dbo.tblPlan.ClientID = Connect.dbo.tblPerson.PersonID LEFT OUTER JOIN
                Connect.dbo.tblAddress ON Connect.dbo.tblPerson.PersonID = Connect.dbo.tblAddress.PersonID and Connect.dbo.tblAddress.AddressTypeID = 'M' LEFT OUTER JOIN
                Connect.dbo.tblPhone ON Connect.dbo.tblPerson.PersonID = Connect.dbo.tblPhone.PersonID LEFT OUTER JOIN
                CMO.dbo.tblCMOEnrollment ON dbo.tblPlan.EnrollmentID = CMO.dbo.tblCMOEnrollment.EnrollmentID LEFT OUTER JOIN
                CMO.dbo.tblCMOClientInfo ON CMO.dbo.tblCMOEnrollment.ClientID = CMO.dbo.tblCMOClientInfo.ClientID LEFT OUTER JOIN
                CMO.dbo.tblAdminMaintItem ON CMO.dbo.tblCMOClientInfo.TargetGroupID = CMO.dbo.tblAdminMaintItem.ItemID LEFT OUTER JOIN
                aspnetdb.dbo.tblDemographics ON CMO.dbo.tblCMOEnrollment.CMOSocialWorkerID = aspnetdb.dbo.tblDemographics.UserID LEFT OUTER JOIN
                aspnetdb.dbo.tblDemographics AS tblDemographics_1 ON CMO.dbo.tblCMOEnrollment.CMONurseID = tblDemographics_1.UserID LEFT OUTER JOIN
                CMO.dbo.tblSupportWorker ON CMO.dbo.tblCMOEnrollment.EconomicSupportWorkerID = CMO.dbo.tblSupportWorker.SupportWorkerID LEFT OUTER JOIN
                Connect.dbo.tblPersonInsurance ON dbo.tblPlan.ClientID = Connect.dbo.tblPersonInsurance.PersonID
    WHERE (dbo.tblPlan.PlanID = 658)
User is offlineProfile CardPM

Go to the top of the page


AdaHacker
post 19 Jun, 2008 - 10:58 AM
Post #2


D.I.C Head

**
Joined: 17 Jun, 2008
Posts: 112



Thanked 15 times
My Contributions


If I'm understanding your query, your tblPhone table is storing each number in its own row with a type code. If you're just doing an outer join on that table, there's no way to get the results all in a single row. Instead, you're going to get one row per phone number.

The obvious way to get them all in one row is subqueries. Instead of the 4 CASE statements, you would use 4 scalar subqueries with the appropriate code in the WHERE clause. The code would look something like this:
SQL
SELECT dbo.tblPlan.*, Connect.dbo.tblPerson.*, Connect.dbo.tblAddress.*,
(SELECT PhoneNumber FROM tblPhone WHERE PhoneTypeId = 'H' AND tblPhone.PersonID = tblPerson.PersonID) AS 'Home Phone Number',
(SELECT PhoneNumber FROM tblPhone WHERE PhoneTypeId = 'B' AND tblPhone.PersonID = tblPerson.PersonID) AS 'Business Phone Number',
... -- Same idea for other 2 numbers

You could then eliminate tblPhone from the outer join, assuming that you aren't using it for anything else.
User is online!Profile CardPM

Go to the top of the page

Mr_Mom
post 19 Jun, 2008 - 11:10 AM
Post #3


New D.I.C Head

*
Joined: 21 May, 2008
Posts: 28


My Contributions


Thanks! biggrin.gif

Not sure why I didn't think of that so thanks for the help. I always seem to overcomplicate things. tongue.gif

CODE

    SELECT TOP 1 CMO.dbo.tblAdminMaintItem.ItemDescription as TargetGroupDesc,
        dbo.tblPlan.*, Connect.dbo.tblPerson.*, Connect.dbo.tblAddress.*,
        (SELECT Connect.dbo.tblPhone.PhoneNumber FROM Connect.dbo.tblPhone WHERE Connect.dbo.tblPhone.PhoneTypeId = 'H' AND Connect.dbo.tblPhone.PersonID = Connect.dbo.tblPerson.PersonID) AS 'Home Phone Number',
        (SELECT Connect.dbo.tblPhone.PhoneNumber FROM Connect.dbo.tblPhone WHERE Connect.dbo.tblPhone.PhoneTypeId = 'B' AND Connect.dbo.tblPhone.PersonID = Connect.dbo.tblPerson.PersonID) AS 'Business Phone Number',
        (SELECT Connect.dbo.tblPhone.PhoneNumber FROM Connect.dbo.tblPhone WHERE Connect.dbo.tblPhone.PhoneTypeId = 'C' AND Connect.dbo.tblPhone.PersonID = Connect.dbo.tblPerson.PersonID) AS 'Cell Phone Number',
        (SELECT Connect.dbo.tblPhone.PhoneNumber FROM Connect.dbo.tblPhone WHERE Connect.dbo.tblPhone.PhoneTypeId = 'O' AND Connect.dbo.tblPhone.PersonID = Connect.dbo.tblPerson.PersonID) AS 'Other Phone Number',
        CMO.dbo.tblCMOClientInfo.MentalHealth,
        tblDemographics.FirstName + ' ' + tblDemographics.LastName as SWName,
        tblDemographics.Phone as SWPhone,
        tblDemographics_1.FirstName + ' ' + tblDemographics_1.LastName as NurseName,
        tblDemographics_1.Phone as NursePhone,
        tblSupportWorker.FirstName + ' ' + tblSupportWorker.LastName as ESWName,
        tblSupportWorker.Phone as ESWPhone,
        Connect.dbo.tblPersonInsurance.MedicaidNumber,
        Connect.dbo.tblPersonInsurance.MedicareNumber
    FROM  dbo.tblPlan LEFT OUTER JOIN
                   Connect.dbo.tblPerson ON dbo.tblPlan.ClientID = Connect.dbo.tblPerson.PersonID LEFT OUTER JOIN
                Connect.dbo.tblAddress ON Connect.dbo.tblPerson.PersonID = Connect.dbo.tblAddress.PersonID and Connect.dbo.tblAddress.AddressTypeID = 'M' LEFT OUTER JOIN
                Connect.dbo.tblPhone ON Connect.dbo.tblPerson.PersonID = Connect.dbo.tblPhone.PersonID LEFT OUTER JOIN
                CMO.dbo.tblCMOEnrollment ON dbo.tblPlan.EnrollmentID = CMO.dbo.tblCMOEnrollment.EnrollmentID LEFT OUTER JOIN
                CMO.dbo.tblCMOClientInfo ON CMO.dbo.tblCMOEnrollment.ClientID = CMO.dbo.tblCMOClientInfo.ClientID LEFT OUTER JOIN
                CMO.dbo.tblAdminMaintItem ON CMO.dbo.tblCMOClientInfo.TargetGroupID = CMO.dbo.tblAdminMaintItem.ItemID LEFT OUTER JOIN
                aspnetdb.dbo.tblDemographics ON CMO.dbo.tblCMOEnrollment.CMOSocialWorkerID = aspnetdb.dbo.tblDemographics.UserID LEFT OUTER JOIN
                aspnetdb.dbo.tblDemographics AS tblDemographics_1 ON CMO.dbo.tblCMOEnrollment.CMONurseID = tblDemographics_1.UserID LEFT OUTER JOIN
                CMO.dbo.tblSupportWorker ON CMO.dbo.tblCMOEnrollment.EconomicSupportWorkerID = CMO.dbo.tblSupportWorker.SupportWorkerID LEFT OUTER JOIN
                Connect.dbo.tblPersonInsurance ON dbo.tblPlan.ClientID = Connect.dbo.tblPersonInsurance.PersonID
    WHERE (dbo.tblPlan.PlanID = 658)

User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 10/7/08 03:46PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month