Improving query that counts distinct values that have particular values in another column

Improving query that counts distinct values that have particular values in another column

Say I have a table in the format:

Say I have a table in the format:

| id | category|
|----|---------|
| 10 | A       |
| 10 | B       |  
| 10 | C       |
| 2  | C       |

I want to count the number of distinct id's that have all three values A, B, and C in the category variable. In this case, the query would return 1 since only for id = 10 is this true.

My intuition is to write the following query to get this value:

SELECT 
    COUNT(DISTINCT id), 
    SUM(CASE WHEN category = 'A' THEN 1 else 0 END) AS A,
    SUM(CASE WHEN category = 'B' THEN 1 else 0 END) AS B,
    SUM(CASE WHEN category = 'C' THEN 1 else 0 END) AS C
FROM 
    table 
GROUP BY 
    id
HAVING
    A >= 1
    AND 
    B >= 1
    AND
    C >= 1

This feels a bit overwrought though -- is there a simpler way to achieve the desired outcome?

sql

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Introduction to Structured Query Language SQL pdf

SQL stands for Structured Query Language. SQL is a scripting language expected to store, control, and inquiry information put away in social databases. The main manifestation of SQL showed up in 1974, when a gathering in IBM built up the principal model of a social database. The primary business social database was discharged by Relational Software later turning out to be Oracle.

Introduction to Structured Query Language SQL pdf

SQL stands for Structured Query Language. SQL is a scripting language expected to store, control, and inquiry information put away in social databases. The main manifestation of SQL showed up in 1974, when a gathering in IBM built up the principal model of a social database. The primary business social database was discharged by Relational Software later turning out to be Oracle.

Welcome Back the T-SQL Debugger with SQL Complete – SQL Debugger

Debug SQL stored procedures and develop your SQL database project with dbForge SQL Complete, a new add-in for Visual Studio and SSMS. When you develop large chunks of T-SQL code with the help of the SQL Server Management Studio tool, it is essential to test the “Live” behavior of your code by making sure that each small piece of code works fine and being able to allocate any error message that may cause a failure within that code.

The Easy Guide on How to Use Subqueries in SQL Server

This article provides an outlook on various types of subqueries in SQL such as select or other T-SQL statements and caveats when using them.

Learning How to Transition Your SQL Server Skills to Azure SQL

Are you interested in learning how to translate your existing SQL Server expertise to Azure SQL including Azure SQL Database and Azure SQL Managed Instance? In this episode, Bob Ward, Anna Hoffman, and Marisa Brasile announce all-new content on YouTube, Github, and Microsoft Learn to help you become an Azure SQL professional.