如何在单个列中合并2个SQL CASE语句以进行输出

时间:2015-03-06 06:58:28

标签: sql-server merge case

我正在使用SQL Server 2014,我有一个查询,我需要在一个名为'Market Final'的列中合并2个CASE语句输出。

案例陈述1如下:

(CASE e.TravelAgencyTypeCode WHEN 'DMC' THEN g2.CountryGroup ELSE g.CountryGroup END) AS 'Market Final'

当我使用上面的CASE语句运行我的查询时,它运行正常,我得到的正是我想要的。当我需要添加第二个CASE语句时,它对我来说有点复杂,这也影响了“市场最终”列。

CASE声明2如下:

(CASE c.TAProfileID WHEN c.TAProfileID = '316' and c.CurrencyCode = 'MUR' THEN g.CountryGroup = 'DB Local' ELSE g.CountryGroup = 'DB International' END)

我已经读过这可以使用UNION ALL语句完成,但我无法弄清楚如何处理它。我也有一种感觉,我的CASE声明2写错了。

感谢您的帮助!

编辑:这是我的完整SQL查询,以便您全面了解:

USE  MyDatabase

SELECT 

  b.PropertyCode,
  c.PMSConfirmationNumber,   
  a.FirstName + ' ' + a.LastName AS 'Name',
  b.ReservationStatus AS 'Status',
  d.rsl_nationality AS 'Nationality',
  d.rsl_rateplan AS 'Rate Plan Code',
  d.rsl_roomtype AS 'Room Type',
  i.RoomType2 AS 'Room Type 2', 
  b.GuestCount AS 'Total Guest', 
  c.CurrencyCode,
  e.TravelAgencyTypeCode AS 'Source of Business',
  c.TAProfileID,
  e.Name AS 'Tour Operator', 
  g.CountryGroup AS 'Market', 
  c.TAProfileID2, 
  e2.Name AS 'Booking Origin (1)', 
  g2.CountryGroup AS 'Booking Origin (2)',

  (CASE e.TravelAgencyTypeCode WHEN 'DMC' THEN g2.CountryGroup ELSE  g.CountryGroup END) AS 'Market Final'

 FROM GuestNameInfo a
 JOIN GuestStaySummary b ON a.ReservationStayID = b.ReservationStayID
 LEFT JOIN ReservationStay c ON c.ReservationStayID = b.ReservationStayID
 LEFT JOIN P5RESERVATIONLIST d ON d.rsl_code = b.ReservationStayID 
 LEFT JOIN TravelAgency e ON e.TravelAgencyID = c.TAProfileID 
 LEFT JOIN Market g ON e.CountryCode = g.CountryCode 
 LEFT JOIN TravelAgency e2 ON e2.TravelAgencyID = c.TAProfileID2
 LEFT JOIN Market g2 ON e2.CountryCode = g2.CountryCode 
 LEFT JOIN Exrate h ON h.Ccode = c.CurrencyCode
 LEFT JOIN RoomCat i ON b.PropertyCode = i.Property AND d.rsl_roomtype = i.RoomType 

所以现在我需要将CASE 2 Statement 2添加到此查询中,以便更改反映在“Market Final”列中。

这是我追求的输出:

PropertyCode....CurrencyCode SourceofBusiness TAProfileID.... Market .... Booking Origin (2)  MarketFinal
    A            MUR          DMC                 50           Other Mkt      Scandinavia       Scandinavia
    A            USD          TO                  75             UK             UK                UK
    B            USD          DIR                 316             DB             USA               DB International
    B            MUR          DIR                 316            DB             MTS               DB Local

2 个答案:

答案 0 :(得分:1)

你可以通过类似的东西来做到这一点

CASE 
WHEN [condition1] THEN [Expression] 
WHEN [condition2] THEN [Expression]
WHEN [condition3] THEN [Expression]
WHEN [condition4] THEN [Expression]
ELSE [Expression]
END

所以在你的情况下会是这样的

(CASE WHEN e.TravelAgencyTypeCode = 'DMC' 
 THEN g2.CountryGroup 
 WHEN e.TravelAgencyTypeCode != 'DMC'
 THEN g.CountryGroup
 WHEN c.TAProfileID = '316' AND c.CurrencyCode = 'MUR'
 THEN 'DB Local' ELSE 'DB International') AS 'Market Final'

答案 1 :(得分:1)

根据您提供的示例和其他信息,您似乎需要以下逻辑:

  • 如果e.TravelAgencyTypeCode = 'DMC',则返回g2.CountryGroup

  • 否则:

    • 如果c.TAProfileID = '316',则:

      • 如果c.CurrencyCode = 'MUR',则返回'DB Local'

      • 否则返回'DB International'

    • 否则返回g.CountryGroup

使用简单的CASE语法,您可以使用嵌套的CASE或多或少地实现此逻辑:

CASE e.TravelAgencyTypeCode
  WHEN 'DMC' THEN g2.CountryGroup
  ELSE
    CASE c.TAProfileID
      WHEN '316' THEN
        CASE c.CurrencyCode
          WHEN 'MUR' THEN 'DB Local'
          ELSE 'DB International'
        END
      ELSE g.CountryGroup
    END
END

或搜索到的CASE语法:

CASE
  WHEN e.TravelAgencyTypeCode = 'DMC' THEN g2.CountryGroup
  ELSE
    CASE
      WHEN c.TAProfileID = '316' THEN
        CASE
          WHEN c.CurrencyCode = 'MUR' THEN 'DB Local'
          ELSE 'DB International'
        END
      ELSE g.CountryGroup
    END
END

但是,嵌套的CASE可能不太可读 - 特别是当您没有多个子类时,例如在这种情况下。所以,如果我是你,我实际上可能会以这种方式压扁上述结构:

CASE
  WHEN e.TravelAgencyTypeCode = 'DMC' THEN g2.CountryGroup
  WHEN c.TAProfileID = '316' AND c.CurrencyCode = 'MUR' THEN 'DB Local'
  WHEN c.TAProfileID = '316' THEN 'DB International'
  ELSE g.CountryGroup
END

第三个WHEN也可以写成

  WHEN c.TAProfileID = '316' AND c.CurrencyCode <> 'MUR' THEN 'DB International'

AND c.CurrencyCode <> 'MUR'是不必要的:在评估的那一点c.CurrencyCode无论如何都不会等于'MUR'。这是因为CASE的评估在找到匹配条件后立即停止,如果在达到裸c.TAProfileID = '316'时未找到匹配,则表示前一条件失败,因此c.CurrencyCode肯定是其他