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 257627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:06:47+00:00 2026-05-11T22:06:47+00:00

I need to pull several rows from a table and process them in two

  • 0

I need to pull several rows from a table and process them in two ways:

  • aggregated on a key
  • row-by-row, sorted by the same key

The table looks roughly like this:

table (
   key,
   string_data,
   numeric_data
)

So I’m looking at two approaches to the function I’m writing.

The first would pull the aggregate data with one query, and then query again inside a loop for each set of row-by-row data (the following is PHP-like pseudocode):

$rows = query(
        "SELECT key,SUM(numeric_data)
         FROM table
         GROUP BY key"
    );

foreach ($rows as $row) {
    <process aggregate data in $row>

    $key = $row['key'];
    $row_by_row_data = handle_individual_rows($key);
}

function handle_individual_rows($key)
{
    $rows = query(
            "SELECT string_data
             FROM table WHERE key=?",
            $key
        );

    <process $rows one row at a time>

    return $processed_data;
}

Or, I could do one big query and let the code do all the work:

$rows = query(
    "SELECT key, string_data, numeric_data
     FROM table"
);

foreach ($rows as $row) {
    <process rows individually and calculate aggregates as I go>
}

Performance is not a practical concern in this application; I’m just looking to write sensible and maintainable code.

I like the first option because it’s more modular — and I like the second option because it seems structurally simple. Is one option better than the other or is it really just a matter of style?

  • 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-11T22:06:47+00:00Added an answer on May 11, 2026 at 10:06 pm

    One SQL query, for sure.

    This will

    • Save you lots of roundtrips to database
    • Allow to use more efficient GROUP BY methods

    Since your aggregates may be performed equally well by the database, it will also be better for mainainability: you have all your resultset logic in one place.

    Here is an example of a query that returns every row and calculates a SUM:

    SELECT  string_data, numeric_data, SUM(numeric_data) OVER (PARTITION BY key)
    FROM    table
    

    Note that this will most probably use parallel access to calculate SUM‘s for different key‘s, which is hardly implementable in PHP.

    Same query in MySQL:

    SELECT  key, string_data, numeric_data,
            (
            SELECT  SUM(numeric_data)
            FROM    table ti
            WHERE   ti.key = to.key
            ) AS key_sum
    FROM    table to
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 166k
  • Answers 166k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Take a look at the range() function. >> $a =… May 12, 2026 at 1:21 pm
  • Editorial Team
    Editorial Team added an answer From what I see, all of your pages' <body>s have… May 12, 2026 at 1:21 pm
  • Editorial Team
    Editorial Team added an answer The big innovation with GCD is that it includes kernel-level… May 12, 2026 at 1:21 pm

Related Questions

I have a large table, 1B+ records that I need to pull down and
I'm currently trying to write a website for testing / learning purposes that will
I have an interesting problem and would appreciate your thoughts for the best solution.
I am a graduate student of physics and I am working on writing some

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.