在SQL中排序和排序varchars

时间:2018-12-07 17:56:53

标签: sql sql-server

我有一个看起来像这样的SQL表

enter image description here

如您所见,原始本金余额订购不正确。这些变量存储为VARCHAR。如何正确排序和排序?

这是我的sql代码:

WITH Original_Principal_Bal
AS (
    SELECT [New Loan Number]
        ,[Current Amortizing UPB]
        ,[Current Def UPB]
        ,sum([Current Amortizing UPB] + [Current Def UPB]) OVER (PARTITION BY Deal) AS [Total UPB]
        ,BPO
        ,[Current Rate]
        ,[Current Maturity]
        ,[Next Due Date]
        ,[First Payment Date]
        ,CASE 
            WHEN [Original Loan Amount] BETWEEN 0.01
                    AND 100000
                THEN '$0.01 to $100,000'
            WHEN [Original Loan Amount] BETWEEN 100000.01
                    AND 200000
                THEN '$100,000.01 to $200,000'
            WHEN [Original Loan Amount] BETWEEN 200000.01
                    AND 300000
                THEN '$200,000.01 to $300,000'
            WHEN [Original Loan Amount] BETWEEN 300000.01
                    AND 400000
                THEN '$300,000.01 to $400,000'
            WHEN [Original Loan Amount] BETWEEN 400000.01
                    AND 500000
                THEN '$400,000.01 to $500,000'
            WHEN [Original Loan Amount] BETWEEN 500000.01
                    AND 600000
                THEN '$500,000.01 to $600,000'
            WHEN [Original Loan Amount] BETWEEN 600000.01
                    AND 700000
                THEN '$600,000.01 to $700,000'
            WHEN [Original Loan Amount] BETWEEN 700000.01
                    AND 800000
                THEN '$700,000.01 to $800,000'
            WHEN [Original Loan Amount] BETWEEN 800000.01
                    AND 900000
                THEN '$800,000.01 to $900,000'
            WHEN [Original Loan Amount] BETWEEN 900000.01
                    AND 1000000
                THEN '$900,000.01 to $1,000,000'
            WHEN [Original Loan Amount] BETWEEN 1100000.01
                    AND 1200000
                THEN '$1,100,000.01 to $1,200,000'
            WHEN [Original Loan Amount] BETWEEN 1300000.01
                    AND 1400000
                THEN '$1,300,000.01 to $1,400,000'
            WHEN [Original Loan Amount] BETWEEN 1600000.01
                    AND 1700000
                THEN '$1,600,000.01 to $1,700,000'
            WHEN [Original Loan Amount] BETWEEN 1900000.01
                    AND 2000000
                THEN '$1,900,000.01 to $2,000,000'
            WHEN [Original Loan Amount] > 2000000.01
                THEN '$2,000,000 or greater'
            END AS [Original Principal Balance]
    FROM Portfolio_Analytics..Securitization_Tape
    )
SELECT [Original Principal Balance]
    ,COUNT([New Loan Number]) AS [Number of Mortgage Loans]
    ,ROUND(sum([Current Amortizing UPB] + [Current Def UPB]), 0) AS [Aggregate Unpaid Principal Balance as of Cut-off Date ($)]
    ,ROUND(avg([Current Amortizing UPB] + [Current Def UPB]), 0) AS [Average Unpaid Principal Balance ($)]
    ,ROUND(sum(([Current Amortizing UPB] + [Current Def UPB]) / [Total UPB]) * 100, 2) AS [Percetage of Aggregate Principal Balance as of Cut-off Date(%)]
    ,sum(BPO) AS [Aggregate Updated Value($)]
    ,ROUND(sum([Current Rate] * ([Current Amortizing UPB] + [Current Def UPB])) / sum([Current Amortizing UPB] + [Current Def UPB]), 2) AS [Weighted Average Mortgage Interest Rate(%)]
    ,ROUND(sum(([Current Amortizing UPB] + [Current Def UPB]) / bpo * ([Current Amortizing UPB] + [Current Def UPB])) / sum([Current Amortizing UPB] + [Current Def UPB]) * 100, 2) AS [Weighted Average Updated Loan-to-Value Ratio(%)]
    ,ROUND(sum((DATEDIFF(month, [Next Due Date], [Current Maturity]) + 1) * ([Current Amortizing UPB] + [Current Def UPB])) / (sum([Current Amortizing UPB] + [Current Def UPB])), 0) AS [Weighted Average Remaining Term to Maturity(Months)]
    ,ROUND(sum((DATEDIFF(month, [First Payment Date], [Next Due Date]) + 1) * ([Current Amortizing UPB] + [Current Def UPB])) / (sum([Current Amortizing UPB] + [Current Def UPB])), 0) AS [Weighted Average Remaining Term to Maturity(Months)]
FROM Original_Principal_Bal
GROUP BY [Original Principal Balance]

也许在创建表时我需要以不同的方式存储数字,但是我不确定其他如何与我想要的表匹配。

4 个答案:

答案 0 :(得分:1)

您可以这样做:

ORDER BY MIN([Original Loan Amount])

您还需要在CTE中加入[Original Loan Amount]

答案 1 :(得分:1)

正如Trincot所说,“渲染”不属于数据库层。我不必同意或不同意这一说法。尽管这可能是“最佳实践”,但总有一些实际的理由违背规范。

看到您的存储桶标签是手动输入的字符串,我认为最好的选择是在您的select语句中简单地添加一个排名列。像这样:

WITH Original_Principal_Bal
AS (
    SELECT [New Loan Number]
        ,[Current Amortizing UPB]
        ,[Current Def UPB]
        ,sum([Current Amortizing UPB] + [Current Def UPB]) OVER (PARTITION BY Deal) AS [Total UPB]
        ,BPO
        ,[Current Rate]
        ,[Current Maturity]
        ,[Next Due Date]
        ,[First Payment Date]
        ,CASE 
            WHEN [Original Loan Amount] BETWEEN 0.01
                    AND 100000
                THEN '$0.01 to $100,000'
            WHEN [Original Loan Amount] BETWEEN 100000.01
                    AND 200000
                THEN '$100,000.01 to $200,000'
            WHEN [Original Loan Amount] BETWEEN 200000.01
                    AND 300000
                THEN '$200,000.01 to $300,000'
            WHEN [Original Loan Amount] BETWEEN 300000.01
                    AND 400000
                THEN '$300,000.01 to $400,000'
            WHEN [Original Loan Amount] BETWEEN 400000.01
                    AND 500000
                THEN '$400,000.01 to $500,000'
            WHEN [Original Loan Amount] BETWEEN 500000.01
                    AND 600000
                THEN '$500,000.01 to $600,000'
            WHEN [Original Loan Amount] BETWEEN 600000.01
                    AND 700000
                THEN '$600,000.01 to $700,000'
            WHEN [Original Loan Amount] BETWEEN 700000.01
                    AND 800000
                THEN '$700,000.01 to $800,000'
            WHEN [Original Loan Amount] BETWEEN 800000.01
                    AND 900000
                THEN '$800,000.01 to $900,000'
            WHEN [Original Loan Amount] BETWEEN 900000.01
                    AND 1000000
                THEN '$900,000.01 to $1,000,000'
            WHEN [Original Loan Amount] BETWEEN 1100000.01
                    AND 1200000
                THEN '$1,100,000.01 to $1,200,000'
            WHEN [Original Loan Amount] BETWEEN 1300000.01
                    AND 1400000
                THEN '$1,300,000.01 to $1,400,000'
            WHEN [Original Loan Amount] BETWEEN 1600000.01
                    AND 1700000
                THEN '$1,600,000.01 to $1,700,000'
            WHEN [Original Loan Amount] BETWEEN 1900000.01
                    AND 2000000
                THEN '$1,900,000.01 to $2,000,000'
            WHEN [Original Loan Amount] > 2000000.01
                THEN '$2,000,000 or greater'
            END AS [Original Principal Balance]

,CASE 
            WHEN [Original Loan Amount] BETWEEN 0.01
                    AND 100000
                THEN 0
            WHEN [Original Loan Amount] BETWEEN 100000.01
                    AND 200000
                THEN 1
            WHEN [Original Loan Amount] BETWEEN 200000.01
                    AND 300000
                THEN 2
            WHEN [Original Loan Amount] BETWEEN 300000.01
                    AND 400000
                THEN 4
            WHEN [Original Loan Amount] BETWEEN 400000.01
                    AND 500000
                THEN 5
            WHEN [Original Loan Amount] BETWEEN 500000.01
                    AND 600000
                THEN 6
            WHEN [Original Loan Amount] BETWEEN 600000.01
                    AND 700000
                THEN 7
            WHEN [Original Loan Amount] BETWEEN 700000.01
                    AND 800000
                THEN 8
            WHEN [Original Loan Amount] BETWEEN 800000.01
                    AND 900000
                THEN 9
            WHEN [Original Loan Amount] BETWEEN 900000.01
                    AND 1000000
                THEN 10
            WHEN [Original Loan Amount] BETWEEN 1100000.01
                    AND 1200000
                THEN 11
            WHEN [Original Loan Amount] BETWEEN 1300000.01
                    AND 1400000
                THEN 12
            WHEN [Original Loan Amount] BETWEEN 1600000.01
                    AND 1700000
                THEN 13
            WHEN [Original Loan Amount] BETWEEN 1900000.01
                    AND 2000000
                THEN 14
            WHEN [Original Loan Amount] > 2000000.01
                THEN 15
            END AS [Balance Rank]

    FROM Portfolio_Analytics..Securitization_Tape
    )
SELECT [Original Principal Balance]
    ,COUNT([New Loan Number]) AS [Number of Mortgage Loans]
    ,ROUND(sum([Current Amortizing UPB] + [Current Def UPB]), 0) AS [Aggregate Unpaid Principal Balance as of Cut-off Date ($)]
    ,ROUND(avg([Current Amortizing UPB] + [Current Def UPB]), 0) AS [Average Unpaid Principal Balance ($)]
    ,ROUND(sum(([Current Amortizing UPB] + [Current Def UPB]) / [Total UPB]) * 100, 2) AS [Percetage of Aggregate Principal Balance as of Cut-off Date(%)]
    ,sum(BPO) AS [Aggregate Updated Value($)]
    ,ROUND(sum([Current Rate] * ([Current Amortizing UPB] + [Current Def UPB])) / sum([Current Amortizing UPB] + [Current Def UPB]), 2) AS [Weighted Average Mortgage Interest Rate(%)]
    ,ROUND(sum(([Current Amortizing UPB] + [Current Def UPB]) / bpo * ([Current Amortizing UPB] + [Current Def UPB])) / sum([Current Amortizing UPB] + [Current Def UPB]) * 100, 2) AS [Weighted Average Updated Loan-to-Value Ratio(%)]
    ,ROUND(sum((DATEDIFF(month, [Next Due Date], [Current Maturity]) + 1) * ([Current Amortizing UPB] + [Current Def UPB])) / (sum([Current Amortizing UPB] + [Current Def UPB])), 0) AS [Weighted Average Remaining Term to Maturity(Months)]
    ,ROUND(sum((DATEDIFF(month, [First Payment Date], [Next Due Date]) + 1) * ([Current Amortizing UPB] + [Current Def UPB])) / (sum([Current Amortizing UPB] + [Current Def UPB])), 0) AS [Weighted Average Remaining Term to Maturity(Months)]
FROM Original_Principal_Bal
GROUP BY [Original Principal Balance]

答案 2 :(得分:1)

尝试:

order by cast(substring(left( [Original Principal Balance],charindex(' ',[Original Principal Balance])-1),2,1000) as decimal(18,4))

这会将第一个数字转换为数字并对其进行排序。

答案 3 :(得分:0)

关于MONEY数据类型,有一件很酷的事情……它不抱怨逗号或货币符号,并且它是一种数字数据类型,因此它的排序方式与其他数字类似……这里仅举几个例子……

DECLARE @string VARCHAR(50) = '$700,000.01 to $800,000';

SELECT CONVERT(MONEY, SUBSTRING(@string, 1, CHARINDEX(' ', @string, 2)));

SELECT CONVERT(MONEY, LEFT(@string, PATINDEX('%[^0-9.,$]%', @string)));

SELECT CONVERT(MONEY, LEFT(@string, CHARINDEX(' ', @string)));