Skip to content
Advertisement

SQL Count columns from other columns

I have this view generated after using LEFT JOIN over 2 tables (simplified example).

Tables:

Current View:

View: (I got till here)

The required view needs to have separate columns for each status value (which are exactly 0, 1, 2 or 3). 0 & 1 are kept in the same column.

Required View: (But I need this)

I feel like I missing something basic. How can I get this view?

I don’t think we need Rank() stuff, and it’s a big query over 4 normalized tables (in practice), which is why I need an optimal solution. Subqueries work with inline SELECT queries, but they need JOINs as well. The production requirement has 10 columns for counts over 2 separate columns.

Advertisement

Answer

Use conditional aggregation:


In your orignal query, this should look like:

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement