<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Converting SQL Code to SQL Databricks in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/converting-sql-code-to-sql-databricks/m-p/21112#M14347</link>
    <description>&lt;P&gt;Spark SQL does have Common Table Expressions: &lt;A href="https://spark.apache.org/docs/latest/sql-ref-syntax-qry-select-cte.html" target="test_blank"&gt;https://spark.apache.org/docs/latest/sql-ref-syntax-qry-select-cte.html&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm not an expert at SQL or CTEs, but my guess is that it's the recursion option.  &lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 09 May 2022 15:27:40 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2022-05-09T15:27:40Z</dc:date>
    <item>
      <title>Converting SQL Code to SQL Databricks</title>
      <link>https://community.databricks.com/t5/data-engineering/converting-sql-code-to-sql-databricks/m-p/21111#M14346</link>
      <description>&lt;P&gt;I am new to Databricks. Please excuse my ignorance. &lt;/P&gt;&lt;P&gt;My requirement is to convert the SQL query below into Databricks SQL. The query comes from EventLog table and the output of the query goes into EventSummary&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;These queries can be found &lt;A href="https://dbfiddle.uk/?rdbms=sqlserver_2019l&amp;amp;fiddle=4aade2a388ffa21356f1581c4212b540" alt="https://dbfiddle.uk/?rdbms=sqlserver_2019l&amp;amp;fiddle=4aade2a388ffa21356f1581c4212b540" target="_blank"&gt;here&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;CREATE TABLE EventLog 
&amp;nbsp;
(
&amp;nbsp;
 EventID INT
&amp;nbsp;
, EventName String
&amp;nbsp;
, EventStartDateTime timestamp
&amp;nbsp;
, EventEndDateTime  timestamp
&amp;nbsp;
)
&amp;nbsp;
 
&amp;nbsp;
 
&amp;nbsp;
INSERT INTO EventLog(EventID, EventName, EventStartDateTime, EventEndDateTime)
&amp;nbsp;
VALUES(100, 'Planting', '20210620 10:34:09 AM','20211018 10:54:49 PM')
&amp;nbsp;
 ,(200, 'Foundation', '20200420 10:34:09 AM','20211018 10:54:49 PM')
&amp;nbsp;
 ,(300, 'Seeding', '20210410 10:27:19 AM','')
&amp;nbsp;
 ,(400, 'Spreading', '20220310 10:24:09 PM','');
&amp;nbsp;
 
&amp;nbsp;
 CREATE TABLE EventSummary
&amp;nbsp;
(  
&amp;nbsp;
 EventID INT
&amp;nbsp;
, EventName VARCHAR(50) String
&amp;nbsp;
, [Year] INT
&amp;nbsp;
, [MonthName] String
&amp;nbsp;
, [Hours] DECIMAL
&amp;nbsp;
)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have a code like so which I want to convert to Databricks SQL:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;;WITH CTE AS (
&amp;nbsp;
  SELECT EventID,EventName,EventStartDateTime,IIF(EventEndDateTime = '',GETUTCDATE(),EventEndDateTime) EventEndDateTime
&amp;nbsp;
  FROM EventLog
&amp;nbsp;
  UNION ALL
&amp;nbsp;
  SELECT EventID,EventName, DATEADD(month, DATEDIFF(month, 0, DATEADD(month , 1 , EventStartDateTime)), 0) , EventEndDateTime
&amp;nbsp;
  FROM CTE 
&amp;nbsp;
  WHERE DATEADD(month, DATEDIFF(month, 0, DATEADD(month , 1 , EventStartDateTime)), 0) &amp;lt;= EventEndDateTime
&amp;nbsp;
)
&amp;nbsp;
INSERT INTO EventSummary(EventID,EventName,Year,MonthName,Hours)
&amp;nbsp;
SELECT EventID,EventName,YEAR(EventStartDateTime),DATENAME(MONTH,EventStartDateTime),DATEDIFF(second, EventStartDateTime, n_EventStartDateTime) / 3600.0
&amp;nbsp;
FROM (
&amp;nbsp;
 SELECT EventID,EventName,EventStartDateTime,LEAD(EventStartDateTime,1,EventEndDateTime) OVER(PARTITION BY EventID,EventName ORDER BY EventStartDateTime) n_EventStartDateTime
&amp;nbsp;
 FROM CTE
&amp;nbsp;
) t1
&amp;nbsp;
option (maxrecursion 0)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;span class="lia-inline-image-display-wrapper" image-alt="image"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/1883iF269A72F64492414/image-size/large?v=v2&amp;amp;px=999" role="button" title="image" alt="image" /&gt;&lt;/span&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This query works very well against SQL Server but I am having issues converting this to SQL Databricks&lt;/P&gt;&lt;P&gt;I want to get the same result but in Databricks, I get errors that CTE cannot be found because I am calling CTE inside the first CTE&lt;/P&gt;&lt;P&gt;I greatly appreciate your help on this. &lt;/P&gt;</description>
      <pubDate>Mon, 09 May 2022 13:59:21 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/converting-sql-code-to-sql-databricks/m-p/21111#M14346</guid>
      <dc:creator>LightUp</dc:creator>
      <dc:date>2022-05-09T13:59:21Z</dc:date>
    </item>
    <item>
      <title>Re: Converting SQL Code to SQL Databricks</title>
      <link>https://community.databricks.com/t5/data-engineering/converting-sql-code-to-sql-databricks/m-p/21112#M14347</link>
      <description>&lt;P&gt;Spark SQL does have Common Table Expressions: &lt;A href="https://spark.apache.org/docs/latest/sql-ref-syntax-qry-select-cte.html" target="test_blank"&gt;https://spark.apache.org/docs/latest/sql-ref-syntax-qry-select-cte.html&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm not an expert at SQL or CTEs, but my guess is that it's the recursion option.  &lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 09 May 2022 15:27:40 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/converting-sql-code-to-sql-databricks/m-p/21112#M14347</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2022-05-09T15:27:40Z</dc:date>
    </item>
    <item>
      <title>Re: Converting SQL Code to SQL Databricks</title>
      <link>https://community.databricks.com/t5/data-engineering/converting-sql-code-to-sql-databricks/m-p/21113#M14348</link>
      <description>&lt;P&gt;Thank you @Joseph Kambourakis​&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="image"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/1880i01A8ED41829B045B/image-size/large?v=v2&amp;amp;px=999" role="button" title="image" alt="image" /&gt;&lt;/span&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The part that is not clear to me from the how to rework the part circled in the image above. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Even this part of the code does not work in databricks:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;DATEADD(month, DATEDIFF(month, 0, DATEADD(month , 1 , EventStartDateTime)), 0)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Tried converting too but not sure which function(s) can replace those to get the same answers&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Looking forward to more ideas&lt;/P&gt;</description>
      <pubDate>Mon, 09 May 2022 15:43:07 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/converting-sql-code-to-sql-databricks/m-p/21113#M14348</guid>
      <dc:creator>LightUp</dc:creator>
      <dc:date>2022-05-09T15:43:07Z</dc:date>
    </item>
    <item>
      <title>Re: Converting SQL Code to SQL Databricks</title>
      <link>https://community.databricks.com/t5/data-engineering/converting-sql-code-to-sql-databricks/m-p/102142#M40982</link>
      <description>&lt;P&gt;&lt;SPAN&gt;you may explore the tool and services from&amp;nbsp;&lt;/SPAN&gt;&lt;A href="https://travinto.com/?utm_source=databricks" target="_blank" rel="noopener nofollow noreferrer"&gt;Travinto Technologies&lt;/A&gt;&lt;SPAN&gt;&amp;nbsp;. They have very good tools. We had explored their tool for our code coversion from&amp;nbsp; Informatica, Datastage and abi initio to DATABRICKS , pyspark. Also we used for SQL queries, stored procedure, trigger, cursor etc.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 14 Dec 2024 18:13:12 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/converting-sql-code-to-sql-databricks/m-p/102142#M40982</guid>
      <dc:creator>thelogicplus</dc:creator>
      <dc:date>2024-12-14T18:13:12Z</dc:date>
    </item>
  </channel>
</rss>

