我对报告有以下SQL请求。
select customers."AppId", second_dep "SecondDeps", first_dep "FirstDeps",
customers_count "Customers", registrations "Registrations"
From (select Count("AppId") as customers_count, "AppId"
FROM "Customers"
join "Advertisers" A on "Customers"."AdvertiserId" = A."AdvertiserId"
join "Categories" C2 on "Customers"."CategoryId" = C2."CategoryId"
where A."Name" in (:AdvertiserNames)
AND C2."Name" = :CategoryName
GROUP BY "AppId"
) as customers
left join
(select C."AppId", count(CE.*) as second_dep
from "CustomerEvents" as CE
inner join "Customers" C on CE."CustomerId" = C."CustomerId"
WHERE "EventType" = 'deposit'
and "Again" = TRUE
GROUP BY C."AppId") as dep2 on customers."AppId" = dep2."AppId"
left join
(select C."AppId", count(CE.*) as first_dep
from "CustomerEvents" as CE
inner join "Customers" C on CE."CustomerId" = C."CustomerId"
WHERE "EventType" = 'deposit'
and "Again" = false
GROUP BY C."AppId") as dep on customers."AppId" = dep."AppId"
left join
(select C."AppId", count(CE.*) as registrations
from "CustomerEvents" as CE
inner join "Customers" C on CE."CustomerId" = C."CustomerId"
WHERE "EventType" = 'registration'
GROUP BY C."AppId") as regs on regs."AppId" = customers."AppId";
有问题的字符串是
where A."Name" in (:AdvertiserNames)
如果AdvertiserNames
为空,我想跳过它。可能吗?好的,我可以在代码端进行检查,但是这种方式将导致我复制整个请求,但有一点点不同(我的意思是如果AdvertiserNames
是空的,而没有where A."Name" in (:AdvertiserNames)
的话运行SQL)。或者,我可以使用串联来获取合适的SQL。我也不喜欢这种方式。
关于我的技术栈。它是带有PostgreSQL的.NET Core 2.2。这是整个报告方法的代码:
public IQueryable<ByApplicationsReportModel> ByApplications(string category, List<string> advertisers)
{
var rawSql = new RawSqlString(@"
select customers.""AppId"", second_dep ""SecondDeps"", first_dep ""FirstDeps"",
customers_count ""Customers"", registrations ""Registrations""
From (select Count(""AppId"") as customers_count, ""AppId""
FROM ""Customers""
join ""Advertisers"" A on ""Customers"".""AdvertiserId"" = A.""AdvertiserId""
join ""Categories"" C2 on ""Customers"".""CategoryId"" = C2.""CategoryId""
where A.""Name"" in (@AdvertiserNames)
AND C2.""Name"" = @CategoryName
GROUP BY ""AppId""
) as customers
left join
(select C.""AppId"", count(CE.*) as second_dep
from ""CustomerEvents"" as CE
inner join ""Customers"" C on CE.""CustomerId"" = C.""CustomerId""
WHERE ""EventType"" = 'deposit'
and ""Again"" = TRUE
GROUP BY C.""AppId"") as dep2 on customers.""AppId"" = dep2.""AppId""
left join
(select C.""AppId"", count(CE.*) as first_dep
from ""CustomerEvents"" as CE
inner join ""Customers"" C on CE.""CustomerId"" = C.""CustomerId""
WHERE ""EventType"" = 'deposit'
and ""Again"" = false
GROUP BY C.""AppId"") as dep on customers.""AppId"" = dep.""AppId""
left join
(select C.""AppId"", count(CE.*) as registrations
from ""CustomerEvents"" as CE
inner join ""Customers"" C on CE.""CustomerId"" = C.""CustomerId""
WHERE ""EventType"" = 'registration'
GROUP BY C.""AppId"") as regs on regs.""AppId"" = customers.""AppId""");
var advertisersParam = new NpgsqlParameter("AdvertiserNames",
string.Join(",", advertisers) );
var categoryParam = new NpgsqlParameter("CategoryName", category);
return _context.ByApplicationsReportModels
.FromSql(rawSql, categoryParam, advertisersParam);
}
有什么想法吗?
答案 0 :(得分:3)
您可以尝试将where A.""Name"" in (@AdvertiserNames)
更改为where (A.""Name"" in (@AdvertiserNames) or @AdvertiserNames = '')
。
答案 1 :(得分:1)
您可以将字符串数组直接传递给查询,而不是将广告客户串联成字符串:
var advertisersParam = new NpgsqlParameter("AdvertiserNames", advertisers));
在SQL中,您需要更改为x IN (@advertisers)
,而不是使用x = ANY (@advertisers)
构造。
注意:如果您希望在@advertisers为空时通过检查,则仍需要一个附加子句。