- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下情况,我需要解决在列内显示数据的问题,对于串联字符串的每个数据部分。
我对此比较陌生,可悲的是我无法将其他已经阅读过的例子转移到我目前的情况中。我已经阅读了一些关于交叉表和数据透视表的内容,但我只有一列包含该数据,而且我不知道该列内有多少逗号分隔值,所以它无论如何都需要是动态的,我不知道如何解决这个问题。
假设我们有一个这样的 postgres 表(Amawon Aws 上的版本 9.6.3 - 遗憾的是它们不支持数组之类的东西):
CREATE TABLE touchpoints (
customer_id varchar(50) PRIMARY KEY,
purchase_timestamp date,
touchpoints_till_purchase text,
purchase_quantity int,
purchase_value int);
让我们用数据填充它,就像我在 View 中一样:
INSERT INTO touchpoints (customer_id, purchase_timestamp, touchpoints_till_purchase, purchase_quantity, purchase_value)
VALUES ('testuser0@example.com', 'Jun 01, 2018 11:12', 'SEO,Direct', 2, 180),('testuser1@example.com', 'Jun 03, 2018 15:56', 'Direct,Facebook_Paid,SEO', 1, 100),
('testuser2@example.com', 'Jun 04, 2018 21:44', 'Direct,Direct,Direct,Direct,Direct,Direct,Direct,Direct', 3, 170),('testuser3@example.com', 'Jun 06, 2018 15:42', 'Direct,SEO,SEO,Direct', 5, 270),
('testuser4@example.com', 'Jun 06, 2018 15:42', 'Direct,Direct,Direct,Direct,Direct,Direct,Direct', 5, 270);
创建后的表应该是这样的,这也是我实际表的样子。
实际情况:
所以我正在尝试设置一个查询或函数,将“touchpoints_till_purchase”字段内的值拆分为单独的列,就像在数据透视表中一样。
我想要建立的是这样的东西:
预期结果:
应该没有我想的那么难,但我无法用我在 PostgreSQL 的实际低技能来解决它。我观察和尝试的所有解决方案都没有成功,或者我猜我用错了它们。
预期查询应牢记以下几点:
就我尝试解决这么多小时但没有找到解决方案而言,我真的很期待像您这样的 sql 专业人员的帮助。
非常非常感谢!
最佳答案
一种应该可行的方法是使用子字符串的正则表达式版本,但它并不漂亮。
SELECT customer_id,purchase_timestamp
,substring (touchpoints_till_purchase from '^([^,]*)') as touchpoint1
,substring (touchpoints_till_purchase from '^(?:(?:[^,]*),){1}([^,]*)') as touchpoint2
,substring (touchpoints_till_purchase from '^(?:(?:[^,]*),){2}([^,]*)') as touchpoint3
,substring (touchpoints_till_purchase from '^(?:(?:[^,]*),){3}([^,]*)') as touchpoint4
,substring (touchpoints_till_purchase from '^(?:(?:[^,]*),){4}([^,]*)') as touchpoint5
,substring (touchpoints_till_purchase from '^(?:(?:[^,]*),){5}([^,]*)') as touchpoint6
,substring (touchpoints_till_purchase from '^(?:(?:[^,]*),){6}([^,]*)') as touchpoint7
,substring (touchpoints_till_purchase from '^(?:(?:[^,]*),){7}([^,]*)') as touchpoint8
,purchase_quantity
,purchase_value
FROM touchpoints;
thgis 一个也可能有效。
SELECT customer_id,purchase_timestamp
,substring (touchpoints_till_purchase from '^(?:([^,]*)(,|$)){1}') as touchpoint1
,substring (touchpoints_till_purchase from '^(?:([^,]*)(,|$)){2}') as touchpoint2
,substring (touchpoints_till_purchase from '^(?:([^,]*)(,|$)){3}') as touchpoint3
,substring (touchpoints_till_purchase from '^(?:([^,]*)(,|$)){4}') as touchpoint4
,substring (touchpoints_till_purchase from '^(?:([^,]*)(,|$)){5}') as touchpoint5
,substring (touchpoints_till_purchase from '^(?:([^,]*)(,|$)){6}') as touchpoint6
,substring (touchpoints_till_purchase from '^(?:([^,]*)(,|$)){7}') as touchpoint7
,substring (touchpoints_till_purchase from '^(?:([^,]*)(,|$)){8}') as touchpoint8
,purchase_quantity
,purchase_value
FROM touchpoints;
但是,如果 Amazon 的子字符串使用 8.0 正则表达式引擎,情况会变得更糟。
SELECT customer_id,purchase_timestamp
,substring (substring (touchpoints_till_purchase from '^(([^,]*)(,|$)){1}') from '([^,]*),?$') as touchpoint1
,substring (substring (touchpoints_till_purchase from '^(([^,]*)(,|$)){2}') from '([^,]*),?$') as touchpoint2
,substring (substring (touchpoints_till_purchase from '^(([^,]*)(,|$)){3}') from '([^,]*),?$') as touchpoint3
,substring (substring (touchpoints_till_purchase from '^(([^,]*)(,|$)){4}') from '([^,]*),?$') as touchpoint4
,substring (substring (touchpoints_till_purchase from '^(([^,]*)(,|$)){5}') from '([^,]*),?$') as touchpoint5
,substring (substring (touchpoints_till_purchase from '^(([^,]*)(,|$)){6}') from '([^,]*),?$') as touchpoint6
,substring (substring (touchpoints_till_purchase from '^(([^,]*)(,|$)){7}') from '([^,]*),?$') as touchpoint7
,substring (substring (touchpoints_till_purchase from '^(([^,]*)(,|$)){8}') from '([^,]*),?$') as touchpoint8
,purchase_quantity
,purchase_value
FROM touchpoints;
使用数组等不允许的功能的解决方案要简洁得多。
关于postgresql - Postgresql - 将聚合、逗号分隔值拆分为查询内的单独列 - 使用 Amazon Aws 和 PostgreSql 9.6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51583383/
我正在尝试从下拉列表中创建一个多选复选框,并通过 ; 连接所选结果。 我的代码是这样的: var myobject = { ValueA : 'Text A', ValueB : 'T
我有输入,我需要获取值并用 “|” 符号分隔。 我的输入: 输出我需要的: 00:00|00:00|00:00 我的代码是: (而且它不工作) var timesArray = $('table').
我正在尝试将超过 400 万行的列拆分为 4 个新列,问题是我不知道在哪里查看或我应该使用 Google 搜索哪个术语。 (是的,我已经在 Google 和 Stack 中搜索了一个类似的问题,但只在
我有一个很大的 csv 文件,其中充满了用“|”分隔的数字字符,例如: 432452 | 543634 4122442 | 41256512 64523 | 12416 然后我读入数据如下: fs
我有一个程序可以计算多个数字的阶乘。这些数字在 cmd 中作为参数传递: factorial.exe 3 4 5 这将分别计算 3、4 和 5 的阶乘。该程序的早期版本有一个百分比显示堆栈的完整性。我
这个问题已经有答案了: 奥 git _a (6 个回答) 已关闭 9 年前。 我有一个双数“547.123456” 我只想使用这个 double 作为“547.1”,就像“.”后面只有 1 个数字 我
我有一个程序可以计算多个数字的阶乘。这些数字在 cmd 中作为参数传递: factorial.exe 3 4 5 这将分别计算 3、4 和 5 的阶乘。该程序的早期版本有一个百分比显示堆栈的完整性。我
我有一个 ArrayList,其中包含一个 messageId、一个 -、一个用户名。 示例:E123-sam 我想划分 List 的每个元素,使得 - 之前的部分进入一个 ArrayList ,而之
我目前有一个“日期”列作为 pandas 数据框的索引,其格式为: January February .... Year2 January February ... Year3 (它来自 pdf 表格
我正在尝试对我的 .mdb 数据库进行 ODBC 查询。我正在使用 mdbtools 驱动程序。该代码是使用 Eclipse 用 C 语言编写的。唯一的问题是,当我写例如: "SELECT 'last
我需要知道如何将这两个if else 部分分开。 public static int NextBday(int Bdays, int days){ int daysleft = 0;
我想计算我的员工分开但合并在一起的出勤率 My target output 我的代码 SELECT count(employees_id) as numbers FROM attendance WH
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 7 年前。 Improve t
下面是我的代码的一小段摘录,它从 API 添加了一行。总共有很多行。 每一行包含一行数据如 TY8tr,50,34,P,SB, 数据行在数据类型上是一致的。我如何通过 ,'s 拆分数组列表中
我想获取选中的元素并用逗号分隔它们,最后一个元素用“and”分隔 它的显示输出为: 我想删除最后一个元素后面的逗号 (,) 并在它前面添加 'and',例如 Sugar, Milk and Extra
我是 JSON 的新手,但在从已解析的 JSON 对象中提取数据时遇到问题: 我有一个 getstats.php 文件,它回显 mysql 查询的 json 编码结果。以下是 php 文件返回的示例:
我有一个像这样的数字/字符串(我不确定如何将 int 与字符串相互转换) 000000122310200000223340000700012220000011411000000011011271043
标签内的文本
我想尝试一些基本的网络抓取,但遇到了一个问题,因为我习惯了简单的 td-tags,在这种情况下,我有一个网页,其中包含以下预标记和其中的所有文本,这意味着刮掉它有点棘手。 11111111 1111
出于练习目的,我正在开发 TCP 客户端/服务器系统,我想在两者之间发送特定数据。 我已经能够发送字节并让它们显示为字符串。此外,我还可以发送一个特定的字符串(“mb”)并在服务器端弹出一个 Mess
在大量的 unicode 字符中,有一些实际上表示多个字符,例如两个 'f' 字符的 U+FB00 连字 ff。有什么方法可以轻松地将这样的字符转换为多个单个字符?最好是标准 Java API 中可用
我是一名优秀的程序员,十分优秀!