需要帮助旋转数据
Posted
技术标签:
【中文标题】需要帮助旋转数据【英文标题】:Need assistance pivoting data 【发布时间】:2020-10-29 20:53:05 【问题描述】:我查看了所有示例,但无法完全理解 Oracle 的 PIVOT 功能。我也不确定是否可以在单个查询中完成我需要的操作。
我正在尝试处理一些选举数据。以下是一些示例数据:
create table elections_test (election_date date, county_name varchar2(40), precinct_code varchar2(7), race_number varchar2(7), race_description varchar2(250), cand_name varchar2(100), cand_party varchar2(3), cand_tot_votes number, cand_absmail_votes number, cand_early_votes number, cand_elecday_votes number);
insert into elections_test values ('8-NOV-2016', 'CREEK', '190420', '10001', 'FOR PRESIDENT AND VICE PRESIDENT', 'DONALD TRUMP', 'REP', 402, 16, 42, 344);
insert into elections_test values ('8-NOV-2016', 'CREEK', '190420', '10001', 'FOR PRESIDENT AND VICE PRESIDENT', 'GARY JOHNSON', 'LIB', 33, 1, 2, 30);
insert into elections_test values ('8-NOV-2016', 'CREEK', '190420', '10001', 'FOR PRESIDENT AND VICE PRESIDENT', 'HILLARY CLINTON', 'DEM', 155, 15, 35, 105);
insert into elections_test values ('8-NOV-2016', 'CREEK', '190420', '10003', 'FOR US SENATOR', 'JAMES BOND', 'REP', 438, 16, 43, 379);
insert into elections_test values ('8-NOV-2016', 'CREEK', '190420', '10003', 'FOR US SENATOR', 'MIKE SMITH', 'DEM', 126, 11, 33, 82);
insert into elections_test values ('8-NOV-2016', 'CREEK', '190420', '10003', 'FOR US SENATOR', 'ROBERT JONES', 'IND', 7, 1, 0, 6);
insert into elections_test values ('8-NOV-2016', 'CREEK', '190420', '10003', 'FOR US SENATOR', 'JOHN JAMES', 'LIB', 17, 1, 1, 15);
insert into elections_test values ('8-NOV-2016', 'ALFALFA', '020121', '10001', 'FOR PRESIDENT AND VICE PRESIDENT', 'DONALD TRUMP', 'REP', 300, 20, 40, 240);
insert into elections_test values ('8-NOV-2016', 'ALFALFA', '020121', '10001', 'FOR PRESIDENT AND VICE PRESIDENT', 'GARY JOHNSON', 'LIB', 23, 1, 1, 21);
insert into elections_test values ('8-NOV-2016', 'ALFALFA', '020121', '10001', 'FOR PRESIDENT AND VICE PRESIDENT', 'HILLARY CLINTON', 'DEM', 125, 10, 20, 95);
insert into elections_test values ('8-NOV-2016', 'ALFALFA', '020121', '10003', 'FOR US SENATOR', 'JAMES BOND', 'REP', 330, 10, 3, 317);
insert into elections_test values ('8-NOV-2016', 'ALFALFA', '020121', '10003', 'FOR US SENATOR', 'MIKE SMITH', 'DEM', 100, 10, 8, 82);
insert into elections_test values ('8-NOV-2016', 'ALFALFA', '020121', '10003', 'FOR US SENATOR', 'ROBERT JONES', 'IND', 4, 1, 0, 3);
insert into elections_test values ('8-NOV-2016', 'ALFALFA', '020121', '10003', 'FOR US SENATOR', 'JOHN JAMES', 'LIB', 12, 0, 1, 11);
这是我想要的:
election_date | county_name | precinct_code | race_number | race_description | total_votes | total_absmail_votes | total_early_votes | total_elecday_votes | rep_total_votes | rep_absmail_votes | rep_early_votes | rep_elecday_votes | dem_total_votes | dem_absmail_votes | dem_early_votes | dem_elecday_votes | other_total_votes | other_absmail_votes | other_early_votes | other_elecday_votes
8-NOV-2016 | CREEK | 190420 | 10001 | FOR PRESIDENT AND VICE PRESIDENT | 590 | 32 | 79 | 479 | 402 | 16 | 42 | 344 | 155 | 15 | 35 | 105 | 33 | 1 | 2 | 30
8-NOV-2016 | CREEK | 190420 | 10003 | FOR US SENATE | ### | ## | # | # | ### | ## | ## | ### | # | # | ## | ### | ## | # | # | ##
8-NOV-2016 | ALFALFA | 020121 | 10001 | FOR PRESIDENT AND VICE PRESIDENT | ### | ## | # | # | ### | ## | ## | ### | # | # | ## | ### | ## | # | # | ##
8-NOV-2016 | ALFALFA | 020121 | 10003 | FOR US SENATE | ### | ## | # | # | ### | ## | ## | ### | # | # | ## | ### | ## | # | # | ##
基本上只是试图将每个选举日期、选区代码和种族都压平为一个记录。任何帮助将不胜感激。
【问题讨论】:
【参考方案1】:您可以使用条件聚合:
SELECT e.election_date,
e.county_name,
e.precinct_code,
e.race_number,
e.race_description,
SUM(e.cand_tot_votes) AS total_votes,
SUM(e.cand_absmail_votes) AS total_absmail_votes,
SUM(e.cand_early_votes) AS total_early_votes,
SUM(e.cand_elecday_votes) AS total_elecday_votes,
SUM(CASE WHEN e.cand_party = 'REP' THEN e.cand_tot_votes END) AS rep_total_votes,
SUM(CASE WHEN e.cand_party = 'REP' THEN e.cand_absmail_votes END) AS rep_absmail_votes,
SUM(CASE WHEN e.cand_party = 'REP' THEN e.cand_early_votes END) AS rep_early_votes,
SUM(CASE WHEN e.cand_party = 'REP' THEN e.cand_elecday_votes END) AS rep_elecday_votes,
SUM(CASE WHEN e.cand_party = 'DEM' THEN e.cand_tot_votes END) AS dem_total_votes,
SUM(CASE WHEN e.cand_party = 'DEM' THEN e.cand_absmail_votes END) AS dem_absmail_votes,
SUM(CASE WHEN e.cand_party = 'DEM' THEN e.cand_early_votes END) AS dem_early_votes,
SUM(CASE WHEN e.cand_party = 'DEM' THEN e.cand_elecday_votes END) AS dem_elecday_votes
FROM elections_test e
GROUP BY e.election_date,
e.county_name,
e.precinct_code,
e.race_number,
e.race_description
为了根据需要旋转您的数据。
Demo
【讨论】:
您是说从该查询开始,然后将其用作 PIVOT 的基础吗?不要以为那行得通。我没有政党信息,因此无法按政党细分选票。 没有按党派细分。 对不起,我最近能理解你的意思@sukach,只是将这些案例添加到答案中。以上是关于需要帮助旋转数据的主要内容,如果未能解决你的问题,请参考以下文章