为了对这些记录进行一些更改,我尝试按外键对一个大表进行分组。
我想做的是group(:foreign_key_id)并返回这个组中的ids。所以我会得到:{foreign_key_id => [array of ID's with this key], {foreign_key_id => [array of ID's with this key] }
也许我应该映射(&:id)值,但是这个函数位于一个where之后,所以返回的不是这个外键的所有记录。
我的工作代码应该是这样的:
SubscriptionTag.where(subscription_id:
我像这样质疑过:
SELECT
LAG(col, 1) OVER(w) AS c_1,
LAG(col, 2) OVER(w) AS c_2,
...
LAG(col, 12) OVER(w) AS c_12,
other_col,
FROM table
WINDOW w AS (...)
所以很多代码复制都是为了延迟。在12-12个月的时间里,这几乎是难以控制的,但是365天又如何呢?如果有像LAG(col, [1,2,..,365]) OVER(w) AS c_1, c_2, ..., c_365这样的构造是很好的
是否有特定的标准或供应商(,但也可以是ohter)来处理这个问题
在我的Postgresql数据库中,我有多个引用数据表。示例:
- Gender(id:Serial, name:varchar)
- Sexuality(id:Serial, name:varchar)
- Location(id:Serial,name:varchar)
为了检索所有表中的所有信息,我执行了3个单独的select语句。示例:
- "SELECT name from Gender;
- "SELECT name from Sexuality;
- "SELECT name from Location;
如何将其放入一个调用中,以便返回一行,
在postgresql中,我很难用数组agreggate值创建聚合查询,但希望将其中一个值放入自己的列中。
我有一个像这样的语言翻译表,我们把它叫做component_translation
id, component_code, language_code, name
1 abc en tyre
2 abc de reifen
3 abc es llanta
4 def en rim
5
假设这样的源表:
Year Long Lat Values
========================================
2015 8 1 12
2015 8 1 43
2015 8 1 11
...
命运桌应该是这样的:
Year Long Lat Field1 Field2 Fiel
当按多个值排序时,AWS Athena的array_agg可能会返回错误的顺序。 这是最小复印件 with xxx(id, sort_by, val) as (
values
(1, 'a' , 999),
(1, 'b', 555)
)
select
id,
array_agg(val order by sort_by) as single_sort, -- [999, 555]
array_agg(val order by sort_by, val) as dual_sort -- [
在以下请求中:
SELECT
myid,
min(name) AS name,
array_agg(array[zip, city])) AS city
FROM my_table
WHERE name LIKE 'Doe'
GROUP BY myid
LIMIT 10
我得到以下结果:
+-------+------+-------------------+
| myid | name | city |
+-------+------+-------------------+
| A123 | Doe | {{69,"Gro
我正在尝试运行以下查询(我发表了一些到目前为止尝试过的内容):
WITH
res AS (
SELECT * ...
), -- res has a column of integers called "node". I need to transform this column into an array to use it in viapath below
nodes AS (
-- SELECT ARRAY[node] FROM res
-- SELECT array_agg(node) FROM res
),
viapath AS (
SELEC
我需要对所有按值分组的对象执行BITWISE_OR。
假设我在group by中的值是: 100、010和110。我需要计算"100、010或110“,即110。
所以我想这样做:select col1, array_agg(col2) from table group by col1,然后对数组中的所有col2值应用bitwise_or。有一个函数bitwise_or_agg,但它在数组上不起作用。
如有任何帮助,我们不胜感激!
你能帮我做个group by查询吗?我有一个类似下面的表格
Country State SomeType
UK England Yes
UK Wales No
UK Scotland MayBe
Germany Bavaria Yes
Germany Bremen Yes
我需要标记每个国家为是,否,MayBe或混合类型。
如果所有州均为是,则该国家将为是;全部为否,则该国家将为否;如果所有州均为MayBe,则该国家将为混合;但如果不同州具有不同的SomeType,则应将其标记为混合
在上面的例子中,我应该得到如下输出
我有一些数据,对于每个ID,我希望将两个或更多字段聚合到一个数组中,并且希望它们按顺序匹配。
例如,如果我有以下数据:
我想把它变成这样:
或者,类似这样的代码也可以:
因此,首先,如果我使用这样的查询,它会执行我想要的操作,还是不能保证两个字段以相同的顺序完成(即Value_1和Value_2中的相应值可能不匹配)?
SELECT
ID,
ARRAY_AGG (
Value_1
) AS Value_1,
ARRAY_AGG (
Value_2
) AS Value_2
FROM
table
GROUP BY
ID
我有一个名为jobs的表:
id PK Serial
model String
model_id UNSIGNED Integer
status String
created_at timestamp
prosessed_at timestamp
并在一个数组中记录每个状态:
select
model_id,
model,
array_agg(id) as jobs,
array_agg(statuses) as statuses
from jobs
group by model,model_id
我使用它作为子查询,以检测已修复的作业中的任何故障:
select
*
from (
下面是postgresql中的表格:
id | grade | comment
--------------------
01 | A | Good
01 | B | OK
01 | C | BAD
02 | A | Good
02 | E | Very BAD
02 | D | BAD
我想以以下格式获取记录
id | grade1 | grade2 | grade3 | comment1 | comment2 | comment3
------------------------------------------------------
我在创建JSON对象时遇到了一些困难,其中对象的键是我在Postgres中聚合行的值。
这是我正在工作的桌子:
create table if not exists safety_training_options (
id serial primary key,
option_type text not null,
name text not null
)
一些样本数据:
insert into safety_training_options (option_type, name)
values ('category', 'General Industry&