计算一个案例或如果为空
counting a case OR if null
我很难弄清楚如何在此处整合我的逻辑。
有两种情况:
- cn.create_time 为空
- cn.create_time 不为空,在这种情况下它包含时间戳
我的 select 查询的开头如下所示:
select
c.eid,
count(c.eid),
count(case when c.last_mod < cn.create_time then c.eid end)
但我需要的是使带有 case 语句的行成为(在 EnSQLish 中)
"if cn.create_time is null then count(c.last_mod), else count(case when c.last_mod < cn.create_time then c.eid end)."
换句话说,如果 create_time 为空,则计算所有记录,否则仅计算 last_mod 小于 create_time 的记录。
我该怎么做?
您在找这样的东西吗?
SELECT c.eid,
COUNT(c.edi),
SUM
(
CASE
WHEN cn.create_time IS NULL OR c.last_mod < cn.create_time THEN 1
ELSE 0
END
)
您可以进行两次查询而不是一次查询:
select eid, count(eid) FROM Table WHERE create_time IS NULL
select eid, count(eid) FROM Table WHERE NOT create_time IS NULL AND last_mod < create_time
然后如果你需要你可以加入。
我会在指标上使用 SUM()
select
c.eid,
sum(IF(c.last_mod < coalesce(cn.create_time,c.last_mod),0,1)),
如果 cn.create_time
为空 coalesce
将 return c.last_mod
我很难弄清楚如何在此处整合我的逻辑。
有两种情况:
- cn.create_time 为空
- cn.create_time 不为空,在这种情况下它包含时间戳
我的 select 查询的开头如下所示:
select
c.eid,
count(c.eid),
count(case when c.last_mod < cn.create_time then c.eid end)
但我需要的是使带有 case 语句的行成为(在 EnSQLish 中)
"if cn.create_time is null then count(c.last_mod), else count(case when c.last_mod < cn.create_time then c.eid end)."
换句话说,如果 create_time 为空,则计算所有记录,否则仅计算 last_mod 小于 create_time 的记录。
我该怎么做?
您在找这样的东西吗?
SELECT c.eid,
COUNT(c.edi),
SUM
(
CASE
WHEN cn.create_time IS NULL OR c.last_mod < cn.create_time THEN 1
ELSE 0
END
)
您可以进行两次查询而不是一次查询:
select eid, count(eid) FROM Table WHERE create_time IS NULL
select eid, count(eid) FROM Table WHERE NOT create_time IS NULL AND last_mod < create_time
然后如果你需要你可以加入。
我会在指标上使用 SUM()
select c.eid, sum(IF(c.last_mod < coalesce(cn.create_time,c.last_mod),0,1)),
如果 cn.create_time
为空 coalesce
将 return c.last_mod