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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:32:54+00:00 2026-05-11T18:32:54+00:00

Background: I have this with rollup query defined in MySQL: SELECT case TRIM(company) when

  • 0

Background:

I have this “with rollup” query defined in MySQL:

    SELECT      
        case TRIM(company)
            when 'apple'        THEN 'AAPL'
            when 'microsoft'    THEN 'MSFT'
            else '__xx__'
        END as company        

        ,case TRIM(division)
            when 'hardware'         THEN Trim(division)
            when 'software'         THEN Trim(division)
            else '__xx__'
        END as division

        ,concat( '$' , format(sum(trydollar),0))    as  dollars
    FROM        pivtest
    GROUP BY    
        company, division with rollup        

And it generates this output:

AAPL;hardware;$279,296
AAPL;software;$293,620
AAPL;__xx__;$572,916
MSFT;hardware;$306,045
MSFT;software;$308,097
MSFT;__xx__;$614,142
__xx__;__xx__;$1,187,058

If you have used “with rollup” queries in MySQL before, you can most likely infer the structure of my source table.

Question:

Given this raw output of MySQL, what is the easiest way to get a “tree” structure like the following?

AAPL
    hardware;$279,296
    software;$293,620
        Total; $572,916
MSFT
    hardware;$306,045
    software;$308,097
        Total;$614,142
Total    
            $1,187,058
  • 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-11T18:32:54+00:00Added an answer on May 11, 2026 at 6:32 pm

    Easiest is to do it in whatever client program you’re using to receive and show the user MySQL’s output — definitely not easiest to implement presentation-layer functionality in the data layer!-) So tell us what language &c is in your client program and we may be able to help…

    Edit: giving a simple Python client-side solution at the original asker’s request.

    With Python’s DB API, results from a DB query can be most simply seen as a list of tuples. So here’s a function to format those results as required:

    def formout(results):
      marker = dict(__xx__='   Total')
      current_stock = None
      for stock, kind, cash in results:
        if stock != current_stock:
          print marker.get(stock, stock).strip()
          current_stock = stock
        if kind in marker and stock in marker:
          kind = ' '*8
        print '    %s;%s' % (marker.get(kind, kind), cash)
    

    marker is a dictionary to map the special marker '__xx__' into the desired string in the output (I’m left-padding it appropriately for the “intermediate” totals, so when I print the final “grand total”, I .strip() those blanks off). I also use it to check for the special case in which both of the first two columns are the marker (because in that case the second column needs to be turned into spaces instead). Feel free to ask in comments for any further clarification of Python idioms and use that may be necessary!

    Here’s the output I see when I call this function with the supplied data (turned into a list of 7 tuples of 3 strings each):

    AAPL
        hardware;$279,296
        software;$293,620
           Total;$572,916
    MSFT
        hardware;$306,045
        software;$308,097
           Total;$614,142
    Total
                ;$1,187,058
    

    The space-alignment is not identical to that I see in the question (which is a little inconsistent in terms of how many spaces are supposed to be where) but I hope it’s close enough to what you want to make it easy for you to adjust this to your exact needs (as you’re having to translate Python into PHP anyway, the space-adjustment should hopefully be the least of it).

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

Sidebar

Ask A Question

Stats

  • Questions 197k
  • Answers 197k
  • 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 It's a bug of Range. There is the source code… May 12, 2026 at 7:20 pm
  • Editorial Team
    Editorial Team added an answer You'll want to have a custom sort on your DataGridView.… May 12, 2026 at 7:20 pm
  • Editorial Team
    Editorial Team added an answer No, this completes only the certificate part. you need to… May 12, 2026 at 7:20 pm

Related Questions

BACKGROUND I am using a commercial application on windows that creates a drawing This
Background: I have an application written in native C++ which uses the wxWidgets toolkit's
I have an href in HTML that I dynamically produce from a server. I
Long story short, I need to get up to speed with Joomla fast .

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.