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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:06:41+00:00 2026-06-07T06:06:41+00:00

OK, a slight variation on an earlier theme. Using the same basic idea, I

  • 0

OK, a slight variation on an earlier theme. Using the same basic idea, I want to get independent counts of the fields, then I want them grouped by a higher order breakdown.

I expanded the example by David to include a higher order column:

district_id, product_id, service_id

dist    proj    serv
1   1   1
1   1   2
1   1   2
1   1   3
1   1   3
1   1   4
1   2   2
1   2   4
1   2   4
1   2   5
1   2   5
2   1   1
2   2   1
2   1   6
2   2   6
2   3   6

To get a result on the total, I used a simple query with two sub-queries.

select 
(select count(Distinct project_id) from GroupAndCountTest) AS "projects",
(select count(Distinct service_id) from GroupAndCountTest) as "services";

projects  services
       3         6

The challenge was to get this grouped within the district_id. What I wanted was:

district_id   projects  services
          1          2         5
          2          3         6 

I ended up using similar sub-queries, but the only way I was able to combine them (other than using a stored function) was to re-run the sub-queries for every district. (Not a big problem here, but in my application the sub-queries use multiple tables with a substantial number of “districts” so the two sub-queries are run again for each “district” which will become increasingly ineffecient.

This query works, but I would love to see something more effecient.

select t1.district_id, p1.projects, s1.services
  from GroupAndCountTest as t1
  join (select district_id, count(Distinct project_id) as projects 
    from GroupAndCountTest
    group by district_id) AS p1
    on p1.district_id=t1.district_id
  join (select district_id, count(Distinct service_id) as services 
    from GroupAndCountTest
    group by district_id) as s1
    on s1.district_id=t1.district_id
  group by t1.district_id;

Thanks.

PS: If you want to experiment, you can create the table with:

CREATE TABLE `GroupAndCountTest` (
  `district_id` int(5) DEFAULT NULL,
  `project_id` int(5) DEFAULT NULL,
  `service_id` int(5) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
insert  into `GroupAndCountTest`(`district_id`,`project_id`,`service_id`) 
  values (1,1,1),(1,1,2),(1,1,2),(1,1,3),(1,1,3),(1,1,4),(1,2,2),(1,2,4),
  (1,2,4),(1,2,5),(1,2,5),(2,1,1),(2,2,1),(2,1,6),(2,2,6),(2,3,6);
  • 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-07T06:06:42+00:00Added an answer on June 7, 2026 at 6:06 am
    select district_id, 
    count(distinct(product_id)) projects, 
    count(distinct(service_id)) services 
    from MyTable group by district_id;
    

    where MyTable contains district_id, product_id, service_id columns

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

Sidebar

Related Questions

Regarding CRP if I want to implement a slight variation of it (using template
I want a slight variation of the custom cell example from the MS website
I have a slight variation on the window.opener.reload javascript. Normally, if I want to
My question is a slight variation on the question What is the best way
slight problem. I have a 20 x 20px delete icon which I want to
Basically I want to load a HTML document and using controls such as multiple
After using Yii's CRUD generator (gii) and making some slight variations to the form
I have a list of hclust objects resulting from slight variations in one variable
I am having a slight issue with validation in struts2 during consecutive validations.Now if
I have a slight problem with a path: D:\\Music\\DJ Ti%C3%ABsto\\Tiesto\\Adagio For Strings (Spirit of

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.