sql - 从多行中创建一个连接值
问题描述
请参阅下面的可重现示例。
需要做什么
请参阅代码中的注释:使用 '2021-01-01'(column: Date) 和 'A'(column: Tote) 创建唯一的手提包,同时从 'hat'(column: content) 和 'cat'(column : 内容)按字母顺序排列。
可重现的例子:
CREATE OR REPLACE TABLE
`first_table` (
`Date` string NOT NULL,
`TotearrivalTimestamp` string NOT NULL,
`Tote` string NOT NULL,
`content` string NOT NULL,
`location` string NOT NULL,
);
INSERT INTO `first_table` (`Date`, `TotearrivalTimestamp`, `Tote`, `content`, `location`) VALUES
('2021-01-01', '13:00','A','hat','1'), #first tote
('2021-01-01', '13:00','A','cat','1'), #first tote
('2021-01-01', '14:00', 'B', 'toy', '1'),
('2021-01-01', '14:00', 'B', 'cat', '1'),
('2021-01-01', '15:00', 'A', 'toy', '1'),
('2021-01-01', '13:00', 'A', 'toy', '1'),
('2021-01-02', '13:00', 'A', 'hat', '1'),
('2021-01-02', '13:00', 'A', 'cat', '1');
CREATE OR REPLACE TABLE
`desired_result` (
`Date` string NOT NULL,
`Tote` string NOT NULL,
`TotearrivalTimestamp` string NOT NULL,
`content_combination` string NOT NULL,
);
INSERT INTO `desired_result` (`Date`, `Tote`, `TotearrivalTimestamp`, `content_combination`) VALUES
('2021-01-01', 'A', '13:00', 'cathat'), #first tote
('2021-01-01', 'B', '14:00', 'cattoy'),
('2021-01-01', 'A', '15:00', 'toy'),
('2021-01-02', 'A', '13:00', 'cathattoy');
解决方案
STRING_AGG()
成功了 - 为我工作
推荐阅读
- python - 收到错误“NameError:名称'Tk'未定义”
- python - 如何将参数传递给 django-rest-framework 中的序列化器?
- c# - 使用 LINQ 表达式生成具有相关数据和条件的查询
- laravel - Laravel - 从同一控制器中的另一个方法调用控制器方法(使用自定义表单请求验证)
- php - PHPMailer 不发送任何邮件
- ruby - 如何使用 Ruby 在命令提示符中打印 unicode 字符
- c# - 使用 RestSharp 读取响应
- java - 为什么我的表单没有在 Spring Boot 中传递信息
- android - 无法使用 Android Studio 中的 Firebase 助手将应用连接到 Firebase
- python - 代码优化解析列和创建日期时间