如何计算范围内结果数量的SQL字段?

时间:2014-05-27 04:21:30

标签: sql count sum

我有一张表格,其中列出了发票说明,发票逾期的金额和天数。 (例如,负数表示尚未到期; -4表示4天内到期)。

+------------------------------+------------+-----------+
| Description                  | Amount     | OverdueBy |
+------------------------------+------------+-----------+
|  Payment made on 17/05/13    | -149874.25 | 368       |
|  Stationary                  | 185.85     | -34       |
| .4m3 Concrete RMD            | 300.52     | -4        |
| 1 x red isolation lock       | 29.52      | -34       |
| 1.3T Concrete Lifting Clutch | 279.40     | -34       |
| 1.3T Swiftlift Clutch        | 634.63     | -34       |
| 100kg Garage Creeper         | 92.20      | 27        |
| 100x100x3.0 RHS Ptd          | 64.91      | -34       |
| 10mm Round Bar               | 502.43     | -34       |
| 10t Round Slings             | 453.20     | -34       |
| 12V 130A ALT                 | 324.50     | -4        |
| 12V 55W & 24V 70W P14 5S     | 137.28     | -4        |
| 12V Bilge Pump               | 150.95     | 26        |
| 12V Master Switch            | 286.00     | -4        |
| 14t Excavator 3103-300414    | 12540.00   | -4        |
| 150mm Blue Sunny Layflat     | 105.60     | -4        |
| 15kg Bright bullethead nails | 55.61      | -34       |
| 19 x Red isolation locks     | 542.36     | -34       |
| 2 Day Hire of HYTORC         | 330.00     | -4        |
| 2 x 1m x 1m spill trays      | 378.40     | -34       |
| 2 x Coolant 5Lt              | 45.36      | -4        |
| 2 x Printer Cartridges       | 87.90      | -34       |
| 2.1 ROADBASE                 | 6381.94    | -34       |
| 2.1 Road Base Wet            | 755.37     | -34       |
| 2.1 roadbase                 | 1311.76    | -4        |
+------------------------------+------------+-----------+

我想计算50天+,40-49天和30-39天过期的项目数量,然后显示选择金额的总和。

我的搜索结果的示例:

+--------------+--------------+--------------+
| OverdueRange | OverdueCount | Total Amount |
+--------------+--------------+--------------+
| 50+          | 12           | 10220        |
| 49-49        | 13           | 494          |
| 30-39        | 25           | -403         |
+--------------+--------------+--------------+

我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:1)

您可以使用CASE子句执行此操作:

SELECT CASE WHEN OverdueBy >= 50 THEN '50+' 
WHEN OverdueBy >= 40 and OverdueBy < 50 THEN '40-49'
WHEN OverdueBy >= 30 and OverdueBy < 40 THEN '30-39'
.....
ELSE 'Less than that' END AS OverdueRange,
Count(*) as OverdueCount,
Sum(Amount) as [Total Amount]
From yourtable
group by CASE WHEN OverdueBy >= 50 THEN '50+' 
WHEN OverdueBy >= 40 and OverdueBy < 50 THEN '40-49'
WHEN OverdueBy >= 30 and OverdueBy < 40 THEN '30-39'
.....
ELSE 'Less than that' END 

答案 1 :(得分:0)

内部查询创建一个“OverdueByRange”列,然后外部查询将其分组以获取总和和计数。

SELECT 
    OverdueRange, 
    COUNT(*) OverdueCount,
    SUM(AMOUNT) TotalAmount
FROM (
    SELECT
        Amount, 
        (CASE
            WHEN OverdueBy >= 50
            THEN '50+'
            WHEN OverdueBy BETWEEN 40 AND 49
            THEN '40-49'
            WHEN OverdueBy BETWEEN 30 AND 39
            THEN '30-39'
            ELSE 'Other'
        END) OverdueRange
    FROM invoices
    WHERE OverdueBy >= 30) t1
GROUP BY OverdueRange