我有一个Postgresql-14的查询如下,如果我在DBeaver 23.1.4上运行它找到了记录。
我用:
Npgsql.EntityFrameworkCore.PostgreSQL
7.0.4附加信息:
process_datetime
和 start_datetime
的数据类型为 timestamptz
。
我不明白的是为什么它在 DBeaver 23.1.4 上可以正常工作?
我将查询复制到名为“sql”的字符串变量中,但是如果我在 Net Core 6 (C#) 代码上运行它,我没有找到记录,我错过了什么?
select
t.organizations_pid,
coalesce(sum(t.sum_usage),0) * 1.05 totalUsage,
coalesce(count(t.iccid), 0) numSIMs
from
(select
s.organizations_pid, c2.pid cdrs_pid, c2.cdr_id,
c2.iccid, c2.network, c1.sum_usage,
c2.start_datetime, c2.status
from
(select
s.organizations_pid, c1.iccid,
sum(c1.usage) sum_usage, max(c1.process_datetime) max_process_datetime
from
cdrs c1
inner join
sims s on c1.iccid = s.iccid
where
s.organizations_pid = 29
and to_char(c1.start_datetime,'YYYY-MM-DD') >= '2023-08-01'
and to_char(c1.start_datetime,'YYYY-MM-DD') <= '2023-08-07'
and c1.is_beginbal = false
group by
s.organizations_pid, c1.iccid) c1
inner join
cdrs c2 on c2.process_datetime = c1.max_process_datetime
and c2.iccid = c1.iccid
inner join
sims s on c2.iccid = s.iccid
and s.last_cdr_id = c2.cdr_id
inner join
organizations o on s.organizations_pid = o.pid
where
s.organizations_pid = 29
and s.attributes->'subscription'->> 'subscriptionStatus' = 'ACTIVE') t
group by
t.organizations_pid
我的 C# 代码如下:
using (var dr = conn.ExecuteReader(sql, new { }))
{
if (dr.Read())
{
double totalUsage = dr.GetDouble(dr.GetOrdinal("totalUsage"));
}
}
我也尝试了下面,但结果返回null。
var result = conn.Query(sql, new { }).SingleOrDefault();
我通过改变
发现了这一点to_char(c1.start_datetime,'YYYY-MM-DD') >=
到
DATE_TRUNC('day',c.start_datetime at TIME zone 'UTC') >=