2017-09-08 18 views
1

私はこれほど長く苦労しています。誰かが私を助けることを願っています。 3列の条件に基づいて2列を追加したいと思います。複数の条件に基づいて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)) 

オファー額については

​​

これは私が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 

は、私はいくつかの他の方法を試してみましたが、これはそのI最も近いです必要な結果を得る。

私は本当に助けに感謝します。これが十分な情報であることを願っています。

+0

予想される出力は何ですか? –

+0

私の予想される出力は、上記の表の最後の2つの列です。見積もり金額とオファー額 –

+0

なぜすべてを選択するのですか?このクエリでは、すべての列の行が、あなたのQuoteAmount列より大きい場合に使用されます。 –

答えて

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

問題は、あなたが分離WHEN年代にそれぞれの場合の複数の条件を置く代わりに適切なAND/OR演算子と条件を連鎖していることであった

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

大変ありがとうございました –

0

等しくなる([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 
+0

ありがとう –

関連する問題