cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

How get days of year with sql in databricks.

Everton_Costa
New Contributor II
 
1 ACCEPTED SOLUTION

Accepted Solutions

Cami
Contributor III

I hope it helps:

SELECT  DATEADD(DAY, rnk - 1, '{{StartDate}}')
FROM    (
    WITH lv0(c) AS(
    SELECT 1 as c
    UNION ALL
    SELECT 1
    )
    , lv1 AS (
    Select t1.c
    from lv0 t1
    cross JOIN lv0  t2
    )
     , lv2 AS (
    Select t1.c
    from lv1 t1
    cross JOIN lv1  t2
    )
    
    , lv3 AS (
    Select t1.c
    from lv2 t1
    cross JOIN lv2  t2
    )
     , lv4 AS (
    Select t1.c
    from lv3 t1
    cross JOIN lv3  t2
    )
    SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 1)) rnk   FROM lv4
    ) tbl
WHERE   rnk - 1 <= DATEDIFF(DAY, '{{StartDate}}', '{{EndDate}}')

View solution in original post

2 REPLIES 2

Cami
Contributor III

I hope it helps:

SELECT  DATEADD(DAY, rnk - 1, '{{StartDate}}')
FROM    (
    WITH lv0(c) AS(
    SELECT 1 as c
    UNION ALL
    SELECT 1
    )
    , lv1 AS (
    Select t1.c
    from lv0 t1
    cross JOIN lv0  t2
    )
     , lv2 AS (
    Select t1.c
    from lv1 t1
    cross JOIN lv1  t2
    )
    
    , lv3 AS (
    Select t1.c
    from lv2 t1
    cross JOIN lv2  t2
    )
     , lv4 AS (
    Select t1.c
    from lv3 t1
    cross JOIN lv3  t2
    )
    SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 1)) rnk   FROM lv4
    ) tbl
WHERE   rnk - 1 <= DATEDIFF(DAY, '{{StartDate}}', '{{EndDate}}')

Everton_Costa
New Contributor II

Thanks for the answer, could you explain the code to me or share a link that explains it please?

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you wonโ€™t want to miss the chance to attend and share knowledge.

If there isnโ€™t a group near you, start one and help create a community that brings people together.

Request a New Group