sql - 在oracle中将行值添加到列
解决方案
你可以做这样的事情。
Select * from (select id, person_id, f_device, report_date, originaldate)
pivot
(
count(*)
for person_id in (1, 2, 3, 4)
);
那就是您需要聚合,您将获得 select 语句和 for 子句中的列。此查询将计算 (1, 2, 3, 4) 中 person_id 的行数,并在内部按剩余列分组。
由于您的问题不清楚,如果不是这种情况,请尝试改进您的问题。
更新
WITH cte as ( select personid id, reportdate c_date, listagg(originaldate||' ') og FROM my_table GROUP BY personid, reportdate),
cte2 as (SELECT id, c_date, regexp_substr(og, '(.*?)( |$)', 1, 1 ) one,
regexp_substr(og, '(.*?)( |$)', 1, 2) two,
regexp_substr(og, '(.*?)( |$)', 1, 3 ) three,
regexp_substr(og, '(.*?)( |$)', 1, 4 ) four
from cte)
SELECT * FROM cte2;
如果您希望每行都有一个不同的personid,则需要汇总reportdate
WITH cte as ( select personid id, max(reportdate ) c_date, listagg(originaldate||' ') WITHIN GROUP(ORDER BY personid ) og FROM my_table GROUP BY personid),
cte2 as (SELECT id, c_date, regexp_substr(og, '(.*?)( |$)', 1, 1 ) one,
regexp_substr(og, '(.*?)( |$)', 1, 2) two,
regexp_substr(og, '(.*?)( |$)', 1, 3 ) three,
regexp_substr(og, '(.*?)( |$)', 1, 4 ) four
from cte)
SELECT * FROM cte2;
推荐阅读
- ios - React Native Ios 错误:权限被拒绝,mkdir
- google-sheets - 我如何有条件地格式化一整列
- bash - Bash - 从一个文件列表创建多个列表文件
- mysql - STR_TO_DATE 返回两位数年份的未来日期
- python - 为什么这个带有线程的 Popen 不起作用?
- php - MYSQL echo 显示十进制和非十进制
- ios - 我想在 iOS 12 上获得其他应用程序的屏幕时间,谁能告诉我使用哪个私有 API
- docker - docker-compose 动态端口分配并检索端口
- powershell - Powershell 找不到类型 [Microsoft.Office.Interop.Word.WdSaveFormat]
- python-3.x - 在 Python 中使用 Stanford Tregex 处理德语文本