sqlkata / querybuilder

SQL query builder, written in c#, helps you build complex queries easily, supports SqlServer, MySql, PostgreSql, Oracle, Sqlite and Firebird
https://sqlkata.com
MIT License
3.08k stars 498 forks source link

Join with coalesce #611

Closed marekbe closed 1 year ago

marekbe commented 1 year ago

SqlKata: v.2.3.7 Framework: .NET 6.0 Compiler: PostgresCompiler

I cannot figure out if there is any way to create join with coalesce. When I try this:

var query = new Query("Users")
    .Join("TableA", j =>
    {
        j.On("coalesce(Users.Id, 'null')", "coalesce(TableA.UserId, 'null')");
        return j;
    });

I recive:

SELECT * FROM "Users" 
INNER JOIN "TableA" ON ("coalesce(Users"."Id, 'null')" = "coalesce(TableA"."UserId, 'null')")

but expected result is:

SELECT * FROM "Users" 
INNER JOIN "TableA" ON (coalesce("Users"."Id", 'null') = coalesce("TableA"."UserId", 'null'))

I can't use operator IS NOT DISTINCT FROM because of redshift (by the way this doesn't work in playground) I don't want to use condition like: (x.Id IS NULL AND y.Id IS NULL) OR x.Id = y.Id because of performance I also don't want to use any Raw method like here

I will be grateful for your response, tips etc. :)

marekbe commented 1 year ago

solution from link is acceptable