SELECT Statement in Snowflake and Databricks

The SELECT statement in Snowflake continues to amazed me. I've mentioned about the EXCLUDE clause before (link) which enables us to select all columns except one or two. And I've mentioned about the time travel, which is very cool. Today I'm going to write about a SELECT statement feature in Snowflake which is not available in SQL Server or Synapse. This wonderful feature is available in Databricks too.

Say you need to process column 1 and column 2 from table 1, and then combine them.

So need to write a CTE, like this:

WITH CTE1 AS
( SELECT 
	SomeFunction(Column1) AS X,
	SomeFunction(Column2) as Y
  FROM Table1
  WHERE ...
)
SELECT CONCAT(X,' ',Y) AS Z
FROM CTE1        

Sometimes we end up defining multiple layers of CTEs in order to process the output of the previous process.

Like this:

WITH CTE1 AS
( ... ),
CTE2 AS
( SELECT ...
  FROM CTE1
),
CTE3 AS
( SELECT ...
  FROM CTE2
)
SELECT ...
FROM CTE3         

That era in SQL Server or Oracle is now gone. In Snowflake and Databricks in the same select we can access the previously mentioned column.

Like this:

SELECT 
	SomeFunction(Column1) AS X,
	SomeFunction(X) AS Y1,
	SomeFunction(X) AS Y2,
	SomeFunction(X) AS Y3,
FROM ...        

You see, previously in SQL Server and Oracle we would need to put X in CTE1, so that we can use it multiple times for Y1, Y2, Y3.

But in Snowflake and Databricks we can just put X as the first column, and then all subsequent columns can access X. Very useful, isn't it?

If you haven't tried it before try it as soon as you can. You'll find it a useful feature to have. Whether you use Snowflake or Databricks, you've got to try this feature. It's cool.

The other thing that continue to amaze me in Snow SQL is the regex. Here's an example that I did this week: finding the last space in a string. It's as simple as this:

SELECT Column1, REGEXP_INSTR(Column1, '[ ][^ ]*$') AS LAST_SPACE
FROM Table1        

Quite simple, isn't it?

The [ ] means a space.

The *$ means "search until the end of the string"

So if you do REGEXP_INSTR(Column1, '[ ]*$') you'll find the last space right?

Wrong, it will return you the length of the string plus 1. It is as if it thinks that the end of the string is a space.

To do it we put a caret (^). In regex a caret means "not". So [^ ] means not a space.

So [ ][^ ] means: "a space followed by not space".

This way it will find that last space in the string.

Thanks to JNevill for this technique: link.

Joakim Dalby

Consultant database, BI, data warehouse, data mart, cube, ETL, SQL, analysis, design, development, documentation, test, management, SQL Server, Access, ADP+, Kimball practitioner. JOIN people ON data.

6 个月

Function call is performance expensive.

要查看或添加评论,请登录

Vincent Rainardi的更多文章

  • Snowflake vs SQL Server

    Snowflake vs SQL Server

    Sometimes we need to remind ourselves that Snowflake is not an OLTP database. I know today is the era of Hybrid tables…

    6 条评论
  • Data engineer becoming solution architect

    Data engineer becoming solution architect

    Are you a data engineer thinking about transitioning to a cloud solution architect? Data engineer are good with…

    2 条评论
  • Asset Mgt vs Fund Mgt vs Investment Mgt vs Wealth Mgt: What's the difference?

    Asset Mgt vs Fund Mgt vs Investment Mgt vs Wealth Mgt: What's the difference?

    If you work in banking or investment or any other sector in financial services, you might be wondering about the above.…

  • Data Warehousing Basics: Cost

    Data Warehousing Basics: Cost

    If you call yourself a data engineer you need to be aware of 2 additional things compared to a developer. The first one…

    2 条评论
  • My Linkedin post & articles

    My Linkedin post & articles

    The list below goes back to Nov 2024. For older than that see here.

    9 条评论
  • Data Warehousing Basics: Single Customer View

    Data Warehousing Basics: Single Customer View

    Imagine that you work for an insurance company who sell health insurance (HI), life insurance (LI), general insurance…

    2 条评论
  • Data Warehousing Basics: NFR

    Data Warehousing Basics: NFR

    What I’m about to tell you today failed a lot of data warehousing projects which is why it’s worth paying attention so…

    1 条评论
  • ML and AI - What's the difference?

    ML and AI - What's the difference?

    Machine Learning covers about 20-30 algorithms such as Logistic Regression, Decision Tree, Gradient Boosting, Random…

    4 条评论
  • Microsoft Fabric or Synapse Analytics?

    Microsoft Fabric or Synapse Analytics?

    When it comes to Data Warehousing, Microsoft is confusing. Why? Because it has Microsoft Fabric and it also has Synapse…

    16 条评论
  • Data Warehousing Basics: Transformations

    Data Warehousing Basics: Transformations

    As Bill Inmon said, T is the most difficult thing to do in the ETL. And that is why ETL vendors swap it around - they…

    2 条评论

社区洞察

其他会员也浏览了