Advanced SQL: Using MAX() and INNERJOIN()

365 Days of Daily Coding: Day 137

I just recently finished a data viz project that I built in Tableau. I am thinking of making a blog post for the process. It is going to be a small post. I will be talking about where I sourced the data from, what were few design choices that I made and some blockers I faced.

I am also in the midst of another data viz projects which I had started quite few weeks ago. Unlike most of the data I have worked with, this is more of a categorical/qualitative data. I will be using Adobe Illustrator and RawGraphs for this project and making a blog post about it too.

Puzzle #22: Occurrences

I tried the solution in the live oracle environment here.

WITH temp As (
Select 'Alpha' As Workflow, 5 AS Occurrences, 'Error: Conversion Failed' AS Message FROM Dual Union All
Select 'Alpha' As Workflow, 8 AS Occurrences, 'Status Complete' AS Message FROM Dual Union All
Select 'Alpha' As Workflow, 9 AS Occurrences, 'Error: Unidentified error occurred' AS Message FROM Dual Union All
Select 'Bravo' As Workflow, 3 AS Occurrences, 'Error: Cannot Divide by 0' AS Message FROM Dual Union All
Select 'Bravo' As Workflow, 1 AS Occurrences, 'Error: Unidentified error occurred' AS Message FROM Dual Union All
Select 'Charlie' As Workflow, 10 AS Occurrences, 'Error: Unidentified error occurred' AS Message FROM Dual Union All
Select 'Charlie' As Workflow, 7 AS Occurrences, 'Error: Conversion Failed' AS Message FROM Dual Union All
Select 'Charlie' As Workflow, 6 AS Occurrences, 'Status Complete' AS Message FROM Dual
), cte_LogMessageCount AS
(SELECT	Message, MAX(Occurrences) AS MaxOccurrences
FROM temp
GROUP BY Message
)
SELECT	a.Workflow, a.Occurrences, a.Message
FROM	temp a INNER JOIN
		cte_LogMessageCount b ON a.Message = b.Message AND 							 a.Occurrences = b.MaxOccurrences
ORDER BY 1;

Leave a comment