Advanced SQL Challenge: Using LAG() and FLOOR()

365 Days of Daily Coding: Day 93

Lately, I have been thinking a lot about the purpose of my life. Perhaps, as I reach near my thirties, I am being given a chance to reassess my life. Perhaps, this self-awareness is asking me to rethink my chase for material and superficial possessions. As a Buddhist, I believe that life in itself is suffering and to want anything is to add to your suffering. Maybe my suffering will help me answer.

It’s rare that I let my thoughts run bare openly in my blog. It’s because there’s hardly anyone reading except my one friend. LoL!

Advance SQL Challenge

WITH temp AS (
Select 'Alpha' AS Workflow, '6/1/2018' AS ExecutionDate FROM Dual Union All
Select 'Alpha' AS Workflow, '6/14/2018' AS ExecutionDate FROM Dual Union All
Select 'Alpha' AS Workflow, '6/15/2018' AS ExecutionDate FROM Dual Union All
Select 'Bravo' AS Workflow, '6/1/2018' AS ExecutionDate FROM Dual Union All
Select 'Bravo' AS Workflow, '6/2/2018' AS ExecutionDate FROM Dual Union All
Select 'Bravo' AS Workflow, '6/19/2018' AS ExecutionDate FROM Dual Union All
Select 'Charlie' AS Workflow, '6/1/2018' AS ExecutionDate FROM Dual Union All
Select 'Charlie' AS Workflow, '6/15/2018' AS ExecutionDate FROM Dual Union All
Select 'Charlie' AS Workflow, '6/30/2018' AS ExecutionDate FROM Dual 
)
SELECT DISTINCT Workflow, FLOOR(AVG(Diff) OVER (PARTITION BY Workflow)) AS "Average Days"
FROM(
SELECT Workflow, ExecutionDate - LAG(ExecutionDate) OVER (PARTITION BY Workflow ORDER BY ExecutionDate) AS Diff
FROM (
SELECT Workflow, TO_DATE(ExecutionDate, 'MM/DD/YYYY') AS ExecutionDate
FROM temp))
ORDER BY Workflow

2 responses to “Advanced SQL Challenge: Using LAG() and FLOOR()”

  1. Hello karma,

    I read what you wrote, and I feel you 😉

    Thanks for the blog, It’s helping me practice my SQL again for a job interview!

    Liked by 1 person

    1. Oh gosh! hmmm.. I am kind of lost for words really 😅😅😅 .Thank you for your kind words. My motivation has been dwindling lately and your message was just what I needed and reminded me why I started this blog in the first place. Good luck for your job interview! I wish you all the best. ✨✨✨

      Like

Leave a reply to Karma Cancel reply