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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:44:47+00:00 2026-06-18T11:44:47+00:00

I know this question might be repeated … But I have query like this,

  • 0

I know this question might be repeated … But I have query like this, I have a field “Compute_CRM_State” in my table. And that field contains data like “approved,pending,cancelled” but now i want to fetch total count of approved or pending or cancelled. I tried it with sub query. I can able to get total count of approved or pending or cancelled but.. I got same records many times.

result what I am getting from my query.

query :

SELECT        (SELECT        COUNT(Compute_CRM_State) AS Expr1
                          FROM            CRM_Doctor_Request
                          WHERE        (CRM_State_Id = 1)) AS PENDING,
                             (SELECT        COUNT(Compute_CRM_State) AS Expr2
                               FROM            CRM_Doctor_Request AS CRM_Doctor_Request_3
                               WHERE        (CRM_State_Id = 2)) AS Approved,
                             (SELECT        COUNT(Compute_CRM_State) AS Expr3
                               FROM            CRM_Doctor_Request AS CRM_Doctor_Request_2
                               WHERE        (CRM_State_Id = 3)) AS CANCELLED
FROM            CRM_Doctor_Request AS CRM_Doctor_Request_1

There should be display only 1 record of count.. but it is getting display the number of times that number of records in table contains.

  • 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-18T11:44:48+00:00Added an answer on June 18, 2026 at 11:44 am

    The reason you were not getting the total count correctly and getting many records instead, is that the COUNTs are selected as a correlated subquery, so you will got the count for each record in the table.

    To fix that you have to use the aggregate function in the outer query, not from the correlated subuery. And to get those total count for each status, use the CASE expression to do this like so:

    SELECT
      SUM(CASE WHEN CRM_State_Id = 1 THEN 1 ELSE 0 END) AS PENDING,
      SUM(CASE WHEN CRM_State_Id = 2 THEN 1 ELSE 0 END) AS Approved,
      SUM(CASE WHEN CRM_State_Id = 3 THEN 1 ELSE 0 END) AS CANCELLED
    FROM  CRM_Doctor_Request;
    

    SQL Fiddle Demo


    Or: you can use the PIVOT table operator to do the same thing:

    SELECT 
      [1] AS PENDING, 
      [2] AS Approved, 
      [3] AS Cancelled
    FROM 
    ( 
       SELECT * FROM CRM_Doctor_Request
    ) AS t
    PIVOT
    (
      COUNT(Compute_CRM_State)
      FOR  CRM_State_Id IN([1], [2], [3])
    ) AS p;
    

    Like this.

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

Sidebar

Related Questions

I know that this question might have been asked like 100 times, but, believe
I know this question might be repeated but my query is different let me
I know this question might have already been discussed a thousands of times but
I know this question might be repeated many times but would really appreciate any
I know this question might have been asked a few times already, but I
I know this question might sound a little bit crazy, but I tough that
I know the title of this question might be strange, but that was the
i know this question might be too easy, but i have no idea how
i know this question might look like a duplicate. but i had a hard
I know this question might sound quite silly, but I somehow found myself stuck

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.