希望获得为 oracle sql 开发人员输出构建的查询
looking to get a query constructed out for oracle sql developer output
现有table
输出查询应该呈现这个
使用GROUP BY
并取每列的MAX
:
SELECT event,
MAX(numericval) AS quantity,
MAX(details) AS status
FROM table_name
GROUP BY event
其中,对于示例数据:
CREATE TABLE table_name (event, elements, numericval, details) AS
SELECT 1, 'quality', 5, NULL FROM DUAL UNION ALL
SELECT 1, 'status', NULL, 'yes' FROM DUAL UNION ALL
SELECT 2, 'quality', 10, NULL FROM DUAL UNION ALL
SELECT 2, 'status', NULL, 'no' FROM DUAL UNION ALL
SELECT 3, 'quality', 15, NULL FROM DUAL UNION ALL
SELECT 3, 'status', NULL, 'yes' FROM DUAL;
输出:
EVENT
QUANTITY
STATUS
1
5
yes
2
10
no
3
15
yes
对于您的更新,您可以使用条件聚合:
SELECT event,
MAX(CASE elements WHEN 'quality' THEN numericval END) AS quantity,
MAX(CASE elements WHEN 'status' THEN details END) AS status,
MAX(CASE elements WHEN 'Lot' THEN numericval END) AS lot
FROM table_name
GROUP BY event
其中更新数据:
CREATE TABLE table_name (event, elements, numericval, details) AS
SELECT 1, 'quality', 5, NULL FROM DUAL UNION ALL
SELECT 1, 'status', NULL, 'yes' FROM DUAL UNION ALL
select 1 , 'Lot',1,null from dual union all
SELECT 2, 'quality', 10, NULL FROM DUAL UNION ALL
SELECT 2, 'status', NULL, 'no' FROM DUAL UNION ALL
select 2 , 'Lot',3,null from dual union all
SELECT 3, 'quality', 15, NULL FROM DUAL UNION ALL
SELECT 3, 'status', NULL, 'yes' FROM DUAL union all
select 3, 'Lot', 4,null from dual;
输出:
EVENT
QUANTITY
STATUS
LOT
1
5
yes
1
2
10
no
3
3
15
yes
4
db<>fiddle here
现有table
输出查询应该呈现这个
使用GROUP BY
并取每列的MAX
:
SELECT event,
MAX(numericval) AS quantity,
MAX(details) AS status
FROM table_name
GROUP BY event
其中,对于示例数据:
CREATE TABLE table_name (event, elements, numericval, details) AS
SELECT 1, 'quality', 5, NULL FROM DUAL UNION ALL
SELECT 1, 'status', NULL, 'yes' FROM DUAL UNION ALL
SELECT 2, 'quality', 10, NULL FROM DUAL UNION ALL
SELECT 2, 'status', NULL, 'no' FROM DUAL UNION ALL
SELECT 3, 'quality', 15, NULL FROM DUAL UNION ALL
SELECT 3, 'status', NULL, 'yes' FROM DUAL;
输出:
EVENT QUANTITY STATUS 1 5 yes 2 10 no 3 15 yes
对于您的更新,您可以使用条件聚合:
SELECT event,
MAX(CASE elements WHEN 'quality' THEN numericval END) AS quantity,
MAX(CASE elements WHEN 'status' THEN details END) AS status,
MAX(CASE elements WHEN 'Lot' THEN numericval END) AS lot
FROM table_name
GROUP BY event
其中更新数据:
CREATE TABLE table_name (event, elements, numericval, details) AS
SELECT 1, 'quality', 5, NULL FROM DUAL UNION ALL
SELECT 1, 'status', NULL, 'yes' FROM DUAL UNION ALL
select 1 , 'Lot',1,null from dual union all
SELECT 2, 'quality', 10, NULL FROM DUAL UNION ALL
SELECT 2, 'status', NULL, 'no' FROM DUAL UNION ALL
select 2 , 'Lot',3,null from dual union all
SELECT 3, 'quality', 15, NULL FROM DUAL UNION ALL
SELECT 3, 'status', NULL, 'yes' FROM DUAL union all
select 3, 'Lot', 4,null from dual;
输出:
EVENT QUANTITY STATUS LOT 1 5 yes 1 2 10 no 3 3 15 yes 4
db<>fiddle here