Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9199755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:36:29+00:00 2026-06-17T22:36:29+00:00

I want to count how frequent values appear in certain columns and create a

  • 0

I want to count how frequent values appear in certain columns and create a new table, with the values as columns and the frequencies as data. Example:

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');

So I got different users and their meals (here Bread, Apple and Oranges). For every user I want to know how often did he eat the different food. The following query does exactly what I want:

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;

The problem is, this is REALLY slow, especially when I got more than 100.000 users and dozens of different foods. I am sure that there is a faster way, but I am not experienced enough in SQL to solve this problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:36:30+00:00Added an answer on June 17, 2026 at 10:36 pm

    If you’re using 11g, then you can use the pivot operator, like so:

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

    Otherwise you’ll have to do a manual pivot:

    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
    

    In either case, these should be faster than your original approach as you’re only accessing the meals table once.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count the number of accounts from the resulting table generated from
I want to count number of occurence of BB code like word (example: [b]
I want to count the number of columns in a row for a CSV
I want to count number of files in a folder in a remote host.
We want to count number of shakes done by user. We tried motionBegan, motionEnded
I want to count tokens in word which don't duplicate . Example: aabbcc abc
I want to get the count how many values are repeating in the result
I want to count the number of activity views in my application. The documentation
Possible Duplicate: How do we count rows using Hibernate? I want count number of
I have an unnormalized table of customer orders. I want count how many products

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.