select语句中的SQL增量列值 [英] SQL Increment column value in select statement

查看:122
本文介绍了select语句中的SQL增量列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写 Select 语句,该语句将的值增加50,但是范围可以最终是200,000,所以我不能在case语句中手动完成所有操作。

I'm trying to write a Select statement that increments a column value by 50, but the range can end up being 200,000 so I can't do it all in a case statement manually.

与此类似,但不是手动编写增量

Something similar to this, but instead of manually writing the 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


推荐答案

打开您的收入到存储桶值中,然后从中取出字符串:

Turn your revenue into the bucket value, then make string out of it:

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。

This obviously runs well past $200,000.

这篇关于select语句中的SQL增量列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆