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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:06:49+00:00 2026-06-18T00:06:49+00:00

how to get count based on enum status using self join I have a

  • 0

how to get count based on enum status using self join

I have a table called “student” in which I have used enum for status 0,1,2 where 0 for inactive, 1 for active and 2 for deleted.

that is

studentid studentenrollid studentname studentstatus

1          3                 xyz          1

2          3                 xyz          2

3          8                 asda         1

4          7                 sdd          1

5          9                 asds         0

6          3                 xyz          1

I need get the count based on status where studentrollid is unique.

studentrollid | inactive | active | deleted

    3            0           2        1

    8            0           1        0

    7            0           1        0

    9            1           0        0
  • 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-18T00:06:50+00:00Added an answer on June 18, 2026 at 12:06 am

    Try this please:

    SQLFIDDLE DEMO

    select studentenrollid,
    sum(case when studentstatus = 1 then 1
        else 0 end) as Active, 
    sum(case when studentstatus = 0 then 1
        else 0 end) as Inactive, 
    sum(case when studentstatus = 2 then 1
        else 0 end) as Deleted
    from demo
    group by studentenrollid
    ;
    
    | STUDENTENROLLID | ACTIVE | INACTIVE | DELETED |
    -------------------------------------------------
    |               3 |      2 |        0 |       1 |
    |               7 |      1 |        0 |       0 |
    |               8 |      1 |        0 |       0 |
    |               9 |      0 |        1 |       0 |
    

    Another shorter query:

    select a.studentenrollid,
    sum(a.studentstatus = 1) as Active, 
    sum(a.studentstatus = 0) as Inactive,
    sum(a.studentstatus = 2) as Deleted 
    from demo a
    group by a.studentenrollid
    ;
    
    | STUDENTENROLLID | ACTIVE | INACTIVE | DELETED |
    -------------------------------------------------
    |               3 |      2 |        0 |       1 |
    |               7 |      1 |        0 |       0 |
    |               8 |      1 |        0 |       0 |
    |               9 |      0 |        1 |       0 |
    

    MYSQL 5.1.61 version query


    Based on OP’s create statment/insert statements given in the comments:

    SQLFIDDEL DEMO 2

    | STUDENTENROLLID | ACTIVE | INACTIVE | DELETED |
    -------------------------------------------------
    |               1 |      1 |        0 |       0 |
    |               2 |      1 |        0 |       0 |
    |               3 |      1 |        0 |       0 |
    |              41 |      9 |        1 |       1 |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a table which has many fields but i want to get count
i have used the following query to get time range based count. subtract currentime
I am using a library function called get_count_and_price which returns a 2-tuple (count,price). In
how can I count row based on its contents? assumed I have table like
I'm trying to get an ID from a table based on which ID has
I am trying to get a custom enum class working which should enable me
I'm trying to get the count of surveys that need to be taken which
I have build my own custom validation framework for WPF using attribute based validation.
how do i get a KEY based on a VALUE in Perl using hashes?
I made server/ client application in WCF. How get count of connected clients? And

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.