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

  • Home
  • SEARCH
  • 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 8954001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:13:05+00:00 2026-06-15T14:13:05+00:00

I have a table with some data like id group_id f1 f2 f3 1

  • 0

I have a table with some data like

id      group_id    f1    f2    f3
1          1        a     b   
2          1        c   
3          2        a           c

How can i retrieve one row with group_id and count of rows for each field satisfying some textual condition?

Like that:

MY_MAGIC_SELECT(`f1`='a',`f3`='c');

must return

group_id    f1    f2    f3
   1        1     0     0        
   2        1     0     1
  • 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-15T14:13:06+00:00Added an answer on June 15, 2026 at 2:13 pm

    Using a sequence of SUM(CASE...) aggregate functions to represent each of your conditions should do it. The CASE returns a 0 or 1 if the condition is matched, and the SUM() adds the result. The GROUP BY is applied on the group_id.

    SELECT
      group_id
      SUM(CASE WHEN f1 = 'a' THEN 1 ELSE 0 END) AS f1,
      SUM(CASE WHEN f2 = 'b' THEN 1 ELSE 0 END) AS f2,
      /* f3 = 'b' isn't in your "magic select" but here it is anyway... */
      SUM(CASE WHEN f3 = 'c' THEN 1 ELSE 0 END) AS f3
    FROM 
      yourtable
    GROUP BY group_id
    

    Specifically for MySQL, you don’t need the CASE since the boolean expression f1 = 'a' will itself return a 1 or 0. So you can simplify it to the example below. This is not portable to any RDBMS, however.

    SELECT
      group_id
      SUM(f1 = 'a') AS f1,
      SUM(f2 = 'b') AS f2,
      SUM(f3 = 'c') AS f3
    FROM 
      yourtable
    GROUP BY group_id
    

    Here is a quick demonstration on SQLfiddle.com

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

Sidebar

Related Questions

Ok, I have a table which contains multiple rows. Each row contains some data
I have an Excel table with some data that I would like to export
I have some data in a table that looks roughly like the following: table
I am trying to transfer some data between tables. The 'NEW' table can have
I have 8 separate tables. Each table has id, a datetime field and some
Given I have table with some data... I have a query which is used
I have Access table with fields, in which there is some data and path
I have defined a table type PL/SQL variable and added some data there. create
I have a table from a vendor application that stores some xml data into
I have some data in an SQL table with an XML datatype column where

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.