以下是我尝试使用case语句
的select查询的一部分CASE SoftripNetTest.dbo.ResGeneral.ResStatus
WHEN 'X' THEN CAST(SoftripNetTest.dbo.fn_GetXMLValue(SoftripNetTest.dbo.fn_GetXMLValue(AdditionalData, 'HeaderCancellationData', ''), 'TotalQuotePrice', 0) AS DECIMAL(18, 2))
+ CAST(SoftripNetTest.dbo.fn_GetXMLValue(SoftripNetTest.dbo.fn_GetXMLValue(AdditionalData, 'HeaderCancellationData', ''), 'TotalQuotePriceTax', 0) AS DECIMAL(18, 2))
+ CAST(SoftripNetTest.dbo.fn_GetXMLValue(SoftripNetTest.dbo.fn_GetXMLValue(AdditionalData, 'HeaderCancellationData', ''), 'TotalQuotePriceGST', 0) AS DECIMAL(18, 2))
as total
ELSE (SUM(SoftripNetTest.dbo.ResGeneral.TTlQuotePrice + SoftripNetTest.dbo.ResGeneral.TTlQuotePriceTax + SoftripNetTest.dbo.ResGeneral.TTlQuotePriceGST) as total
END
这是整个查询
SELECT
SoftripNetTest.dbo.ResGeneral.ResNumber, SoftripNetTest.dbo.ResGeneral.LeadName,
SoftripNetTest.dbo.ResGeneral.NoPax, SoftripNetTest.dbo.ResGeneral.MarketCode,
SoftripNetTest.dbo.ResGeneral.ResProductCode, SoftripNetTest.dbo.ResGeneral.ResTourCode,
SoftripNetTest.dbo.ResGeneral.SellMethod, SoftripNetTest.dbo.ResGeneral.DepDate,
CONVERT(datetime, SoftripNetTest.dbo.ResGeneral.BookDate, 101) AS bookdate,
SoftripNetTest.dbo.ResGeneral.BookedBy,
CASE SoftripNetTest.dbo.ResGeneral.ResStatus
WHEN 'X' THEN CAST(SoftripNetTest.dbo.fn_GetXMLValue(SoftripNetTest.dbo.fn_GetXMLValue(AdditionalData, 'HeaderCancellationData', ''), 'TotalQuotePrice', 0) AS DECIMAL(18, 2))
+ CAST(SoftripNetTest.dbo.fn_GetXMLValue(SoftripNetTest.dbo.fn_GetXMLValue(AdditionalData, 'HeaderCancellationData', ''), 'TotalQuotePriceTax', 0) AS DECIMAL(18, 2))
+ CAST(SoftripNetTest.dbo.fn_GetXMLValue(SoftripNetTest.dbo.fn_GetXMLValue(AdditionalData, 'HeaderCancellationData', ''), 'TotalQuotePriceGST', 0) AS DECIMAL(18, 2))
ELSE (SUM(SoftripNetTest.dbo.ResGeneral.TTlQuotePrice + SoftripNetTest.dbo.ResGeneral.TTlQuotePriceTax + SoftripNetTest.dbo.ResGeneral.TTlQuotePriceGST)
END,
SoftripNetTest.dbo.Product.ProductName, SoftripNetTest.dbo.ResGeneral.ParentResNumber,
SoftripNetTest.dbo.ResGeneral.ResStatus,
dbo.ResItinerary_insurance.LocalCost * dbo.ResItinerary_insurance.Qty AS insurance
FROM
SoftripNetTest.dbo.ResGeneral
INNER JOIN
SoftripNetTest.dbo.Product ON SoftripNetTest.dbo.ResGeneral.ResProductCode = SoftripNetTest.dbo.Product.ProductCode
LEFT OUTER JOIN
dbo.ResItinerary_insurance ON SoftripNetTest.dbo.ResGeneral.ResNumber = dbo.ResItinerary_insurance.ResNumber
WHERE
(SoftripNetTest.dbo.ResGeneral.BookDate BETWEEN @startdate AND @enddate)
GROUP BY
SoftripNetTest.dbo.ResGeneral.ResNumber, SoftripNetTest.dbo.ResGeneral.LeadName,
SoftripNetTest.dbo.ResGeneral.NoPax, SoftripNetTest.dbo.ResGeneral.MarketCode,
SoftripNetTest.dbo.ResGeneral.ResProductCode, SoftripNetTest.dbo.ResGeneral.ResTourCode,
SoftripNetTest.dbo.ResGeneral.SellMethod, SoftripNetTest.dbo.ResGeneral.DepDate, $
SoftripNetTest.dbo.ResGeneral.BookDate, SoftripNetTest.dbo.ResGeneral.BookedBy,
SoftripNetTest.dbo.ResGeneral.BookSource, SoftripNetTest.dbo.Product.ProductName,
SoftripNetTest.dbo.ResGeneral.ParentResNumber, SoftripNetTest.dbo.ResGeneral.ResStatus,
在我的案例陈述中,我一直在AS附近收到错误? case语句可以在查询中使用吗?我希望对于resstatus A和X都能以相同的方式拉动所有内容,除了总计这是最好的方法吗?我使用的是fn_getxmlvalue这是一个函数,因为取消的信息存储在xml中。
答案 0 :(得分:2)
CASE SoftripNetTest.dbo.ResGeneral.ResStatus
WHEN 'X' THEN CAST(SoftripNetTest.dbo.fn_GetXMLValue(SoftripNetTest.dbo.fn_GetXMLValue(AdditionalData, 'HeaderCancellationData', ''), 'TotalQuotePrice', 0) AS DECIMAL(18, 2))
+ CAST(SoftripNetTest.dbo.fn_GetXMLValue(SoftripNetTest.dbo.fn_GetXMLValue(AdditionalData, 'HeaderCancellationData', ''), 'TotalQuotePriceTax', 0) AS DECIMAL(18, 2))
+ CAST(SoftripNetTest.dbo.fn_GetXMLValue(SoftripNetTest.dbo.fn_GetXMLValue(AdditionalData, 'HeaderCancellationData', ''), 'TotalQuotePriceGST', 0) AS DECIMAL(18, 2))
ELSE (SUM(SoftripNetTest.dbo.ResGeneral.TTlQuotePrice + SoftripNetTest.dbo.ResGeneral.TTlQuotePriceTax + SoftripNetTest.dbo.ResGeneral.TTlQuotePriceGST) as total
END
as total
您不能命名case语句的各个部分,但您可以命名结果列,只需将您的名称移到case语句的末尾
答案 1 :(得分:2)
在as total
声明的END
之后移动CASE
别名。
CASE SoftripNetTest.dbo.ResGeneral.ResStatus
WHEN 'X' THEN CAST(SoftripNetTest.dbo.fn_GetXMLValue(SoftripNetTest.dbo.fn_GetXMLValue(AdditionalData, 'HeaderCancellationData', ''), 'TotalQuotePrice', 0) AS DECIMAL(18, 2))
+ CAST(SoftripNetTest.dbo.fn_GetXMLValue(SoftripNetTest.dbo.fn_GetXMLValue(AdditionalData, 'HeaderCancellationData', ''), 'TotalQuotePriceTax', 0) AS DECIMAL(18, 2))
+ CAST(SoftripNetTest.dbo.fn_GetXMLValue(SoftripNetTest.dbo.fn_GetXMLValue(AdditionalData, 'HeaderCancellationData', ''), 'TotalQuotePriceGST', 0) AS DECIMAL(18, 2))
ELSE (SUM(SoftripNetTest.dbo.ResGeneral.TTlQuotePrice + SoftripNetTest.dbo.ResGeneral.TTlQuotePriceTax + SoftripNetTest.dbo.ResGeneral.TTlQuotePriceGST)
END as total
答案 2 :(得分:0)
只需移除as total
中间的CASE
。
CASE
语句最后会输出一个字段,您将其命名为结束后:
CASE WHEN ...
WHEN ...
ELSE ...
END as 'Blah'
答案 3 :(得分:0)
从以下行中删除“as total”别名:
ELSE (SUM(SoftripNetTest.dbo.ResGeneral.TTlQuotePrice + SoftripNetTest.dbo.ResGeneral.TTlQuotePriceTax + SoftripNetTest.dbo.ResGeneral.TTlQuotePriceGST) as total
END