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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:09:02+00:00 2026-05-20T12:09:02+00:00

Could you please help me out in building a Query. I Have a table

  • 0

Could you please help me out in building a Query. I Have a table as below

Id            Info_Id                 Type
1              2                              2
2              6                              2
3              5                              3
4              8                              3
5              2                              3
6              2                              2
7              5                              2
8              8                              2
9              5                              2
10             8                              2
11             8                              2
12             5                              3
13             6                              3
14             8                              3

a query need to be framed so as to group by “Info_Id”.

I need output as below for eg:

Info_Id CountOfRec       Type2    Type3
2              3         2              1
5              4         2              2
6              2         1              1
8              5         3              2

I Tried as below but I m not able to get the efficient output

select  Info_Id, count(Id)as CountOfRec,
(select count(Id)from tbl_TypeInfo where Info_Id = 5 AND Type = 2) as Type2,
(select count(Id)from tbl_ TypeInfo where Info_Id = 5 AND Type = 3) as Type3
 from tbl_TypeInfo
where Info_Id = 5
group by Info_Id

output was this ,

Info_Id CountOfRec       Type2    Type3
5              4                              2              2

(I have to loop for each “Info_id” to get desired OP, there is thousand of records and its time consuming)

I wanted to get the highlighted output from the table. The query I have framed is not efficient and there would be good solution for this can you help me out.

  • 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-20T12:09:03+00:00Added an answer on May 20, 2026 at 12:09 pm

    You can use the CASE expression to only count the rows for a specific type:

    SELECT Info_Id,
        COUNT(*) AS CountOfRec,
        COUNT(CASE WHEN Type = 2 THEN 1 ELSE NULL END) AS Type2
        COUNT(CASE WHEN Type = 3 THEN 1 ELSE NULL END) AS Type3
    FROM tbl_TypeInfo
    GROUP BY Info_Id
    

    Add a WHERE Info_Id = 5 to retrieve results for a specific ID only.

    Update: as per comments, if you do not store a table of ID’s, You need to change your IN(..) list to a virtual “table”:

    SELECT vt.id,
        COUNT(*) AS CountOfRec,
        COUNT(CASE WHEN Type = 2 THEN 1 ELSE NULL END) AS Type2,
        COUNT(CASE WHEN Type = 3 THEN 1 ELSE NULL END) AS Type3
    FROM (
        SELECT 1 id
        UNION SELECT 2
        UNION SELECT 3
        UNION SELECT 5
        UNION SELECT 8 
      ) AS vt LEFT JOIN tbl_TypeInfo ON vt.id = tbl_TypeInfo.Info_Id
    GROUP BY vt.id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

yes, this is a homework-type question but could you please help me out? In
could someone please help me out with the (simplified) code below. I'm trying to
Could someone please help me out with the following problem in java. I have
Could someone please help me out with this. I need to find the gml:pos
Could you please help me out to find the correct deceleration rate for the
Ok so could anyone please help me out with the VB for auto entering
Could you please help me find out the time complexity of the Fleury' algorithm
Could somebody please help me get Clang up and running? (I don't have 3.2)
if you could please help me out... i am try to create o forgot
I'm probably doing something wrong, being a newbie. Could you please help me out?

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.