根据多个条件创建2列

时间:2017-09-08 10:54:37

标签: sql-server

我一直在努力解决这个问题。希望有人可以帮助我。 我想根据3列的条件添加2列。

Quote/Offer|RequiredLoanAmount |QuotationAmount|**Quote Amount**| **Offer Amount**
Quote      |      490000       |       0       |   0            |   0
Pending    |      640000       |     640000    | 640000         |   0
Pending    |      1180000      |     1062000   |   0            | 1062000
Offer      |      2562000      |     2305800   |   0            | 2305800
Quote      |      400000       |     392000    | 392000         |   0
Quote      |      770000       |     770000    | 770000         |   0
Pending    |      425000       |     680000    | 680000         |   0
Pending    |      1580000      |     1500000   |   0            | 1500000
Pending    |      260000       |     239985    |   0            | 239985
Pending    |      285000       |     285000    | 285000         |   0
Pending    |      600000       |     600000    | 600000         |   0
Pending    |      700000       |     700000    | 700000         |   0
Pending    |      1350000      |     1350000   | 1350000        |   0
Offer      |      689000       |     650000    |   0            | 650000
Pending    |      1980000      |     1980000   | 1980000        |   0
Pending    |      960000       |     960000    | 960000         |   0
Offer      |      670000       |     636500    |   0            | 636500

当报价/报价=待定或报价和收到的金额 - 所需金额> = 0时,它应将收到的金额列入报价金额列

当报价/要约=待定或要约和收到的金额 - 所需金额< 0然后它应该将收到的金额列入要约金额列

我的Excel电子表格中的公式如下:

报价金额:

=IF(AND(S22="Pending",(Q22-P22)>=0)=TRUE,Q22,IF(S22="Quote",Q22,0))

要约金额

=IF(AND(S22="Pending",(Q22-P22)<0)=TRUE,Q22,IF(S22="Offer",Q22,0))

这是我在SQL中尝试过的:

    SELECT *
, CASE
    WHEN [Quote/Offer] = 'Quote' AND QuotationAmount - RequiredLoanAmount >= 0 THEN QuotationAmount
    WHEN [Quote/Offer] = 'Pending' AND QuotationAmount - RequiredLoanAmount < 0 THEN QuotationAmount 
    ELSE 0
END AS QuoteAmount,

CASE
    WHEN [Quote/Offer] = 'Offer' AND QuotationAmount - RequiredLoanAmount > 0 THEN QuotationAmount 
    WHEN [Quote/Offer] = 'Pending' AND QuotationAmount - RequiredLoanAmount < 0 THEN QuotationAmount
    ELSE 0 
END AS OfferAmount

FROM #SubDatePartition

我尝试了其他一些方法,但这是我得到所需结果的最接近的方式。

我真的很感激帮助。希望这是足够的信息。

2 个答案:

答案 0 :(得分:0)

=IF(AND(S22="Pending",(Q22-P22)>=0)=TRUE,Q22,IF(S22="Quote",Q22,0))

将是平等的

CASE
WHEN [Quote/Offer] = 'Pending' AND QuotationAmount - RequiredLoanAmount >= 0 THEN QuotationAmount
WHEN [Quote/Offer] = 'Quote' THEN QuotationAmount
ELSE 0
END AS QuoteAmount

答案 1 :(得分:0)

问题在于您将每个案例的多个条件放在单独的WHEN中,而不是使用相应的AND/OR运算符链接条件(请注意[Quote/Offer] IN ('Pending','Quote')等效于([Quote/Offer]='Pending' OR [Quote/Offer]='Quote')):

SELECT [Quote/Offer], RequiredLoanAmount, QuotationAmount,
  CASE WHEN [Quote/Offer] IN ('Pending','Quote') AND QuotationAmount-RequiredLoanAmount>=0
       THEN QuotationAmount ELSE 0
    END AS QuoteAmount,
  CASE WHEN [Quote/Offer] IN ('Pending','Offer') AND QuotationAmount-RequiredLoanAmount<0
       THEN QuotationAmount ELSE 0 
  END AS OfferAmount
FROM #SubDatePartition