0

我想计算某些列中出现的频率值并创建一个新表,其中值作为列,频率作为数据。例子:

create table users
(id number primary key, 
name varchar2(255));

insert into users  values (1, 'John');
insert into users  values (2, 'Joe');
insert into users  values (3, 'Max');

create table meals
(id number primary key,
user_id number,
food varchar2(255));

insert into meals values (1, 1, 'Apple');
insert into meals values (2, 1, 'Apple');
insert into meals values (3, 1, 'Orange');
insert into meals values (4, 1, 'Bread');
insert into meals values (5, 1, 'Apple');
insert into meals values (6, 2, 'Apple');
insert into meals values (7, 2, 'Bread');
insert into meals values (8, 2, 'Bread');
insert into meals values (9, 2, 'Apple');
insert into meals values (10, 3, 'Orange');
insert into meals values (11, 3, 'Bread');
insert into meals values (12, 3, 'Bread');

所以我得到了不同的用户和他们的饭菜(这里是面包、苹果和橙子)。对于每个用户,我想知道他多久吃一次不同的食物。以下查询正是我想要的:

select 
(select count(id) from meals where meals.user_id = users.id and meals.food = 'Apple') as count_apple,
(select count(id) from meals where meals.user_id = users.id and meals.food = 'Orange') as count_orange,
(select count(id) from meals where meals.user_id = users.id and meals.food = 'Bread') as count_bread
from users;

问题是,这真的很慢,尤其是当我有超过 100.000 个用户和几十种不同的食物时。我确信有一种更快的方法,但是我在 SQL 方面没有足够的经验来解决这个问题。

4

1 回答 1

1

如果您使用的是 11g,那么您可以使用pivot运算符,如下所示:

select * from (
  select user_id, food from meals
)
pivot (count(*) as count for (food) in ('Apple', 'Orange', 'Bread'));

否则,您将不得不进行手动旋转:

select user_id, 
       sum(case when food = 'Apple' then 1 else 0 end) count_apple, 
       sum(case when food = 'Orange' then 1 else 0 end) count_orange, 
       sum(case when food = 'Bread' then 1 else 0 end) count_bread
from   meals
group  by user_id

在任何一种情况下,这些都应该比您原来的方法更快,因为您只访问meals表一次。

于 2013-01-26T16:08:06.433 回答