- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
上下文
我正在为一个站点运行车辆路径问题的变体。布局是这样的。您有取货点、卡车和正在取货的元素。真正的诀窍是客户一天的轮类次数不同,因此这是可定制的。所以我会在运行时知道这一点,但不会提前。我正在尝试创建查询,该查询返回一个结果,其中包含每个公交车站的行、每个类次的列和每个项目 ID 的 json 数组,如下所示:
id | address | city | postalcode | shift1 | shift2 | shift3
-----+------------------+-----------+------------+----------+--------+--------
220 | 1471 N Lymestone | Anytown | 12345 | [14, 16] | [12] | [14]
221 | 108 D Ave | Anytown | 12345 | [15, 17] | | [15,16]
222 | 1434 Bryan Ave | Anytown | 12345 | [16] | [1,19] |
表结构
我这里有三个相关的表; stops
表仅包含停靠点的地理位置,items
表包含取件元素的 ID,shifts
表包含类次和itemstopassignemnt
表(请原谅命名,我继承了它)有一个 stop_id
shift_id
和 item_id
分配一个在那个类次的那个站点要取的元素。
查询
所以在 SO 上四处寻找之后,我提出了以下查询:
SELECT * FROM crosstab(
$$SELECT stop_id,
'shift' || shift_id,
json_agg(item_id)
FROM itemstopassignemnt
GROUP BY stop_id, shift_id
ORDER BY 1,2$$
) AS (stop_id INTEGER, shift1 JSON, shift2 JSON, shift3 JSON);
这适用于三类倒的情况。我可以将其放入以编程方式生成 sql 的函数中,以便它为用户添加的许多类次动态构建它。我认为该函数必须返回 setof record
或 table()
。这是我的问题:
最佳答案
select
stop_id,
jsonb_object_agg('shift' || shift_id, items) as shift_items
from (
select
stop_id,
shift_id,
coalesce (
array_agg (item_id) filter (where item_id is not null),
array[]::int[]
) as items
from
itemstopassignemnt
right join
(
select shift_id, stop_id
from
(select distinct shift_id from itemStopAssignemnt) a
cross join
(select distinct stop_id from itemStopAssignemnt) b
) r using (stop_id, shift_id)
group by 1,2
) s
group by 1
;
stop_id | shift_items
---------+--------------------------------------------------------
220 | {"shift1": [14, 16], "shift2": [12], "shift3": [14]}
221 | {"shift1": [15, 17], "shift2": [], "shift3": [15, 16]}
222 | {"shift1": [16], "shift2": [1, 9], "shift3": []}
我没有将它加入 stops
来缩短它,但应该是微不足道的。
关于postgresql - 用于可变列数查询的 Postgres CrossTab,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43604132/
我有来自 Excel 工作表的数据,我在 Pandas 交叉表中进行了汇总。我想通过对相关行求和来进一步对数据进行分类。 这是我的交叉表: class_of_orbit Ellipti
我是 stackoverflow 的新手,我在尝试做这个主题时遇到了一些问题。报告。 表格示例 Group Client AtributeGroup1 Client1 Atribute1Gro
我在 MySQL 中有一个表,其中记录了学生注册的每一年的信息。我想生成一个查询,显示 yyyy 出生的 nnn 名学生,学年结束为 aaaa。我很快就完成了该查询,但令我困惑的是数据库中有一些重复的
我在 MySQL 中设置了一个查询,以返回结果,其中月份作为列标题,培训师姓名作为行标题。我正在尝试按月确定培训师的 NPS 分数。当我运行查询时,它仅显示上个月有调查结果的结果。如何修复查询以显示他
我在 postgres 中使用交叉表函数。基本 SQL 是: select distinct o_location, co_name, o_date,
两部分问题: 在 MySql 中生成交叉表结果时,Distinct 调用似乎不起作用...或者我遗漏了其他内容?我在几列中得到相同的 ClassName 。即“MDC(简介)”。 : SQLFiddl
如果有人能解决这个问题,那对我来说将是一个很大的帮助。我有一个 MySql 数据库表,其中包含以下记录: student_id subject_id scores s01
我有一个带有静态参数的典型交叉表查询。它适用于 createStatement。我想使用 preparestatement 来查询。 String query = "SELECT * FROM cro
这是我的输入: 身份证颜色 1个绿色 1 红色 1个橙子 1个绿色 1 红色 2红 2红 2 蓝色 3绿色 3红 这是我在输出中想要的 - 每种颜色的 ID 记录计数: ID绿红橙蓝 1 2 2 1
我正在寻求帮助,使用类似于 sumproduct 的函数从现有数据帧创建子数据帧。我想使用列标题将此表转换为一个小总和乘积: dan ste bob t1 0 2 0 t2 2
我正在尝试在 Postgres 中旋转一个表。我的表“示例”有很多列(代码、部门、项目、年份、期间、值、预购),而不是每行有 1 个概念、1 年、1 个值,我想要按年显示值的概念。从此; Item
我有一个 crosstab()查询类似于我上一个问题中的查询: Unexpected effect of filtering on result from crosstab() query 常见的情况
我有一个 crosstab() 查询,如下所示: SELECT * FROM crosstab( 'SELECT row_name, extra1, extra2..., another_table
我不能在这里发布我的原始查询,它太复杂了。所以我用一个示例查询来解释我的问题: 工作枢轴查询: Select pivot.* from public.crosstab($$ select 'na' a
上下文 我正在为一个站点运行车辆路径问题的变体。布局是这样的。您有取货点、卡车和正在取货的元素。真正的诀窍是客户一天的轮类次数不同,因此这是可定制的。所以我会在运行时知道这一点,但不会提前。我正在尝试
我有下表sample_events: Column | Type --------+----- title | text date | date 具有值: title | dat
在我的项目中,数据模型发生了变化,我必须执行一个新的请求来获取信息,但在过去看起来所有数据都在同一行中,但现在部分数据在另一个表中,它返回我的专栏。 所以我的请求返回: 我认为这很简单,但我真的不明白
早上好,我尝试使用 mysql/php 创建报告失败,我希望将行作为类似于 Access Crosstab 或 Excel Pivot 的列。我有销售人员和月销售额,我想在整个页面上显示,月份作为列标
我目前真的在为数据框而苦苦挣扎。通过运行代码:(res_sum = 数据框的名称) summary_table = pd.crosstab(index=[res_sum["Type"],res_sum
我正在用 pandas 制作一些交叉表: a = np.array(['foo', 'foo', 'foo', 'bar', 'bar', 'foo', 'foo'], dtype=object) b
我是一名优秀的程序员,十分优秀!