microsoft / dbt-fabric

MIT License
79 stars 28 forks source link

Limit issue: Order by items must be in the select list if distinct is specified #229

Open colin-rogers-dbt opened 1 month ago

colin-rogers-dbt commented 1 month ago

Looks like the current approach to adding a limit to queries doesn't work if the user adds distinct to their model, example

with

source as (

    select * from "raw_emails"
),

renamed as (

    select

        id as email_id,
        email,
        email_top_level_domain

    from source

)

select distinct * from renamed order by (select null)
            offset 0 rows fetch first 1 rows only

Error: ('42000', '[42000] [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]ORDER BY items must appear in the select list if SELECT DISTINCT is specified. (145) (SQLExecDirectW)')

We might be able to get around this by wrapping in a subquery