Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Snowflake SQL group-by behaving differently depending whether columns are referenced by position or alias

I am trying to understand why the group by function is yielding different results in snowflake depending on how I reference the group-by fields. Here are two Queries that I believe should yield the same result, but do NOT:

Query using explicit field alias references:

select
    hash('SHA2_256', CONCAT('field1','field2','field3','field4')) as hash
    ,field1
    ,field2
    ,field3
    ,field4
    ,count(*) as count
from <table>
    where 
        <some filters>
    group by hash, field1, field2, field3, field4;

Query using positional references to fields:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

select
    hash('SHA2_256', CONCAT('field1','field2','field3','field4')) as hash
    ,field1
    ,field2
    ,field3
    ,field4
    ,count(*) as count
from <table>
    where 
        <same filters as above>
    group by 1,2,3,4,5;

The first query yields significantly more records, suggesting maybe it isn’t applying a grouping field that is being applied in the second query, but based on the snowflake docs I really believe they should be the same. How are these two different?

>Solution :

The clue is that the aliased expression hash does not overshadow existing columns so:

select
    hash('SHA2_256', CONCAT('field1','field2','field3','field4')) as hash
    ,field1
    ,field2
    ,field3
    ,field4
    ,count(*) as count
from <table>
where <some filters>
group by hash, field1, field2, field3, field4;

is

select
    hash('SHA2_256', CONCAT('field1','field2','field3','field4')) as hash
    ,field1
    ,field2
    ,field3
    ,field4
    ,count(*) as count
from <table>
where <some filters>
group by <table>.hash, field1, field2, field3, field4;

which is different than:

select
    hash('SHA2_256', CONCAT('field1','field2','field3','field4')) as hash
    ,field1
    ,field2
    ,field3
    ,field4
    ,count(*) as count
from <table>
where <same filters as above>
group by 1,2,3,4,5;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading