diff --git a/src/Collection/Functions/FetchPropertyFunction.php b/src/Collection/Functions/FetchPropertyFunction.php
index 2ad482f8..7fd30db2 100644
--- a/src/Collection/Functions/FetchPropertyFunction.php
+++ b/src/Collection/Functions/FetchPropertyFunction.php
@@ -328,8 +328,11 @@ private function processRelationship(
 					$targetMapper->getManyHasManyParameters($sourceProperty, $currentMapper);
 			}
 
+			$aggregator = $aggregator ?? new AnyAggregator();
 			/** @var literal-string $joinAlias */
 			$joinAlias = DbalQueryBuilderHelper::getAlias($joinTable, array_slice($tokens, 0, $tokenIndex));
+			$joinAlias .= '_' . $aggregator->getAggregateKey();
+
 			$joins[] = new DbalTableJoin(
 				toExpression: "%table",
 				toArgs: [$joinTable],
diff --git a/tests/cases/integration/Collection/collection.aggregation.join.phpt b/tests/cases/integration/Collection/collection.aggregation.join.phpt
index 8027793b..38fb74bf 100644
--- a/tests/cases/integration/Collection/collection.aggregation.join.phpt
+++ b/tests/cases/integration/Collection/collection.aggregation.join.phpt
@@ -49,6 +49,24 @@ class CollectionAggregationJoinTest extends DataTestCase
 	}
 
 
+	public function testIndependentAnyOverManyHasMany(): void
+	{
+		$books = $this->orm->books->findBy([
+			ICollection::AND,
+			[ICollection::AND, new AnyAggregator('1'), 'tags->id' => 1],
+			[ICollection::AND, new AnyAggregator('2'), 'tags->id' => 2],
+		]);
+		Assert::same(1, $books->count());
+
+		$books = $this->orm->books->findBy([
+			ICollection::AND,
+			[ICollection::AND, new AnyAggregator('3'), 'tags->id' => 3],
+			[CompareEqualsFunction::class, [CountAggregateFunction::class, 'tags->id'], 1],
+		]);
+		Assert::same(1, $books->count());
+	}
+
+
 	public function testAnyDependent(): void
 	{
 		/*
diff --git a/tests/sqls/NextrasTests/Orm/Integration/Collection/CollectionAggregationJoinTest_testHasValueOrEmptyWithFunctions.sql b/tests/sqls/NextrasTests/Orm/Integration/Collection/CollectionAggregationJoinTest_testHasValueOrEmptyWithFunctions.sql
index 8c13a7c2..d7561d22 100644
--- a/tests/sqls/NextrasTests/Orm/Integration/Collection/CollectionAggregationJoinTest_testHasValueOrEmptyWithFunctions.sql
+++ b/tests/sqls/NextrasTests/Orm/Integration/Collection/CollectionAggregationJoinTest_testHasValueOrEmptyWithFunctions.sql
@@ -1,2 +1,2 @@
-SELECT "books".* FROM "books" AS "books" LEFT JOIN "books_x_tags" AS "books_x_tags" ON ("books"."id" = "books_x_tags"."book_id") LEFT JOIN "tags" AS "tags_any" ON (("books_x_tags"."tag_id" = "tags_any"."id") AND "tags_any"."id" IN (1)) LEFT JOIN "tags" AS "tags__COUNT" ON ("books_x_tags"."tag_id" = "tags__COUNT"."id") GROUP BY "books"."id" HAVING (((COUNT("tags_any"."id") > 0)) OR (COUNT("tags__COUNT"."id") = 0));
-SELECT COUNT(*) AS count FROM (SELECT "books"."id" FROM "books" AS "books" LEFT JOIN "books_x_tags" AS "books_x_tags" ON ("books"."id" = "books_x_tags"."book_id") LEFT JOIN "tags" AS "tags_any" ON (("books_x_tags"."tag_id" = "tags_any"."id") AND "tags_any"."id" IN (1)) LEFT JOIN "tags" AS "tags__COUNT" ON ("books_x_tags"."tag_id" = "tags__COUNT"."id") GROUP BY "books"."id" HAVING (((COUNT("tags_any"."id") > 0)) OR (COUNT("tags__COUNT"."id") = 0))) temp;
+SELECT "books".* FROM "books" AS "books" LEFT JOIN "books_x_tags" AS "books_x_tags_any" ON ("books"."id" = "books_x_tags_any"."book_id") LEFT JOIN "tags" AS "tags_any" ON (("books_x_tags_any"."tag_id" = "tags_any"."id") AND "tags_any"."id" IN (1)) LEFT JOIN "books_x_tags" AS "books_x_tags__COUNT" ON ("books"."id" = "books_x_tags__COUNT"."book_id") LEFT JOIN "tags" AS "tags__COUNT" ON ("books_x_tags__COUNT"."tag_id" = "tags__COUNT"."id") GROUP BY "books"."id" HAVING (((COUNT("tags_any"."id") > 0)) OR (COUNT("tags__COUNT"."id") = 0));
+SELECT COUNT(*) AS count FROM (SELECT "books"."id" FROM "books" AS "books" LEFT JOIN "books_x_tags" AS "books_x_tags_any" ON ("books"."id" = "books_x_tags_any"."book_id") LEFT JOIN "tags" AS "tags_any" ON (("books_x_tags_any"."tag_id" = "tags_any"."id") AND "tags_any"."id" IN (1)) LEFT JOIN "books_x_tags" AS "books_x_tags__COUNT" ON ("books"."id" = "books_x_tags__COUNT"."book_id") LEFT JOIN "tags" AS "tags__COUNT" ON ("books_x_tags__COUNT"."tag_id" = "tags__COUNT"."id") GROUP BY "books"."id" HAVING (((COUNT("tags_any"."id") > 0)) OR (COUNT("tags__COUNT"."id") = 0))) temp;
diff --git a/tests/sqls/NextrasTests/Orm/Integration/Collection/CollectionAggregationJoinTest_testIndependentAnyOverManyHasMany.sql b/tests/sqls/NextrasTests/Orm/Integration/Collection/CollectionAggregationJoinTest_testIndependentAnyOverManyHasMany.sql
new file mode 100644
index 00000000..72e64bad
--- /dev/null
+++ b/tests/sqls/NextrasTests/Orm/Integration/Collection/CollectionAggregationJoinTest_testIndependentAnyOverManyHasMany.sql
@@ -0,0 +1,2 @@
+SELECT "books".* FROM "books" AS "books" LEFT JOIN "books_x_tags" AS "books_x_tags_1" ON ("books"."id" = "books_x_tags_1"."book_id") LEFT JOIN "tags" AS "tags_1" ON ("books_x_tags_1"."tag_id" = "tags_1"."id") LEFT JOIN "books_x_tags" AS "books_x_tags_2" ON ("books"."id" = "books_x_tags_2"."book_id") LEFT JOIN "tags" AS "tags_2" ON ("books_x_tags_2"."tag_id" = "tags_2"."id") WHERE ((("tags_1"."id" = 1)) AND (("tags_2"."id" = 2))) GROUP BY "books"."id";
+SELECT "books".* FROM "books" AS "books" LEFT JOIN "books_x_tags" AS "books_x_tags_3" ON ("books"."id" = "books_x_tags_3"."book_id") LEFT JOIN "tags" AS "tags_3" ON ("books_x_tags_3"."tag_id" = "tags_3"."id") LEFT JOIN "books_x_tags" AS "books_x_tags__COUNT" ON ("books"."id" = "books_x_tags__COUNT"."book_id") LEFT JOIN "tags" AS "tags__COUNT" ON ("books_x_tags__COUNT"."tag_id" = "tags__COUNT"."id") GROUP BY "books"."id", "tags_3"."id" HAVING ((("tags_3"."id" = 3)) AND (COUNT("tags__COUNT"."id") = 1));
diff --git a/tests/sqls/NextrasTests/Orm/Integration/Collection/CollectionAggregationTest_testCount.sql b/tests/sqls/NextrasTests/Orm/Integration/Collection/CollectionAggregationTest_testCount.sql
index 5aaa93b7..db2482cf 100644
--- a/tests/sqls/NextrasTests/Orm/Integration/Collection/CollectionAggregationTest_testCount.sql
+++ b/tests/sqls/NextrasTests/Orm/Integration/Collection/CollectionAggregationTest_testCount.sql
@@ -1,3 +1,3 @@
-SELECT "books".* FROM "books" AS "books" LEFT JOIN "books_x_tags" AS "books_x_tags" ON ("books"."id" = "books_x_tags"."book_id") LEFT JOIN "tags" AS "tags__COUNT" ON ("books_x_tags"."tag_id" = "tags__COUNT"."id") GROUP BY "books"."id" HAVING (COUNT("tags__COUNT"."id") >= 2) ORDER BY "books"."id" ASC;
-SELECT "books".* FROM "books" AS "books" LEFT JOIN "books_x_tags" AS "books_x_tags" ON ("books"."id" = "books_x_tags"."book_id") LEFT JOIN "tags" AS "tags__COUNT" ON ("books_x_tags"."tag_id" = "tags__COUNT"."id") GROUP BY "books"."id" ORDER BY COUNT("tags__COUNT"."id") ASC, "books"."id" ASC;
-SELECT "books".* FROM "books" AS "books" LEFT JOIN "books_x_tags" AS "books_x_tags" ON ("books"."id" = "books_x_tags"."book_id") LEFT JOIN "tags" AS "tags__COUNT" ON ("books_x_tags"."tag_id" = "tags__COUNT"."id") GROUP BY "books"."id" ORDER BY COUNT("tags__COUNT"."id") DESC, "books"."id" DESC;
+SELECT "books".* FROM "books" AS "books" LEFT JOIN "books_x_tags" AS "books_x_tags__COUNT" ON ("books"."id" = "books_x_tags__COUNT"."book_id") LEFT JOIN "tags" AS "tags__COUNT" ON ("books_x_tags__COUNT"."tag_id" = "tags__COUNT"."id") GROUP BY "books"."id" HAVING (COUNT("tags__COUNT"."id") >= 2) ORDER BY "books"."id" ASC;
+SELECT "books".* FROM "books" AS "books" LEFT JOIN "books_x_tags" AS "books_x_tags__COUNT" ON ("books"."id" = "books_x_tags__COUNT"."book_id") LEFT JOIN "tags" AS "tags__COUNT" ON ("books_x_tags__COUNT"."tag_id" = "tags__COUNT"."id") GROUP BY "books"."id" ORDER BY COUNT("tags__COUNT"."id") ASC, "books"."id" ASC;
+SELECT "books".* FROM "books" AS "books" LEFT JOIN "books_x_tags" AS "books_x_tags__COUNT" ON ("books"."id" = "books_x_tags__COUNT"."book_id") LEFT JOIN "tags" AS "tags__COUNT" ON ("books_x_tags__COUNT"."tag_id" = "tags__COUNT"."id") GROUP BY "books"."id" ORDER BY COUNT("tags__COUNT"."id") DESC, "books"."id" DESC;
diff --git a/tests/sqls/NextrasTests/Orm/Integration/Collection/CollectionHavingTest_testHavingWithSameNamedColumnsInGroupBy.sql b/tests/sqls/NextrasTests/Orm/Integration/Collection/CollectionHavingTest_testHavingWithSameNamedColumnsInGroupBy.sql
index e9374acc..6f724a68 100644
--- a/tests/sqls/NextrasTests/Orm/Integration/Collection/CollectionHavingTest_testHavingWithSameNamedColumnsInGroupBy.sql
+++ b/tests/sqls/NextrasTests/Orm/Integration/Collection/CollectionHavingTest_testHavingWithSameNamedColumnsInGroupBy.sql
@@ -1 +1 @@
-SELECT "books".* FROM "books" AS "books" LEFT JOIN "books_x_tags" AS "books_x_tags" ON ("books"."id" = "books_x_tags"."book_id") LEFT JOIN "tags" AS "tags_any" ON (("books_x_tags"."tag_id" = "tags_any"."id") AND "tags_any"."id" = 1) LEFT JOIN "public"."authors" AS "author" ON ("books"."author_id" = "author"."id") LEFT JOIN "publishers" AS "publisher" ON ("books"."publisher_id" = "publisher"."publisher_id") GROUP BY "books"."id", "author"."name", "publisher"."name" HAVING ((COUNT("tags_any"."id") > 0) OR ("author"."name" = 'Writer 1') OR ("publisher"."name" = 'Nextras publisher A'));
+SELECT "books".* FROM "books" AS "books" LEFT JOIN "books_x_tags" AS "books_x_tags_any" ON ("books"."id" = "books_x_tags_any"."book_id") LEFT JOIN "tags" AS "tags_any" ON (("books_x_tags_any"."tag_id" = "tags_any"."id") AND "tags_any"."id" = 1) LEFT JOIN "public"."authors" AS "author" ON ("books"."author_id" = "author"."id") LEFT JOIN "publishers" AS "publisher" ON ("books"."publisher_id" = "publisher"."publisher_id") GROUP BY "books"."id", "author"."name", "publisher"."name" HAVING ((COUNT("tags_any"."id") > 0) OR ("author"."name" = 'Writer 1') OR ("publisher"."name" = 'Nextras publisher A'));
diff --git a/tests/sqls/NextrasTests/Orm/Integration/Collection/CollectionTest_testDistinct.sql b/tests/sqls/NextrasTests/Orm/Integration/Collection/CollectionTest_testDistinct.sql
index 759f6569..00e24937 100644
--- a/tests/sqls/NextrasTests/Orm/Integration/Collection/CollectionTest_testDistinct.sql
+++ b/tests/sqls/NextrasTests/Orm/Integration/Collection/CollectionTest_testDistinct.sql
@@ -1 +1 @@
-SELECT "tag_followers".* FROM "tag_followers" AS "tag_followers" LEFT JOIN "tags" AS "tag" ON ("tag_followers"."tag_id" = "tag"."id") LEFT JOIN "books_x_tags" AS "tag_books_x_tags" ON ("tag"."id" = "tag_books_x_tags"."tag_id") LEFT JOIN "books" AS "tag_books_any" ON ("tag_books_x_tags"."book_id" = "tag_books_any"."id") WHERE (("tag_books_any"."id" = 1)) GROUP BY "tag_followers"."tag_id", "tag_followers"."author_id";
+SELECT "tag_followers".* FROM "tag_followers" AS "tag_followers" LEFT JOIN "tags" AS "tag" ON ("tag_followers"."tag_id" = "tag"."id") LEFT JOIN "books_x_tags" AS "tag_books_x_tags_any" ON ("tag"."id" = "tag_books_x_tags_any"."tag_id") LEFT JOIN "books" AS "tag_books_any" ON ("tag_books_x_tags_any"."book_id" = "tag_books_any"."id") WHERE (("tag_books_any"."id" = 1)) GROUP BY "tag_followers"."tag_id", "tag_followers"."author_id";
diff --git a/tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipManyHasManyTest_testCountStoredOnManyToManyCondition.sql b/tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipManyHasManyTest_testCountStoredOnManyToManyCondition.sql
index 0d94db00..037d8729 100644
--- a/tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipManyHasManyTest_testCountStoredOnManyToManyCondition.sql
+++ b/tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipManyHasManyTest_testCountStoredOnManyToManyCondition.sql
@@ -1 +1 @@
-SELECT COUNT(*) AS count FROM (SELECT "books"."id" FROM "books" AS "books" LEFT JOIN "books_x_tags" AS "books_x_tags" ON ("books"."id" = "books_x_tags"."book_id") LEFT JOIN "tags" AS "tags_any" ON ("books_x_tags"."tag_id" = "tags_any"."id") WHERE (("tags_any"."name" = 'Tag 2')) GROUP BY "books"."id") temp;
+SELECT COUNT(*) AS count FROM (SELECT "books"."id" FROM "books" AS "books" LEFT JOIN "books_x_tags" AS "books_x_tags_any" ON ("books"."id" = "books_x_tags_any"."book_id") LEFT JOIN "tags" AS "tags_any" ON ("books_x_tags_any"."tag_id" = "tags_any"."id") WHERE (("tags_any"."name" = 'Tag 2')) GROUP BY "books"."id") temp;
diff --git a/tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipManyHasManyTest_testJoinAcrossDifferentPaths.sql b/tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipManyHasManyTest_testJoinAcrossDifferentPaths.sql
index e9bf2f75..16c787eb 100644
--- a/tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipManyHasManyTest_testJoinAcrossDifferentPaths.sql
+++ b/tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipManyHasManyTest_testJoinAcrossDifferentPaths.sql
@@ -1,7 +1,7 @@
-SELECT "books".* FROM "books" AS "books" LEFT JOIN "books_x_tags" AS "books_x_tags" ON ("books"."id" = "books_x_tags"."book_id") LEFT JOIN "tags" AS "tags_any" ON (("books_x_tags"."tag_id" = "tags_any"."id") AND "tags_any"."name" = 'Tag 1') LEFT JOIN "books" AS "nextPart" ON ("books"."next_part" = "nextPart"."id") LEFT JOIN "books_x_tags" AS "nextPart_books_x_tags" ON ("nextPart"."id" = "nextPart_books_x_tags"."book_id") LEFT JOIN "tags" AS "nextPart_tags_any" ON (("nextPart_books_x_tags"."tag_id" = "nextPart_tags_any"."id") AND "nextPart_tags_any"."name" = 'Tag 3') GROUP BY "books"."id" HAVING ((COUNT("tags_any"."id") > 0) OR (COUNT("nextPart_tags_any"."id") > 0)) ORDER BY "books"."id" ASC;
+SELECT "books".* FROM "books" AS "books" LEFT JOIN "books_x_tags" AS "books_x_tags_any" ON ("books"."id" = "books_x_tags_any"."book_id") LEFT JOIN "tags" AS "tags_any" ON (("books_x_tags_any"."tag_id" = "tags_any"."id") AND "tags_any"."name" = 'Tag 1') LEFT JOIN "books" AS "nextPart" ON ("books"."next_part" = "nextPart"."id") LEFT JOIN "books_x_tags" AS "nextPart_books_x_tags_any" ON ("nextPart"."id" = "nextPart_books_x_tags_any"."book_id") LEFT JOIN "tags" AS "nextPart_tags_any" ON (("nextPart_books_x_tags_any"."tag_id" = "nextPart_tags_any"."id") AND "nextPart_tags_any"."name" = 'Tag 3') GROUP BY "books"."id" HAVING ((COUNT("tags_any"."id") > 0) OR (COUNT("nextPart_tags_any"."id") > 0)) ORDER BY "books"."id" ASC;
 START TRANSACTION;
 INSERT INTO "tags" ("name", "is_global") VALUES ('Tag 5', 'y');
 SELECT CURRVAL('"tags_id_seq"');
 INSERT INTO "books_x_tags" ("book_id", "tag_id") VALUES (4, 4);
 COMMIT;
-SELECT "books".* FROM "books" AS "books" LEFT JOIN "books_x_tags" AS "books_x_tags" ON ("books"."id" = "books_x_tags"."book_id") LEFT JOIN "tags" AS "tags_any" ON ("books_x_tags"."tag_id" = "tags_any"."id") LEFT JOIN "books" AS "nextPart" ON ("books"."next_part" = "nextPart"."id") LEFT JOIN "books_x_tags" AS "nextPart_books_x_tags" ON ("nextPart"."id" = "nextPart_books_x_tags"."book_id") LEFT JOIN "tags" AS "nextPart_tags_any" ON ("nextPart_books_x_tags"."tag_id" = "nextPart_tags_any"."id") WHERE (("tags_any"."name" = 'Tag 5') AND ("nextPart_tags_any"."name" = 'Tag 3')) GROUP BY "books"."id" ORDER BY "books"."id" ASC;
+SELECT "books".* FROM "books" AS "books" LEFT JOIN "books_x_tags" AS "books_x_tags_any" ON ("books"."id" = "books_x_tags_any"."book_id") LEFT JOIN "tags" AS "tags_any" ON ("books_x_tags_any"."tag_id" = "tags_any"."id") LEFT JOIN "books" AS "nextPart" ON ("books"."next_part" = "nextPart"."id") LEFT JOIN "books_x_tags" AS "nextPart_books_x_tags_any" ON ("nextPart"."id" = "nextPart_books_x_tags_any"."book_id") LEFT JOIN "tags" AS "nextPart_tags_any" ON ("nextPart_books_x_tags_any"."tag_id" = "nextPart_tags_any"."id") WHERE (("tags_any"."name" = 'Tag 5') AND ("nextPart_tags_any"."name" = 'Tag 3')) GROUP BY "books"."id" ORDER BY "books"."id" ASC;
diff --git a/tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipManyHasOneTest_testProperAggregation.sql b/tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipManyHasOneTest_testProperAggregation.sql
index a35a1b94..414a96f6 100644
--- a/tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipManyHasOneTest_testProperAggregation.sql
+++ b/tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipManyHasOneTest_testProperAggregation.sql
@@ -1,2 +1,2 @@
-SELECT "books".* FROM "books" AS "books" LEFT JOIN "books_x_tags" AS "books_x_tags" ON ("books"."id" = "books_x_tags"."book_id") LEFT JOIN "tags" AS "tags_any" ON ("books_x_tags"."tag_id" = "tags_any"."id") LEFT JOIN "publishers" AS "publisher" ON ("books"."publisher_id" = "publisher"."publisher_id") WHERE (("tags_any"."id" = 1) AND ("publisher"."name" = 'Nextras publisher A')) GROUP BY "books"."id";
-SELECT "books".* FROM "books" AS "books" LEFT JOIN "books_x_tags" AS "books_x_tags" ON ("books"."id" = "books_x_tags"."book_id") LEFT JOIN "tags" AS "tags_none" ON (("books_x_tags"."tag_id" = "tags_none"."id") AND "tags_none"."id" = 1) LEFT JOIN "publishers" AS "publisher" ON ("books"."publisher_id" = "publisher"."publisher_id") GROUP BY "books"."id", "publisher"."name" HAVING ((COUNT("tags_none"."id") = 0) AND ("publisher"."name" = 'Nextras publisher A'));
+SELECT "books".* FROM "books" AS "books" LEFT JOIN "books_x_tags" AS "books_x_tags_any" ON ("books"."id" = "books_x_tags_any"."book_id") LEFT JOIN "tags" AS "tags_any" ON ("books_x_tags_any"."tag_id" = "tags_any"."id") LEFT JOIN "publishers" AS "publisher" ON ("books"."publisher_id" = "publisher"."publisher_id") WHERE (("tags_any"."id" = 1) AND ("publisher"."name" = 'Nextras publisher A')) GROUP BY "books"."id";
+SELECT "books".* FROM "books" AS "books" LEFT JOIN "books_x_tags" AS "books_x_tags_none" ON ("books"."id" = "books_x_tags_none"."book_id") LEFT JOIN "tags" AS "tags_none" ON (("books_x_tags_none"."tag_id" = "tags_none"."id") AND "tags_none"."id" = 1) LEFT JOIN "publishers" AS "publisher" ON ("books"."publisher_id" = "publisher"."publisher_id") GROUP BY "books"."id", "publisher"."name" HAVING ((COUNT("tags_none"."id") = 0) AND ("publisher"."name" = 'Nextras publisher A'));
diff --git a/tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipOneHasManyTest_testCountStoredOnOneHasManyRelationshipCondition.sql b/tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipOneHasManyTest_testCountStoredOnOneHasManyRelationshipCondition.sql
index 048372c9..81622c38 100644
--- a/tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipOneHasManyTest_testCountStoredOnOneHasManyRelationshipCondition.sql
+++ b/tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipOneHasManyTest_testCountStoredOnOneHasManyRelationshipCondition.sql
@@ -1,3 +1,3 @@
 SELECT "publishers".* FROM "publishers" AS "publishers" WHERE (("publishers"."publisher_id" = 1));
-SELECT "publisher_id", COUNT(DISTINCT "count") as "count" FROM (SELECT "books".*, "books"."id" AS "count" FROM "books" AS "books" LEFT JOIN "books_x_tags" AS "books_x_tags" ON ("books"."id" = "books_x_tags"."book_id") LEFT JOIN "tags" AS "tags_any" ON ("books_x_tags"."tag_id" = "tags_any"."id") WHERE ((("tags_any"."id" = 1))) AND ("books"."publisher_id" IN (1)) GROUP BY "books"."id") AS "temp" GROUP BY "publisher_id";
-SELECT "publisher_id", COUNT(DISTINCT "count") as "count" FROM (SELECT "books".*, "books"."id" AS "count" FROM "books" AS "books" LEFT JOIN "books_x_tags" AS "books_x_tags" ON ("books"."id" = "books_x_tags"."book_id") LEFT JOIN "tags" AS "tags_any" ON (("books_x_tags"."tag_id" = "tags_any"."id") AND "tags_any"."id" = 1) WHERE "books"."publisher_id" IN (1) GROUP BY "books"."id", "books"."title" HAVING (("books"."title" = 'Book 1') OR (COUNT("tags_any"."id") > 0))) AS "temp" GROUP BY "publisher_id";
+SELECT "publisher_id", COUNT(DISTINCT "count") as "count" FROM (SELECT "books".*, "books"."id" AS "count" FROM "books" AS "books" LEFT JOIN "books_x_tags" AS "books_x_tags_any" ON ("books"."id" = "books_x_tags_any"."book_id") LEFT JOIN "tags" AS "tags_any" ON ("books_x_tags_any"."tag_id" = "tags_any"."id") WHERE ((("tags_any"."id" = 1))) AND ("books"."publisher_id" IN (1)) GROUP BY "books"."id") AS "temp" GROUP BY "publisher_id";
+SELECT "publisher_id", COUNT(DISTINCT "count") as "count" FROM (SELECT "books".*, "books"."id" AS "count" FROM "books" AS "books" LEFT JOIN "books_x_tags" AS "books_x_tags_any" ON ("books"."id" = "books_x_tags_any"."book_id") LEFT JOIN "tags" AS "tags_any" ON (("books_x_tags_any"."tag_id" = "tags_any"."id") AND "tags_any"."id" = 1) WHERE "books"."publisher_id" IN (1) GROUP BY "books"."id", "books"."title" HAVING (("books"."title" = 'Book 1') OR (COUNT("tags_any"."id") > 0))) AS "temp" GROUP BY "publisher_id";
diff --git a/tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipOneHasManyTest_testOrderByDifferentTableColumnOnHasManyRelationshipCondition.sql b/tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipOneHasManyTest_testOrderByDifferentTableColumnOnHasManyRelationshipCondition.sql
index 95adf688..0e2f0b50 100644
--- a/tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipOneHasManyTest_testOrderByDifferentTableColumnOnHasManyRelationshipCondition.sql
+++ b/tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipOneHasManyTest_testOrderByDifferentTableColumnOnHasManyRelationshipCondition.sql
@@ -1,2 +1,2 @@
 SELECT "publishers".* FROM "publishers" AS "publishers" WHERE (("publishers"."publisher_id" = 1));
-SELECT "books".* FROM "books" AS "books" LEFT JOIN "books_x_tags" AS "books_x_tags" ON ("books"."id" = "books_x_tags"."book_id") LEFT JOIN "tags" AS "tags_any" ON ("books_x_tags"."tag_id" = "tags_any"."id") LEFT JOIN "public"."authors" AS "author" ON ("books"."author_id" = "author"."id") WHERE ((("tags_any"."id" = 1))) AND ("books"."publisher_id" IN (1)) GROUP BY "books"."id", "author"."name" ORDER BY "author"."name" ASC;
+SELECT "books".* FROM "books" AS "books" LEFT JOIN "books_x_tags" AS "books_x_tags_any" ON ("books"."id" = "books_x_tags_any"."book_id") LEFT JOIN "tags" AS "tags_any" ON ("books_x_tags_any"."tag_id" = "tags_any"."id") LEFT JOIN "public"."authors" AS "author" ON ("books"."author_id" = "author"."id") WHERE ((("tags_any"."id" = 1))) AND ("books"."publisher_id" IN (1)) GROUP BY "books"."id", "author"."name" ORDER BY "author"."name" ASC;
diff --git a/tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipOneHasManyTest_testSameTableJoinWithImplicitAggregation.sql b/tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipOneHasManyTest_testSameTableJoinWithImplicitAggregation.sql
index bef4622c..e1036c21 100644
--- a/tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipOneHasManyTest_testSameTableJoinWithImplicitAggregation.sql
+++ b/tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipOneHasManyTest_testSameTableJoinWithImplicitAggregation.sql
@@ -1,2 +1,2 @@
-SELECT COUNT(*) AS count FROM (SELECT "books"."id" FROM "books" AS "books" LEFT JOIN "books_x_tags" AS "books_x_tags" ON ("books"."id" = "books_x_tags"."book_id") LEFT JOIN "tags" AS "tags_any" ON ((("books_x_tags"."tag_id" = "tags_any"."id") AND "tags_any"."id" IN (1)) OR (("books_x_tags"."tag_id" = "tags_any"."id") AND "tags_any"."id" IS NULL)) GROUP BY "books"."id" HAVING (((COUNT("tags_any"."id") > 0)) OR ((COUNT("tags_any"."id") > 0)))) temp;
-SELECT "books".* FROM "books" AS "books" LEFT JOIN "books_x_tags" AS "books_x_tags" ON ("books"."id" = "books_x_tags"."book_id") LEFT JOIN "tags" AS "tags_any" ON ((("books_x_tags"."tag_id" = "tags_any"."id") AND "tags_any"."id" IN (1)) OR (("books_x_tags"."tag_id" = "tags_any"."id") AND "tags_any"."id" IS NULL)) GROUP BY "books"."id" HAVING (((COUNT("tags_any"."id") > 0)) OR ((COUNT("tags_any"."id") > 0)));
+SELECT COUNT(*) AS count FROM (SELECT "books"."id" FROM "books" AS "books" LEFT JOIN "books_x_tags" AS "books_x_tags_any" ON ("books"."id" = "books_x_tags_any"."book_id") LEFT JOIN "tags" AS "tags_any" ON ((("books_x_tags_any"."tag_id" = "tags_any"."id") AND "tags_any"."id" IN (1)) OR (("books_x_tags_any"."tag_id" = "tags_any"."id") AND "tags_any"."id" IS NULL)) GROUP BY "books"."id" HAVING (((COUNT("tags_any"."id") > 0)) OR ((COUNT("tags_any"."id") > 0)))) temp;
+SELECT "books".* FROM "books" AS "books" LEFT JOIN "books_x_tags" AS "books_x_tags_any" ON ("books"."id" = "books_x_tags_any"."book_id") LEFT JOIN "tags" AS "tags_any" ON ((("books_x_tags_any"."tag_id" = "tags_any"."id") AND "tags_any"."id" IN (1)) OR (("books_x_tags_any"."tag_id" = "tags_any"."id") AND "tags_any"."id" IS NULL)) GROUP BY "books"."id" HAVING (((COUNT("tags_any"."id") > 0)) OR ((COUNT("tags_any"."id") > 0)));