Skip to content
Advertisement

SQL: Return Column Name With Highest Value From Subquery

Working with a large data set in Big Query that assigns a specific grade to an item. For example:

To summarize these, I wrote out some COUNT & CASE functions, like:

It results in something like below:

I have been attempting to have this computation happen in a subquery, and then use the alp_a or org_a to populate in a summary of the results, showing me what the most common grade each fruit has. Something like the below

How would I be able to utilize what was counted, and provide me with a simplified output?

Advertisement

Answer

Below is for BigQuery Standard SQL

if to apply to sample data from your question – output is

enter image description here

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