cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Data Engineering
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?

Welcome to Databricks Community: Lets learn, network and celebrate together

Join our fast-growing data practitioner and expert community of 80K+ members, ready to discover, help and collaborate together while making meaningful connections. 

Click here to register and join today! 

Engage in exciting technical discussions, join a group with your peers and meet our Featured Members.