select语句中的SQL Increment列值

时间:2015-07-22 08:49:35

标签: sql postgresql amazon-redshift

我试图写一个increments语句,将Select count(order_id) as order_count, case when revenue between 0 and 50 then ‘$50’ when order_value between 51 and 100 then ‘$100’ else ‘over $101’ end as revenue_bucket from Orders group by 2 值增加50,但范围最终可能达到200,000,所以我无法在案例陈述。

与此类似,但不是手动编写class PictureGLWidget : public QGLWidget { //.. }

QGridLayout* layout = new QGridLayout;
layout->addWidget(myPictureGLWidget, 0, 1);
ui->verticalLayout_5->addLayout(layout);

4 个答案:

答案 0 :(得分:1)

您可以使用modulo来获取此信息。在你的例子中限制为101。您所要做的就是将结果转换为字符串并在其前面添加$

Select count(order_id) as order_count, 
    case when revenue < limit then revenue - (revenue % 50) + 50
             else ‘over $101’
  end as revenue_bucket
from Orders
group by 2

答案 1 :(得分:1)

将您的revenue转换为存储桶值,然后将其中的字符串输出:

SELECT count(order_id) AS order_count, 
        '$' || ((((revenue - 0.01)/50)::int + 1) * 50)::text AS revenue_bucket
FROM Orders
GROUP BY 2;

这显然超过了200,000美元。

答案 2 :(得分:0)

您可以使用div(整数除法)舍入到最接近的50:

revenue div 50 * 50

向上舍入而不是向下:

(revenue div 50 + 1) * 50

要在下一个括号中包含50(50为$ 50而不是$ 100):

((revenue-1) div 50 + 1) * 50

示例查询:

select  revenue
,       concat('$', ((revenue-1) div 50 + 1) * 50) as revenue_bucket
from    YourTable

See it working at SQL Fiddle.

答案 3 :(得分:0)

想出类似的东西

{% static 'project/css/style.css' %}