我想使用Squeel重构下面的查询.我想这样做,以便我可以链接其中的运算符并重新使用查询的不同部分中的逻辑.
User.find_by_sql("SELECT
users.*,
users.computed_metric,
users.age_in_seconds,
( users.computed_metric / age_in_seconds) as compound_computed_metric
from
(
select
users.*,
(users.id *2 ) as computed_metric,
(extract(epoch from now()) - extract(epoch from users.created_at) ) as age_in_seconds
from users
) as users")
Run Code Online (Sandbox Code Playgroud)
查询必须全部在DB中运行,并且不应该是混合Ruby解决方案,因为它必须对数百万条记录进行排序和切片.
我已经设置了问题,因此它应该针对普通user表运行,以便您可以使用它的替代方案.
User具有所有常规属性的对象extra_metric_we_care_about,age_in_seconds和compound_computed_metric下面的解决方案不起作用,但它显示了我希望实现的优雅类型
class User < ActiveRecord::Base
# this doesn't work - it just illustrates what I want to achieve
def self.w_all_additional_metrics
select{ ['*',
computed_metric,
age_in_seconds,
(computed_metric …Run Code Online (Sandbox Code Playgroud) 我和#1895500有同样的问题,但PostgreSQL不是MySQL.
如何定义具有计算字段的视图,例如:
(mytable.col1 * 2) AS times_two
Run Code Online (Sandbox Code Playgroud)
...并创建另一个基于第一个的计算字段:
(times_two * 2) AS times_four
Run Code Online (Sandbox Code Playgroud)
...?
我在 Doctrine 中有以下 SELECT 子句(查询本身是使用查询构建器创建的):
u.username,
MAX(p.score) as highscore,
SUM(pc.badgeCount) as badgeCount,
(SUM(pc.badgeCount) / :badgeSum) AS probability,
(-LOG(RAND()) * probability) as weight
Run Code Online (Sandbox Code Playgroud)
(p是主实体的别名,pc是连接实体)
这给了我来自 MySQL 的错误消息:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'probability' in 'field list'
Run Code Online (Sandbox Code Playgroud)
如何在同一个 SELECT 子句中重用创建的别名?
我有一个查询,它的select语句是这样的:
select Greatest(p.price,0) as newprice, sum(q.qty) as qty
from ....
Run Code Online (Sandbox Code Playgroud)
它给了我:
newprice qty
10 1
0 1
100 2
1 2
Run Code Online (Sandbox Code Playgroud)
我想将newprice与qty相乘得到:
newprice qty result
10 1 10
0 1 0
100 2 200
1 2 2
Run Code Online (Sandbox Code Playgroud)
当我尝试这样做select Greatest(p.price,0) as newprice, sum(q.qty) as qty, newprice * qty时说
ERROR: column "newprice" does not exist
Run Code Online (Sandbox Code Playgroud)
我真的不需要这个额外的专栏.
我真正想要的是:SUM(Greatest(p.price,0) * SUM(q.qty)) 它应该给出价值,212但它说ERROR: aggregate function calls cannot be nested
基本上我只需要将两列相乘并对结果求和.我知道我可以使用类似于此处所示的CTE ,但我想知道是否有更简单的方法来减少代码.
我目前有一个查询,其工作方式如下:
select AVG(t2 - t1) as delay,
percentile_cont(0.25) within group (order by (t2 - t1)) as q25,
percentile_cont(0.5) within group (order by (t2 - t1)) as median,
percentile_cont(0.75) within group (order by (t2 - t1)) as q75,
p.bool1,
p.cat1
from people p
group by p.bool1, p.cat1
order by p.cat1,p.bool1
Run Code Online (Sandbox Code Playgroud)
但是,我在 postgres 函数聚合页面上阅读: https://www.postgresql.org/docs/9.4/functions-aggregate.html
我应该能够指定多个分位数:
percentile_cont(fractions) WITHIN GROUP (ORDER BY sort_expression) double precision[] double precision or interval array of sort expression's type multiple continuous percentile: returns an array of results matching …Run Code Online (Sandbox Code Playgroud) 我想从字符串中消除一些文本模式,我的字符串有一个管道分隔符,并且参数并不总是相互跟随。
这是我的字符串
TType=SEND|Status=OK|URL=min://j?_a=3&ver=1.1|day=3
Run Code Online (Sandbox Code Playgroud)
我想消除TType=SEND和URL=min://j?_a=3&ver=1.1
因此我的最终结果应该是
Status=OK|day=3
Run Code Online (Sandbox Code Playgroud)
我尝试过的。不适 用于 postgresql。
select REGEXP_REPLACE('TType=SEND|Status=OK|URL=min://j?_a=3&ver=1.1|day=3',
'(TType=.*?(\||$))|(URL=.*?(\||$))', '')
Run Code Online (Sandbox Code Playgroud) postgresql ×5
sql ×3
activerecord ×1
arel ×1
doctrine ×1
doctrine-orm ×1
mysql ×1
php ×1
quantile ×1
regex ×1
replace ×1
sql-view ×1
squeel ×1
unpivot ×1