Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Group HAVING filters #476

Closed
yankyhgoflow opened this issue May 10, 2021 · 1 comment · Fixed by #477
Closed

Group HAVING filters #476

yankyhgoflow opened this issue May 10, 2021 · 1 comment · Fixed by #477

Comments

@yankyhgoflow
Copy link
Contributor

yankyhgoflow commented May 10, 2021

Hi,

When using WHERE filters we might want to group some filters together, using the following filter as an example

-- Postgres
SELECT * FROM ""Table1""
WHERE (""Column1"" = 10 OR ""Column2"" = 20) AND ""Column3"" = 30

If we wouldn't group the OR filters the AND will take precedence, SqlKata has the mechanism to generate the above query perfectly well

new Query("Table1")
    .Where(q => q.Or().Where("Column1", 10).Or().Where("Column2", 20))
    .Where("Column3", 30);

My question is regarding the same functionality but with a HAVING clause, I understood that the API will be the same, but when trying the following code

new Query("Table1")
    .Having(q => q.Or().HavingRaw("SUM([Column1]) = ?", 10).Or().HavingRaw("SUM([Column2]) = ?", 20))
    .HavingRaw("SUM([Column3]) = ?", 30);

This is the generated SQL

SELECT * FROM "Table1"
HAVING AND SUM("Column3")

The filters within the filter group .Having(q => q...) is missing and the HAVING clause just starts with an AND

Sample test methods

using SqlKata.Compilers;
using SqlKata.Tests.Infrastructure;
using Xunit;

namespace SqlKata.Tests
{
    public class WhereTests : TestSupport
    {
        [Fact]
        public void GroupedWhereFilters()
        {
            var q = new Query("Table1")
                .Where(q => q.Or().Where("Column1", 10).Or().Where("Column2", 20))
                .Where("Column3", 30);

            var c = Compile(q);

            Assert.Equal(@"SELECT * FROM ""Table1"" WHERE (""Column1"" = 10 OR ""Column2"" = 20) AND ""Column3"" = 30", c[EngineCodes.PostgreSql]);
        }

        [Fact]
        public void GroupedHavingFilters()
        {
            var q = new Query("Table1")
                .Having(q => q.Or().HavingRaw("SUM([Column1]) = ?", 10).Or().HavingRaw("SUM([Column2]) = ?", 20))
                .HavingRaw("SUM([Column3]) = ?", 30);

            var c = Compile(q);

            Assert.Equal(@"SELECT * FROM ""Table1"" HAVING (SUM(""Column1"") = 10 OR SUM(""Column2"") = 20) AND SUM(""Column3"") = 30", c[EngineCodes.PostgreSql]);
        }
    }
}
@yankyhgoflow
Copy link
Contributor Author

yankyhgoflow commented May 11, 2021

@ahmad-moussawi
I'm pleased that I was able to help out an solving this issue, just wondering how your release cycle goes as in to when I can expect to see this fix in the nuget package.
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant