Сравнить результаты двух подзапросов в SQLKata

Я хочу сравнить результаты двух подзапросов в WHERE-секции в SQLKata

В SQL это выглядит следующим образом:

WHERE (SELECT count(id) FROM main.someTable) = (SELECT count(id) FROM main.anotherTable)

В SQLKata я могу сравнить результат подзапроса со скалярной величиной:

var mainSubquery = new Query("main.someTable")
            .SelectRaw("count(id)");

var anotherSubquery = new Query("main.anotherTable")
            .SelectRaw("count(id)");

query
    .WhereSub(mainSubquery, "=", 0)

Но я не могу сравнить результаты двух подзапросов друг с другом, например, таким способом:

query
    .WhereSub(mainSubquery, "=", anotherSubquery)

Как следует сравнивать? Возможно стоит выполнить оба подзапроса отдельно и потом сравнить результат их выполнения?

UPD: получил ответ от автора библиотеки

https://stackoverflow.com/questions/74939077/how-to-compare-result-of-two-subqueries-in-where-clause-sqlkata/74948798#74948798


Ответы (1 шт):

Автор решения: Alexander Pavlov

Можно посчитать разницу между двумя наборами ((A-B) + (B-A)), а потом убедиться, что она пустая

with A as (
  select col1, col2, col3 from table1
), B as (
  select col1, col2, col3 from table2
)  
select 
  not exists(
    (select * from B except all select * from A)
    union all 
    (select * from A except all select * from B)
  ) as "IsEqual?"

Например, запрос ниже возвращает false, пока все строки, помеченные -- * не будут удалены из обоих таблиц. После этого становится true

with A as (
  select 1 id, 'a1' str
  union all select 1 id, 'a1' str
  union all select 2 id, 'a2' str
  union all select 2 id, 'a2' str -- *
  union all select 3 id, 'a3' str 
  union all select 4 id, 'a4' str -- *
  union all select 4 id, 'a4' str -- *
  union all select 5 id, 'a5' str -- *
), B as (
  select 1 id, 'a1' str
  union all select 1 id, 'a1' str
  union all select 2 id, 'a2' str
  union all select 3 id, 'a3' str -- *
  union all select 3 id, 'a3' str
  union all select 6 id, 'a6' str -- *
)  
select 
  not exists(
    select * from B
    except all select * from A
    union all (select * from A
    except all select * from B)
  ) as "IsEqual?"

SQLKata умеет except / union https://sqlkata.com/docs/combine

→ Ссылка