title: Concatenating multiple CASE statements into one alias
body: <p>After some previous help on how to approach a problem I am having with some legacy code, it seems like the best approach for my issue is to concatenate case statements to return a value I can parse out in PHP.</p>
  
  <p>I am trying to do something like this, but it is returning many rows, and eventually getting this error:</p>
  
  <blockquote>
    <p>Maximum stored procedure, function, trigger, or view nesting level
    exceeded (limit 32).</p>
  </blockquote>
  
  <pre><code>SELECT org.org_id,
         org.org_name_1,
         Datename(YEAR, member.enroll_date) AS enroll_year,
         Max(CASE
               WHEN board.member_from IS NULL THEN 0
               ELSE 1
             END)                           AS board_member,
         CASE
           WHEN ( org.delete_reason = 'OUT'
                  AND org.org_delete_flag = 'Y'
                  AND org.org_status_flag = 'C' ) THEN 'out_of_business|'
           ELSE ''
         END + CASE
                 WHEN ( stat.carrier = 'BS'
                        AND stat.status_id IS NOT NULL
                        AND stat.termination_date IS NULL
                        AND stat.flat_dues &gt; 0 ) THEN 'insurance_member|'
                 ELSE ''
               END + CASE
                       WHEN ( stat.carrier = 'BS'
                              AND stat.status_id IS NOT NULL
                              AND stat.termination_date IS NULL
                              AND stat.flat_dues = 0
                              AND member.status_flag IN( 'C', 'P' ) ) THEN 'insurance_product|'
                       ELSE ''
                     END + CASE
                             WHEN ( member.enroll_date IS NOT NULL
                                    AND member.status_flag NOT IN( 'C', 'P' ) ) THEN 'member_since|'
                             ELSE ''
                           END + CASE
                                   WHEN ( org.org_relationship_parent = 'Y'
                                          AND org.dues_category = 'MBR'
                                          AND org.org_status_flag = 'R' ) THEN 'subsidiary_member|'
                                   ELSE ''
                                 END + CASE
                                         WHEN ( org.org_misc_data_9 = 'PAC' ) THEN 'pac|'
                                         ELSE ''
                                       END + CASE
                                               WHEN ( org.dues_category = 'PART' ) THEN 'partner_member|'
                                               ELSE ''
                                             END + CASE
                                                     WHEN ( org.dues_category = 'FREE'
                                                            AND org.org_status_flag = 'P' ) THEN 'associate_member|'
                                                     ELSE ''
                                                   END
                                            --ELSE 'non_member'
                                            --END 
                                            AS org_status,
         60                                 AS expires_in,
         CASE
           WHEN stat.dues_type = 'M' THEN
             CASE
               WHEN ( stat.termination_date IS NULL ) THEN ( stat.flat_dues )
               ELSE 0
             END
           ELSE
             CASE
               WHEN ( member.payments = 0 ) THEN member.dues_billed_annual
               ELSE member.payments
             END
         END                                AS dues_level,
         CASE
           WHEN ( org.affiliate_code = 'PCCE'
                  AND org.dues_category = 'MBR'
                  AND org.org_status_flag = 'R' ) THEN 1
           ELSE 0
         END                                AS pcce_membr,
         --          '$'+CONVERT(VARCHAR,@dues) AS dues_level,
         Ltrim(@product_level)              AS product_level,
         Ltrim(@involve_level)              AS involvement_level
  FROM   organiz AS org
         LEFT JOIN affilbil AS member
           ON member.status_id = org.org_id
              AND member.dues_category = 'MBR'
         LEFT JOIN individu AS ind
           ON ind.org_id = org.org_id
         LEFT JOIN commembr AS board
           ON board.status_id = ind.ind_id
              AND board.committee_code = '5'
              AND board.member_to IS NULL
         LEFT JOIN statinsmorn AS stat
           ON stat.status_id = org.org_id
              AND stat.carrier = 'BS'
              AND stat.planz = 'PCI'
  WHERE  org.org_id = @org_id
  GROUP  BY org.org_id,
            org.org_name_1,
            member.enroll_date,
            org.delete_reason,
            org.org_status_flag,
            org.org_delete_flag,
            stat.status_id,
            stat.flat_dues,
            stat.dues_type,
            stat.termination_date,
            org.org_misc_data_9,
            org_relationship_parent,
            org.dues_category,
            member.status_flag,
            member.dues_billed_annual,
            member.payments,
            stat.carrier,
            org.Affiliate_Code 
  </code></pre>