2017-05-15 3 views
0

ich folgenden Fall Aussage gegen meine Kreditnehmer Tabelle habenerstellen niedrigen und hohen Bereich mit Case-Anweisung

SELECT borrower_id, 
    CASE 
    WHEN bo.fico_score >= 0 AND bo.fico_score <= 599 THEN '0-599' 
    WHEN bo.fico_score >= 600 AND bo.fico_score <= 649 THEN '600-649' 
    WHEN bo.fico_score >= 650 AND bo.fico_score <= 699 THEN '650-699' 
    WHEN bo.fico_score >= 700 AND bo.fico_score <= 749 THEN '700-749' 
    WHEN bo.fico_score >= 750 AND bo.fico_score <= 800 THEN '750-800' 
    WHEN bo.fico_score > 800 AND bo.fico_score <= 850 THEN '801-850' 
    END AS "Borrower FICO" 
from borrowers bo; 

Der Anwendungsfall erfordert ich für einen niedrigen Bereich und hohen Bereich anstelle der oben genannten. Der folgende Code funktioniert.

SELECT borrower_id, 
    CASE 
    WHEN bo.fico_score >= 0 AND bo.fico_score <= 599 THEN '0' 
    WHEN bo.fico_score >= 600 AND bo.fico_score <= 649 THEN '600' 
    WHEN bo.fico_score >= 650 AND bo.fico_score <= 699 THEN '650' 
    WHEN bo.fico_score >= 700 AND bo.fico_score <= 749 THEN '700' 
    WHEN bo.fico_score >= 750 AND bo.fico_score <= 800 THEN '750' 
    WHEN bo.fico_score > 800 AND bo.fico_score <= 850 THEN '801' 
    END AS "Borrower FICO LOW", 
    CASE 
    WHEN bo.fico_score >= 0 AND bo.fico_score <= 599 THEN '599' 
    WHEN bo.fico_score >= 600 AND bo.fico_score <= 649 THEN '649' 
    WHEN bo.fico_score >= 650 AND bo.fico_score <= 699 THEN '699' 
    WHEN bo.fico_score >= 700 AND bo.fico_score <= 749 THEN '749' 
    WHEN bo.fico_score >= 750 AND bo.fico_score <= 800 THEN '800' 
    WHEN bo.fico_score > 800 AND bo.fico_score <= 850 THEN '850' 
    END AS "Borrower FICO HIGH", 
from borrowers bo; 

Ich glaube nicht, dass es sehr gutes Design ist. Gibt es eine bessere Möglichkeit, diese Case-Anweisung zu schreiben, so dass ich einen niedrigen und hohen Bereich für einen Kreditnehmer Kredit-Score bieten kann? ‚-‘

+0

Wenn Sie Zeichenfolgen verwenden, warum nicht einfach so etwas wie ' '600-649''. –

+0

@GordonLinoff Dies ist, was ich in meiner ursprünglichen Abfrage hatte, aber die Anforderungen wurden geändert. – Alan

Antwort

1

Sie können Ihre ursprüngliche Abfrage und teilen Sie die resultierenden Strings durch verwenden, z.B .:

with borrowers(borrower_id, fico_score) as (
values 
    (1, 133), 
    (2, 633) 
) 

select 
    borrower_id, 
    split_part(fico, '-', 1) as "Borrower FICO Low", 
    split_part(fico, '-', 2) as "Borrower FICO High" 
from (
    select borrower_id, 
     case 
     when bo.fico_score >= 0 and bo.fico_score <= 599 then '0-599' 
     when bo.fico_score >= 600 and bo.fico_score <= 649 then '600-649' 
     when bo.fico_score >= 650 and bo.fico_score <= 699 then '650-699' 
     when bo.fico_score >= 700 and bo.fico_score <= 749 then '700-749' 
     when bo.fico_score >= 750 and bo.fico_score <= 800 then '750-800' 
     when bo.fico_score > 800 and bo.fico_score <= 850 then '801-850' 
     end as fico 
    from borrowers bo 
    ) s; 

borrower_id | Borrower FICO Low | Borrower FICO High 
-------------+-------------------+-------------------- 
      1 | 0     | 599 
      2 | 600    | 649 
(2 rows)   
+0

Ich mag diese Idee. Vielen Dank. Akzeptiert und abgestimmt. – Alan

Verwandte Themen