sql在一个交叉应用/案例表达式中设置多个值

时间:2018-06-10 23:35:23

标签: sql sql-server case cross-apply

我试图清理下面的查询。在第一次交叉申请中,我评估[Store Number][Branch Number]并返回[Store Name],然后在以下三个交叉申请中,我使用此[Store Name]分别返回值[Phone Number][Contact Name][Contact Title]。有没有办法只在一个额外的交叉应用/案例中设置和返回所有这三个值?

SELECT

       C.[Store Number]
       ,B.[Store Name]
       ,PH.[Phone Number]
      ,CN.[Contact Name]
      ,CT.[Contact Title]
  FROM [Central_Account] C 

      cross apply (select[Store Name] = case C.[Store Number]
                                 when 9146 then
                                    case
                                          when C.[Branch Number] In (14, 16, 18)
                                          then 'Community Store'
                                    end  
                                 when 8147 Then 
                                    case
                                          when C.[Branch Number] In (24, 26, 28)
                                          then 'City Store'
                                    end
                                 when 7148 Then 
                                   case
                                          when C.[Branch Number] In (34, 36, 38)
                                          then 'County Store'
                                   end
                                 else
                                    'State Store'
                           end )as B
        cross apply (select [Phone Number] = case B.[Store Name]
                       when 'Community Store' Then
                                      '414.882.8278'       
                                 when 'City Store' Then
                                      '221.332.6221'
                                 when 'County Store' Then
                                    '211.949.2008'
                                 else
                                    '635.588.1878'
                           end ) as PH                        
      cross apply (select [Contact Name] = case B.[Store Name]
                                when 'Community Store' Then
                                      'John A. Smith'      
                                 when 'City Store' Then
                                      'Marcus D. Jones'
                                 when 'County Store' Then
                                      'Mica L. Johnson'
                                 else
                                      'Elroy Watkins, Jr.'
                           end ) as CN   
        cross apply (select [Contact Title] = case B.[Store Name]
                                when 'Community Store' Then
                                      'Executive Vice President'      
                                 when 'City Store' Then
                                      'Manager'
                                 when 'County Store' Then
                                      'President of Operations'
                                 else
                                      'Clerk'
                           end ) as CT   

1 个答案:

答案 0 :(得分:0)

我倾向于将此视为join

select C.[Store Number], sn.Store_Name, 
       v.phone_number, v.contact_name, v.contact_title
from Central_Account C cross apply
     (values (case when C.[Store Number] = 9146 and C.[Branch Number] In (14, 16, 18)
                   then 'Community Store'
                   when C.[Store Number] = 8147 and C.[Branch Number] In (24, 26, 28)
                   then 'City Store'
                   when C.[Store Number] = 7148 and C.[Branch Number] In (34, 36, 38)
                   then 'County Store'
                   when C.[Store Number] not in (9146, 8147, 7148)
                   then 'State Store'
              end)
     ) sn(store_name) join
     (values ('Community Store', '414.882.8278', 'John A. Smith', 'Executive Vice President'), 
             ('City Store', '221.332.6221', 'Marcus D. Jones',  'Manager'),
             ('County Store', '211.949.2008', 'Mica L. Johnson', 'President of Operations'),
             ('State Store', '635.588.1878', 'Elroy Watkins, Jr.', 'Clerk')
     ) v(store_name, phone_number, contact_name, contact_title)
     on sn.store_name = v.store_name or
        (sn.store_name is null and v.store_name = 'State Store';  

第一个cross apply获取商店名称。第二个values然后会提供有关商店的所有相关信息。默认情况下有一个小技巧 - 因为您已设置逻辑,因此某些商店名称为NULL。这只是默认为州商店。