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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:02:14+00:00 2026-05-13T22:02:14+00:00

id – a_id – a_type ————————— 1 9 Granny Smith 2 9 Pink Lady

  • 0
id  - a_id  -  a_type
---------------------------
1      9       Granny Smith
2      9       Pink Lady
---------------------------
3      3       Pink Lady 
4      3       Fuji 
5      3       Granny Smith
---------------------------
6      7       Pink Lady
7      7       Fuji 
8      7       Fuji
9      7       Granny Smith

Ok, assuming we have an Apple sql table like above;
my question, is possible to have a result like below, with one query?

[0] => Array
    (
        [0] => a_id: 7
        [1] => Pink Lady: 1
        [2] => Granny Smith: 1
        [3] => Fuji: 2
    )

[1] => Array
    (
        [0] => a_id: 9
        [1] => Granny Smith: 1
        [2] => Pink Lady: 1
        [3] => 
    )
...

PS: my own query is this:

SELECT a_type , a_id , 
COUNT(a_type) AS tot 
FROM #apple 
GROUP by a_id , a_type 
HAVING tot > 0 
ORDER BY a_type DESC

but this doesn’t do what I need, it produces more than one a_id.

  • 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-13T22:02:14+00:00Added an answer on May 13, 2026 at 10:02 pm

    SELECT a_id,a_type,COUNT(*) FROM Apple GROUP BY a_id, a_type

    This will get you a list of distinct a_id and a_type tuples, but you will still have to parse the list to consolidate those of the same a_id into a single hash table.

    Or, if you want to, in one step, aggregate all by a_id, do this

    SELECT a_id,GROUP_CONCAT(type_count)
      FROM (SELECT a_id,a_type,CONCAT(a_type,",",COUNT(*)) as type_count
              FROM Apple GROUP BY a_id,a_type) as x
     GROUP BY a_id;
    

    This will get you:

    7    Pink Lady,1,Granny Smith,1,Fuji 2
    9    Granny Smith,1,Pink Lady,1
    

    Regarding my comment: if you have MySQL this is what you get

    Creating the exact table you have, with the exact data, and run my query, I get the following.

    mysql> SELECT a_id,GROUP_CONCAT(type_count) FROM (SELECT a_id,a_type,CONCAT(a_type,",",COUNT(*)) as type_count FROM Apple GROUP BY a_id,a_type) as x  GROUP BY a_id;
    
    |    3 | Fuji,1,Granny Smith,1,Pink Lady,1 | 
    |    7 | Fuji,2,Granny Smith,1,Pink Lady,1 | 
    |    9 | Granny Smith,1,Pink Lady,1        | 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have a webpage that will be viewed by mainly IE users, so CSS3 is
I have a JSP page retrieving data and when single or double quotes are
i have a text file with the following data: Calculated Concentrations 30.55 73.48 298.25
We have different types of hyphens/dashes (in some text) populated in db. Before comparing
I am getting the value from web service like ><&  etc. I want to
I am currently running into a problem where an element is coming back from
I'm trying to craft a Java regular expression to split strings of the general
I am generating some text to be shown on a web-site, and use HttpUtility.HtmlEncode
An extension to my previous question: Text cleaning and replacement: delete \n from a
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

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.