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.06k stars 499 forks source link

`With` missing if defined in sub query #717

Closed filipagh closed 3 months ago

filipagh commented 3 months ago

hi maybe im not doing something wrong but i have complicated query with with defined in sub query, and compiled query is missing with part

minimal changed docs example

var activePosts = new Query("Comments")
    .Select("PostId")
    .SelectRaw("count(1) as Count")
    .GroupBy("PostId")
    .HavingRaw("count(1) > 100");

var queryWith = new Query("ActivePosts")
    .With("ActivePosts", activePosts) // now you can consider ActivePosts as a regular table in the database
    .Join("ActivePosts", "ActivePosts.PostId", "Posts.Id");

var query = new Query().From(queryWith); 

result : SELECT * FROM ( SELECT * FROM [ActivePosts] INNER JOIN [ActivePosts] ON [ActivePosts].[PostId] = [Posts].[Id] )

WITH "ActivePosts" as is missing

ahmad-moussawi commented 3 months ago

Here the From will not conserve the inner With, using an outer With will do.

This example should work.

var activePosts = new Query("Comments")
    .Select("PostId")
    .SelectRaw("count(1) as Count")
    .GroupBy("PostId")
    .HavingRaw("count(1) > 100");

var queryWith = new Query("ActivePosts")
    .With("ActivePosts", activePosts) // now you can consider ActivePosts as a regular table in the database
    .Join("ActivePosts", "ActivePosts.PostId", "Posts.Id");

var query = new Query().With("sub", queryWith).From("sub"); 
filipagh commented 3 months ago

yes this worked, thanks