Advanced SQL: Using Count

365 Days of Daily Coding: Day 122

It’s a lovely morning today. I live in an area where there is a small forest besides huge mass of buildings and you can often hear the birds chirping as its quiet. Reflecting back, I am where I have always wanted to be back when I young graduate out of unitversity. I have lot of things to learn, see, be at and do. Dream big and make it possible!

I tried the solution in oracle environment here.

WITH temp AS (
Select 'Alpha'    AS Workflow, 1 AS Step_Number, '7/2/2018' AS CompletionDate FROM Dual Union All
Select 'Alpha'    AS Workflow, 2 AS Step_Number, '7/2/2018' AS CompletionDate FROM Dual Union All
Select 'Alpha'    AS Workflow, 3 AS Step_Number, '7/1/2018' AS CompletionDate FROM Dual Union All
Select 'Bravo'    AS Workflow, 1 AS Step_Number, '6/25/2018' AS CompletionDate FROM Dual Union All
Select 'Bravo'    AS Workflow, 2 AS Step_Number, '' AS CompletionDate FROM Dual Union All
Select 'Bravo'    AS Workflow, 3 AS Step_Number, '6/27/2018' AS CompletionDate FROM Dual Union All
Select 'Charlie'  AS Workflow, 1 AS Step_Number, '' AS CompletionDate FROM Dual Union All
Select 'Charlie' AS Workflow, 2 AS Step_Number, '7/1/2018' AS CompletionDate FROM Dual
)
SELECT Workflow
FROM(
SELECT Workflow, COUNT(Step_Number) AS CNTStep, COUNT(CompletionDate) AS CNTDate
FROM temp GROUP BY Workflow)
WHERE CNTStep <> CNTDate

Leave a comment