- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是一个sqlFiddle这显示了我正在尝试做的事情。
这是@lad2025 sqlFiddle这表明它更好
我的表上有两个索引,外加一个包含列名的列和一个包含值的列。
在 fiddle 中,我展示了一个执行我想执行的操作的查询。但它很慢。
我有一个交叉表请求,它做几乎相同的事情,速度非常快,但几乎没有错误。 (它会融合一些线)
SELECT
end_user_id,
tms,
coalesce(max(IN_VEHICLE), 0) as IN_VEHICLE,
coalesce(max(ON_BICYCLE), 0) as ON_BICYCLE,
coalesce(max(ON_FOOT), 0) as ON_FOOT,
coalesce(max(RUNNING), 0) as RUNNING,
coalesce(max(STILL), 0) as STILL,
coalesce(max(TILTING), 0) as TILTING,
coalesce(max(UNKNOWN), 0) as UNKNOWN,
coalesce(max(WALKING), 0) as WALKING
FROM
crosstab (
'SELECT end_user_id, tms, type, max(confidence) FROM activities group by 1,2,3 ',
'SELECT DISTINCT type FROM activities order by type'
)as newtable (
end_user_id text,
tms timestamp,
IN_VEHICLE float,
ON_BICYCLE float,
ON_FOOT float,
RUNNING float,
STILL float,
TILTING float,
UNKNOWN float,
WALKING float
)
GROUP BY end_user_id, tms
ORDER BY end_user_id, tms
我不知道为什么 postgres 要求我 GROUP BY end_user_id, tms 在最后......它应该是唯一的。
我也不知道为什么,但如果我不在交叉表查询中分组,每个 end_user_id 将只有一行 :(
如何更正该交叉表请求?
编辑:@lad2025 的响应比我的更好,更优雅,而且我敢肯定更快。尽管如此,我还是想知道如何使用交叉表来做到这一点。
最佳答案
您可以避免像 Fiddle 中那样的交叉表/多个左联接并使用简单的条件聚合:
SELECT
end_user_id,
tms,
COALESCE(MAX(CASE WHEN type = 'IN_VEHICLE' THEN confidence END),0) AS IN_VEHICLE,
COALESCE(MAX(CASE WHEN type = 'ON_BICYCLE' THEN confidence END),0) AS ON_BICYCLE,
COALESCE(MAX(CASE WHEN type = 'ON_FOOT' THEN confidence END),0) AS ON_FOOT,
COALESCE(MAX(CASE WHEN type = 'RUNNING' THEN confidence END),0) AS RUNNING,
COALESCE(MAX(CASE WHEN type = 'STILL' THEN confidence END),0) AS STILL,
COALESCE(MAX(CASE WHEN type = 'TILTING' THEN confidence END),0) AS TILTING,
COALESCE(MAX(CASE WHEN type = 'UNKNOWN' THEN confidence END),0) AS UNKNOWN,
COALESCE(MAX(CASE WHEN type = 'WALKING' THEN confidence END),0) AS WALKING
FROM activities
GROUP BY end_user_id, tms
ORDER BY end_user_id, tms;
输出:
╔═══════════════════╦════════════════════════════╦═════════════╦═════════════╦══════════╦══════════╦════════╦══════════╦══════════╦═════════╗
║ end_user_id ║ tms ║ in_vehicle ║ on_bicycle ║ on_foot ║ running ║ still ║ tilting ║ unknown ║ walking ║
╠═══════════════════╬════════════════════════════╬═════════════╬═════════════╬══════════╬══════════╬════════╬══════════╬══════════╬═════════╣
║ 64e8394876a5b7f1 ║ October, 28 2015 08:24:20 ║ 21 ║ 8 ║ 2 ║ 0 ║ 2 ║ 0 ║ 68 ║ 2 ║
║ 64e8394876a5b7f1 ║ October, 28 2015 08:24:41 ║ 15 ║ 0 ║ 3 ║ 0 ║ 72 ║ 0 ║ 10 ║ 3 ║
║ 64e8394876a5b7f1 ║ October, 28 2015 08:25:17 ║ 5 ║ 0 ║ 5 ║ 0 ║ 77 ║ 100 ║ 13 ║ 5 ║
║ 64e8394876a5b7f1 ║ October, 28 2015 08:25:32 ║ 0 ║ 0 ║ 0 ║ 0 ║ 100 ║ 0 ║ 0 ║ 0 ║
║ 64e8394876a5b7f1 ║ October, 28 2015 08:25:36 ║ 0 ║ 0 ║ 0 ║ 0 ║ 92 ║ 0 ║ 8 ║ 0 ║
║ 64e8394876a5b7f1 ║ October, 28 2015 08:27:24 ║ 48 ║ 48 ║ 0 ║ 0 ║ 0 ║ 0 ║ 5 ║ 0 ║
║ 64e8394876a5b7f1 ║ October, 28 2015 08:27:54 ║ 0 ║ 0 ║ 0 ║ 0 ║ 0 ║ 100 ║ 0 ║ 0 ║
║ 64e8394876a5b7f1 ║ October, 28 2015 08:28:11 ║ 62 ║ 8 ║ 3 ║ 0 ║ 15 ║ 0 ║ 13 ║ 3 ║
║ 64e8394876a5b7f1 ║ October, 28 2015 08:28:53 ║ 35 ║ 0 ║ 6 ║ 0 ║ 37 ║ 0 ║ 23 ║ 6 ║
║ 64e8394876a5b7f1 ║ October, 28 2015 08:29:16 ║ 54 ║ 2 ║ 0 ║ 0 ║ 10 ║ 0 ║ 35 ║ 0 ║
║ e86b0b91546194cc ║ October, 28 2015 08:24:41 ║ 13 ║ 13 ║ 69 ║ 3 ║ 0 ║ 100 ║ 5 ║ 67 ║
║ e86b0b91546194cc ║ October, 28 2015 08:33:33 ║ 0 ║ 0 ║ 100 ║ 0 ║ 0 ║ 0 ║ 0 ║ 100 ║
║ e86b0b91546194cc ║ October, 28 2015 08:33:38 ║ 0 ║ 0 ║ 100 ║ 0 ║ 0 ║ 0 ║ 0 ║ 100 ║
║ e86b0b91546194cc ║ October, 28 2015 08:34:06 ║ 19 ║ 6 ║ 31 ║ 2 ║ 29 ║ 0 ║ 16 ║ 29 ║
║ e86b0b91546194cc ║ October, 28 2015 08:34:34 ║ 3 ║ 0 ║ 0 ║ 0 ║ 95 ║ 0 ║ 3 ║ 0 ║
╚═══════════════════╩════════════════════════════╩═════════════╩═════════════╩══════════╩══════════╩════════╩══════════╩══════════╩═════════╝
COALESCE
也是多余的(如果只允许正/零值):
SELECT
end_user_id,
tms,
MAX(CASE WHEN type = 'IN_VEHICLE' THEN confidence ELSE 0 END) AS IN_VEHICLE,
MAX(CASE WHEN type = 'ON_BICYCLE' THEN confidence ELSE 0 END) AS ON_BICYCLE,
MAX(CASE WHEN type = 'ON_FOOT' THEN confidence ELSE 0 END) AS ON_FOOT,
MAX(CASE WHEN type = 'RUNNING' THEN confidence ELSE 0 END) AS RUNNING,
MAX(CASE WHEN type = 'STILL' THEN confidence ELSE 0 END) AS STILL,
MAX(CASE WHEN type = 'TILTING' THEN confidence ELSE 0 END) AS TILTING,
MAX(CASE WHEN type = 'UNKNOWN' THEN confidence ELSE 0 END) AS UNKNOWN,
MAX(CASE WHEN type = 'WALKING' THEN confidence ELSE 0 END) AS WALKING
FROM activities
GROUP BY end_user_id, tms
ORDER BY end_user_id, tms;
您还可以考虑为 type
列创建查找表,例如 activities_type (type_id, type_name)
而不是直接存储在表字符串 ('IN_VEHICLE', ' ON_BICYCLE', ...)
。
我不是 Postgresql 专家,但经过一些尝试:
SELECT
LEFT(end_user_id, strpos(end_user_id, '_')-1) AS end_user_id,
RIGHT(end_user_id, LENGTH(end_user_id) - strpos(end_user_id, '_'))::timestamp AS tms,
COALESCE(IN_VEHICLE,0) AS IN_VEHICLE,
COALESCE(ON_BICYCLE,0) AS ON_BICYCLE,
COALESCE(ON_FOOT,0) AS ON_FOOT,
COALESCE(RUNNING,0) AS RUNNING,
COALESCE(STILL,0) AS STILL,
COALESCE(TILTING,0) AS TILTING,
COALESCE("UNKNOWN",0) AS "UNKNOWN",
COALESCE(WALKING,0) AS WALKING
FROM crosstab(
'SELECT (end_user_id || ''_'' || tms) AS row_id, type, confidence
FROM activities
ORDER BY row_id, type, confidence',
'SELECT DISTINCT type FROM activities order by type'
) AS newtable (
end_user_id text,
IN_VEHICLE int,
ON_BICYCLE int,
ON_FOOT int,
RUNNING int,
STILL int,
TILTING int,
"UNKNOWN" int,
WALKING int)
ORDER BY end_user_id, tms;
为什么要连接和拆分 end_user_id + tms
?
因为 crosstab(text,text)
需要:
row_id <=> end_user_id + tms
category <=> type
value <=> confidence
请注意,此版本中没有GROUP BY
。
基于 tablefunc module doc F.37.1.4。交叉表(文本,文本)
:
这要好得多,因为它可以处理 row_id, extra_col1, extra_col2, category, value)
。所以现在:
row_id <=> id
extra_col1 <=> end_user_id
extra_col2 <=> tms
...
最后的查询:
SELECT
end_user_id,
tms,
coalesce(max(IN_VEHICLE), 0) as IN_VEHICLE,
coalesce(max(ON_BICYCLE), 0) as ON_BICYCLE,
coalesce(max(ON_FOOT), 0) as ON_FOOT,
coalesce(max(RUNNING), 0) as RUNNING,
coalesce(max(STILL), 0) as STILL,
coalesce(max(TILTING), 0) as TILTING,
coalesce(max("UNKNOWN"), 0) as "UNKNOWN",
coalesce(max(WALKING), 0) as WALKING
FROM crosstab(
'SELECT id,end_user_id , tms, type, confidence
FROM activities',
'SELECT DISTINCT type FROM activities order by type'
) AS newtable (
id INT,
end_user_id text,
tms timestamp,
IN_VEHICLE int,
ON_BICYCLE int,
ON_FOOT int,
RUNNING int,
STILL int,
TILTING int,
"UNKNOWN" int,
WALKING int
)
GROUP BY end_user_id, tms
ORDER BY end_user_id, tms;
What would be the point of the activities_type table ?
数据库规范化,你可以使用:
SELECT DISTINCT type FROM activities order by type
vs
SELECT type_name FROM activities_types ORDER BY type_name;
这个版本使用 id
作为 row_id
所以它仍然需要 GROUP BY
来压缩多行。
总结一下:条件聚合是最具可读性的解决方案。
关于sql - PostgreSQL 9.3 : How to use crosstab with multiple indexes?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34437215/
我有来自 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
我是一名优秀的程序员,十分优秀!