我正在尝试将两行结果合并为一行。以下是我的结果:
| OwnerTaxRate | OwnerTaxAmount | OwnerTaxTotal | LoanTaxRate | LoanTaxAmount | LoanTaxTotal
| 8.4% | 71.40 | 921.4 | NULL | NULL | NULL
| NULL | NULL | NULL | 8.4% | 10.08 | 130.08
以下是我需要返回结果的方法。
| OwnerTaxRate | OwnerTaxAmount | OwnerTaxTotal | LoanTaxRate | LoanTaxAmount | LoanTaxTotal
| 8.4% | 71.40 | 921.4 | 8.4% | 10.08 | 130.08
创建两行的原因是因为基于LoanHolder
的每个订单有两个条目(它是0或1)。我想添加一个CASE
语句来选择我想拉的LoanHolder会起作用,但显然不行。这是我的SELECT
声明:
SELECT
CASE
WHEN
ORC.LoanHolder = 1
THEN CONVERT(VARCHAR(MAX),ORC.Owner_TaxRateBasic) + '%'
ELSE NULL END AS 'OwnerTaxRate'
,CASE
WHEN
ORC.LoanHolder = 1
AND ORC.Owner_TaxAmountBasic > 0
AND ORC.Owner_TaxAmountSimultaneous = 0
AND ORC.Owner_TaxAmountAssociated = 0
THEN ORC.Owner_TaxAmountBasic
WHEN
ORC.LoanHolder = 1
AND ORC.Owner_TaxAmountBasic = 0
AND ORC.Owner_TaxAmountSimultaneous > 0
AND ORC.Owner_TaxAmountAssociated = 0
THEN ORC.Owner_TaxAmountSimultaneous
WHEN
ORC.LoanHolder = 1
AND ORC.Owner_TaxAmountBasic = 0
AND ORC.Owner_TaxAmountSimultaneous = 0
AND ORC.Owner_TaxAmountAssociated > 0
THEN ORC.Owner_TaxAmountAssociated
ELSE NULL END AS 'OwnerTaxAmount'
,CASE
WHEN
ORC.LoanHolder = 1
AND ORC.Owner_TaxTotalBasic > 0
AND ORC.Owner_TaxTotalSimultaneous = 0
AND ORC.Owner_TaxTotalAssociated = 0
THEN ORC.Owner_TaxTotalBasic
WHEN
ORC.LoanHolder = 1
AND ORC.Owner_TaxTotalBasic = 0
AND ORC.Owner_TaxTotalSimultaneous > 0
AND ORC.Owner_TaxTotalAssociated = 0
THEN ORC.Owner_TaxTotalSimultaneous
WHEN
ORC.LoanHolder = 1
AND ORC.Owner_TaxTotalBasic = 0
AND ORC.Owner_TaxTotalSimultaneous = 0
AND ORC.Owner_TaxTotalAssociated > 0
THEN ORC.Owner_TaxTotalAssociated
ELSE NULL END AS 'OwnerTaxTotal'
,CASE
WHEN
ORC.LoanHolder = 0
THEN CONVERT(VARCHAR(MAX),ORC.Loan_TaxRateBasic) + '%'
ELSE NULL END AS 'LoanTaxRate'
,CASE
WHEN
ORC.LoanHolder = 0
AND ORC.Loan_TaxAmountBasic > 0
AND ORC.Loan_TaxAmountSimultaneous = 0
AND ORC.Loan_TaxAmountAssociated = 0
THEN ORC.Loan_TaxAmountBasic
WHEN
ORC.LoanHolder = 0
AND ORC.Loan_TaxAmountBasic = 0
AND ORC.Loan_TaxAmountSimultaneous > 0
AND ORC.Loan_TaxAmountAssociated = 0
THEN ORC.Loan_TaxAmountSimultaneous
WHEN
ORC.LoanHolder = 0
AND ORC.Loan_TaxAmountBasic = 0
AND ORC.Loan_TaxAmountSimultaneous = 0
AND ORC.Loan_TaxAmountAssociated > 0
THEN ORC.Loan_TaxAmountAssociated
ELSE NULL END AS 'LoanTaxAmount'
,CASE
WHEN
ORC.LoanHolder = 0
AND ORC.Loan_TaxTotalBasic > 0
AND ORC.Loan_TaxTotalSimultaneous = 0
AND ORC.Loan_TaxTotalAssociated = 0
THEN ORC.Loan_TaxTotalBasic
WHEN
ORC.LoanHolder = 0
AND ORC.Loan_TaxTotalBasic = 0
AND ORC.Loan_TaxTotalSimultaneous > 0
AND ORC.Loan_TaxTotalAssociated = 0
THEN ORC.Loan_TaxTotalSimultaneous
WHEN
ORC.LoanHolder = 0
AND ORC.Loan_TaxTotalBasic = 0
AND ORC.Loan_TaxTotalSimultaneous = 0
AND ORC.Loan_TaxTotalAssociated > 0
THEN ORC.Loan_TaxTotalAssociated
ELSE NULL END AS 'LoanTaxTotal'
FROM
OrderRateCalculation ORC
WHERE
ORC.OrdersID = @OrdersID
答案 0 :(得分:3)
这个怎么样?
SELECT
MAX(CASE
WHEN
ORC.LoanHolder = 1
THEN CONVERT(VARCHAR(MAX),ORC.Owner_TaxRateBasic) + '%'
ELSE NULL END) AS 'OwnerTaxRate'
,MAX(CASE
WHEN
ORC.LoanHolder = 1
AND ORC.Owner_TaxAmountBasic > 0
AND ORC.Owner_TaxAmountSimultaneous = 0
AND ORC.Owner_TaxAmountAssociated = 0
THEN ORC.Owner_TaxAmountBasic
WHEN
ORC.LoanHolder = 1
AND ORC.Owner_TaxAmountBasic = 0
AND ORC.Owner_TaxAmountSimultaneous > 0
AND ORC.Owner_TaxAmountAssociated = 0
THEN ORC.Owner_TaxAmountSimultaneous
WHEN
ORC.LoanHolder = 1
AND ORC.Owner_TaxAmountBasic = 0
AND ORC.Owner_TaxAmountSimultaneous = 0
AND ORC.Owner_TaxAmountAssociated > 0
THEN ORC.Owner_TaxAmountAssociated
ELSE NULL END) AS 'OwnerTaxAmount'
,MAX(CASE
WHEN
ORC.LoanHolder = 1
AND ORC.Owner_TaxTotalBasic > 0
AND ORC.Owner_TaxTotalSimultaneous = 0
AND ORC.Owner_TaxTotalAssociated = 0
THEN ORC.Owner_TaxTotalBasic
WHEN
ORC.LoanHolder = 1
AND ORC.Owner_TaxTotalBasic = 0
AND ORC.Owner_TaxTotalSimultaneous > 0
AND ORC.Owner_TaxTotalAssociated = 0
THEN ORC.Owner_TaxTotalSimultaneous
WHEN
ORC.LoanHolder = 1
AND ORC.Owner_TaxTotalBasic = 0
AND ORC.Owner_TaxTotalSimultaneous = 0
AND ORC.Owner_TaxTotalAssociated > 0
THEN ORC.Owner_TaxTotalAssociated
ELSE NULL END) AS 'OwnerTaxTotal'
,MAX(CASE
WHEN
ORC.LoanHolder = 0
THEN CONVERT(VARCHAR(MAX),ORC.Loan_TaxRateBasic) + '%'
ELSE NULL END) AS 'LoanTaxRate'
,MAX(CASE
WHEN
ORC.LoanHolder = 0
AND ORC.Loan_TaxAmountBasic > 0
AND ORC.Loan_TaxAmountSimultaneous = 0
AND ORC.Loan_TaxAmountAssociated = 0
THEN ORC.Loan_TaxAmountBasic
WHEN
ORC.LoanHolder = 0
AND ORC.Loan_TaxAmountBasic = 0
AND ORC.Loan_TaxAmountSimultaneous > 0
AND ORC.Loan_TaxAmountAssociated = 0
THEN ORC.Loan_TaxAmountSimultaneous
WHEN
ORC.LoanHolder = 0
AND ORC.Loan_TaxAmountBasic = 0
AND ORC.Loan_TaxAmountSimultaneous = 0
AND ORC.Loan_TaxAmountAssociated > 0
THEN ORC.Loan_TaxAmountAssociated
ELSE NULL END) AS 'LoanTaxAmount'
,MAX(CASE
WHEN
ORC.LoanHolder = 0
AND ORC.Loan_TaxTotalBasic > 0
AND ORC.Loan_TaxTotalSimultaneous = 0
AND ORC.Loan_TaxTotalAssociated = 0
THEN ORC.Loan_TaxTotalBasic
WHEN
ORC.LoanHolder = 0
AND ORC.Loan_TaxTotalBasic = 0
AND ORC.Loan_TaxTotalSimultaneous > 0
AND ORC.Loan_TaxTotalAssociated = 0
THEN ORC.Loan_TaxTotalSimultaneous
WHEN
ORC.LoanHolder = 0
AND ORC.Loan_TaxTotalBasic = 0
AND ORC.Loan_TaxTotalSimultaneous = 0
AND ORC.Loan_TaxTotalAssociated > 0
THEN ORC.Loan_TaxTotalAssociated
ELSE NULL END) AS 'LoanTaxTotal'
FROM
OrderRateCalculation ORC
WHERE
ORC.OrdersID = @OrdersID
答案 1 :(得分:0)
根据您的loanHolder
将表格加入另一个自身实例 SELECT
(CONVERT(VARCHAR(MAX),ORC1.Owner_TaxRateBasic) + '%') AS 'OwnerTaxRate'
,CASE
WHEN
ORC1.Owner_TaxAmountBasic > 0
AND ORC1.Owner_TaxAmountSimultaneous = 0
AND ORC1.Owner_TaxAmountAssociated = 0
THEN ORC1.Owner_TaxAmountBasic
WHEN
ORC1.Owner_TaxAmountBasic = 0
AND ORC1.Owner_TaxAmountSimultaneous > 0
AND ORC1.Owner_TaxAmountAssociated = 0
THEN ORC1.Owner_TaxAmountSimultaneous
WHEN
ORC1.Owner_TaxAmountBasic = 0
AND ORC1.Owner_TaxAmountSimultaneous = 0
AND ORC1.Owner_TaxAmountAssociated > 0
THEN ORC1.Owner_TaxAmountAssociated
ELSE NULL END AS 'OwnerTaxAmount'
,CASE
WHEN
ORC1.Owner_TaxTotalBasic > 0
AND ORC1.Owner_TaxTotalSimultaneous = 0
AND ORC1.Owner_TaxTotalAssociated = 0
THEN ORC1.Owner_TaxTotalBasic
WHEN
ORC1.Owner_TaxTotalBasic = 0
AND ORC1.Owner_TaxTotalSimultaneous > 0
AND ORC1.Owner_TaxTotalAssociated = 0
THEN ORC1.Owner_TaxTotalSimultaneous
WHEN
ORC1.Owner_TaxTotalBasic = 0
AND ORC1.Owner_TaxTotalSimultaneous = 0
AND ORC1.Owner_TaxTotalAssociated > 0
THEN ORC1.Owner_TaxTotalAssociated
ELSE NULL END AS 'OwnerTaxTotal'
,(CONVERT(VARCHAR(MAX),ORC0.Loan_TaxRateBasic) + '%') AS 'LoanTaxRate'
,CASE
WHEN
ORC0.Loan_TaxAmountBasic > 0
AND ORC0.Loan_TaxAmountSimultaneous = 0
AND ORC0.Loan_TaxAmountAssociated = 0
THEN ORC0.Loan_TaxAmountBasic
WHEN
ORC0.Loan_TaxAmountBasic = 0
AND ORC0.Loan_TaxAmountSimultaneous > 0
AND ORC0.Loan_TaxAmountAssociated = 0
THEN ORC0.Loan_TaxAmountSimultaneous
WHEN
ORC0.Loan_TaxAmountBasic = 0
AND ORC0.Loan_TaxAmountSimultaneous = 0
AND ORC0.Loan_TaxAmountAssociated > 0
THEN ORC0.Loan_TaxAmountAssociated
ELSE NULL END AS 'LoanTaxAmount'
,CASE
WHEN
ORC0.Loan_TaxTotalBasic > 0
AND ORC0.Loan_TaxTotalSimultaneous = 0
AND ORC0.Loan_TaxTotalAssociated = 0
THEN ORC0.Loan_TaxTotalBasic
WHEN
ORC0.Loan_TaxTotalBasic = 0
AND ORC0.Loan_TaxTotalSimultaneous > 0
AND ORC0.Loan_TaxTotalAssociated = 0
THEN ORC0.Loan_TaxTotalSimultaneous
WHEN
ORC0.Loan_TaxTotalBasic = 0
AND ORC0.Loan_TaxTotalSimultaneous = 0
AND ORC0.Loan_TaxTotalAssociated > 0
THEN ORC0.Loan_TaxTotalAssociated
ELSE NULL END AS 'LoanTaxTotal'
FROM
OrderRateCalculation ORC0
join OrderRateCalculation ORC1 on ORC1.OrdersID = ORC0.OrdersID and ORC1.LoanHolder = 1
WHERE
ORC.OrdersID = @OrdersID and ORC0.LoanHolder = 0