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

  • Home
  • SEARCH
  • 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 1088847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:07:55+00:00 2026-05-16T23:07:55+00:00

I need to get summary data from many many rows. The summary fields are

  • 0

I need to get summary data from many many rows. The summary fields are counts of how many entries have each value for different fields. For example, a table with people’s age, city, job, etc, the summary data would include fields like “countManager”, “countCodeMonkey” for each job, and then “countChicago”, “countNewYork” etc for cities.

I know that a simple way of getting everything is:

select count(*) from table
    group by age, city, job

But this is vertical counting – a different row for each value I need. Instead of rows with the counts, I need fields, because I have other fields to group by, for example state. So I want my results to look like this:

| State | countManager | countMonkey |
|  IL   |      3       |     25      |
|  NY   |      5       |     40      |

I’m looking at two ways to do this. We already have one implemented, and it takes 20 minutes to execute. I’m wondering if a different way would be faster.

The current way looks like this:

create view managers as
    select state, count(*) as theCount from table
        where job = 'Manager'
        group by state;

create view monkeys as
    select state, count(*) as theCount from table
        where job = 'Monkey'
        group by state;

select managers.theCount as managers, monkeys.theCount as monkeys
    from managers left join monkeys
        on managers.state = monkeys.state;

In the real case, there’s about 20 more views, and hence 20 more joins.

I’m considering the following horizontal count method instead:

select state,
  sum(case when job='Manager' then 1 else 0 end) as managers,
  sum(case when job='Monkey' then 1 else 0 end) as monkeys
    from table
        group by state;

This eliminates the joins. But I have no clue about the performance of the “sum case” statement. Will this be any faster, about the same, or much slower? Does the engine have to go through the rows multiple times, once for each such counted field? Or is it smart enough to count all the fields in one run through, checking each value and incrementing the appropriate sum?

I may spend part of a day writing a script to generate a giant amount of garbage data to test this, but I’d still like to know how the db engine behaves, which I couldn’t find elsewhere online.

  • 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-05-16T23:07:56+00:00Added an answer on May 16, 2026 at 11:07 pm

    Totally depends on the engine and how you want to see the data, but your second option should definitely pull through faster.

    20 minutes even for your first query is ridiculous though unless you have literally billions of rows. In that case you should look at archiving data on a monthly/weekly basis with precompiled aggregated data in a table where you can slice and dice to suit.

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

Sidebar

Related Questions

I have two tables with a weak relation. I need get a text value
Ok so I need to query a live website to get data from a
I have a jquery script that collects data from different sites and saves it
A] Problem Summary: I have one to many data models in my project. I
I need get all items these have no categories int? categoryId = null; var
I have linq request. I need get item.Title in select. how do this? var
I need to get information on loaded data percentage when UIWebView loads page. I
I need to get the contents from this URL http://google.fr/ok in a NSString can
I need to get the coefficients along with their SEs, t-values and p-values from
A] Summary: Using get or insert functionality on one to many django models, checking

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.