- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我每天早上 7:45 到下午 5 点在任务计划中运行一个 php 脚本。它工作得很好,但第一次运行时它不会返回任何内容,因为尚未创建记录(记录是由接听电话的 CSR 代理创建的)。我在脚本中运行 SQL 查询来选择、聚合值并将其插入表中,然后根据这些值创建一个 JSON 文件。再说一遍,除了第一次运行之外,所有这些都是完美的。
有人问我是否可以添加一些内容,这样如果第一个作业没有返回任何内容,那么它只会为所有内容插入 0。这样,即使运行了 5 次,系统中没有记录/调用,仍然会有数据,并且 CSR 屏幕会显示一些内容。有没有一种相当简单的方法来做到这一点?
这是脚本的大部分内容:
$data = mysqli_query($conn2,
"SELECT
case
when callingpartyno in (select extension from test.test_users)
then callingpartyno
when finallycalledpartyno in (select extension from test.test_users)
then finallycalledpartyno
end as extension
, sum(duration) as total_talk_time_seconds
, round(sum(duration) / 60,2) as total_talk_time_minutes
, sum(if(legtype1 = 1,1,0)) as total_outbound
, sum( case when(legtype1 = 1 and duration > 60) then 1 else 0 end) as credit_for_outbound
, sum(if(legtype1 = 2,1,0) and answered = 1) as total_inbound
, sum(if(legtype1 = 2,1,0) and answered = 0) as total_missed
, sum(if(legtype1 = 1, 1, 0)) + -- outbound calls
sum(if(legtype1 = 2, 1, 0)) as total_calls
,round((sum(if(legtype1 = 2,1,0) and answered = 1))/(sum(if(legtype1 = 1, 1, 0)) + -- outbound calls
sum(if(legtype1 = 2, 1, 0))) * 100,2) as percent_answered
, now() as time_of_report
, curdate() as date_of_report
FROM
test.session a
join test.callsummary b
on a.notablecallid = b.notablecallid
where
date(a.ts) >= curdate()
and (
callingpartyno in (select extension from test.test_users
)
or finallycalledpartyno in (select extension from test.test_users
)
)
group by
extension") or die(mysqli_error( $conn2));
$stmt = mysqli_prepare($conn2,
"INSERT into test.test_totals
(extension,
total_talk_time_seconds,
total_talk_time_minutes,
total_outbound,
credit_for_outbound,
total_inbound,
missed_calls,
total_calls,
percent_answered,
date_of_report,
time_of_report)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON duplicate key
update
total_talk_time_seconds = values(total_talk_time_seconds),
total_talk_time_minutes = values(total_talk_time_minutes),
total_inbound = values(total_inbound),
total_outbound = values(total_outbound),
credit_for_outbound = values(credit_for_outbound),
missed_calls = values(missed_calls),
total_calls = values(total_calls),
percent_answered = values(percent_answered),
time_of_report = NOW()") or die(mysqli_error( $conn2));
最佳答案
问题的出现是因为有时分机行数为零(没有调用、没有分机、没有结果)
更改您的查询,以便您在 select 中使用的 now() 来自一个简单的子查询,并使用始终为 true 的 ON 子句将其余数据左连接到它
参见http://sqlfiddle.com/#!9/9b2a1/6例如,产生伪表 t 的子查询将始终返回一行。如果 Table1 子查询返回一行或多行,则 t 行将完全相同地重复。如果 Table1 子查询返回零行,则整个查询仍将有一行。此查询永远不能返回零行;这是您需要对查询执行的操作,以确保即使您的实际数据表在给定时间段内没有行,整个查询也将返回一行(带有空扩展;如果需要,您可以将其合并为一个值)
编辑:
就您的查询而言,它看起来更像是这样:
SELECT
case
when YOUR_SUBQUERY.callingpartyno in (select extension from test.test_users) then YOUR_SUBQUERY.callingpartyno
when YOUR_SUBQUERY.finallycalledpartyno in (select extension from test.test_users) then YOUR_SUBQUERY.finallycalledpartyno
end as extension
, sum(YOUR_SUBQUERY.duration) as total_talk_time_seconds
, round(sum(YOUR_SUBQUERY.duration) / 60,2) as total_talk_time_minutes
, sum(if(YOUR_SUBQUERY.legtype1 = 1,1,0)) as total_outbound
, sum(case when(YOUR_SUBQUERY.legtype1 = 1 and duration > 60) then 1 else 0 end) as credit_for_outbound
, sum(if(YOUR_SUBQUERY.legtype1 = 2,1,0) and YOUR_SUBQUERY.answered = 1) as total_inbound
, sum(if(YOUR_SUBQUERY.legtype1 = 2,1,0) and YOUR_SUBQUERY.answered = 0) as total_missed
, sum(if(YOUR_SUBQUERY.legtype1 = 1, 1, 0)) + -- outbound calls
sum(if(YOUR_SUBQUERY.legtype1 = 2, 1, 0)) as total_calls
, round((sum(if(YOUR_SUBQUERY.legtype1 = 2,1,0) and YOUR_SUBQUERY.answered = 1))/(sum(if(YOUR_SUBQUERY.legtype1 = 1, 1, 0)) + -- outbound calls
sum(if(YOUR_SUBQUERY.legtype1 = 2, 1, 0))) * 100,2) as percent_answered
, TIME_OF_REPORT
, DATE_OF_REPORT
FROM
(SELECT NOW() AS TIME_OF_REPORT, CURDATE() AS DATE_OF_REPORT) T
LEFT OUTER JOIN
(
SELECT * FROM
test.session a
join test.callsummary b
on a.notablecallid = b.notablecallid
where
date(a.ts) >= curdate() and
(
callingpartyno in (select extension from test.test_users)
or
finallycalledpartyno in (select extension from test.test_users)
)
) YOUR_SUBQUERY
ON 1=1
group by
DATE_OF_REPORT, TIME_OF_REPORT, extension
你的sql本质上是小写的。我用大写添加了这些位。我无法对此进行测试,因为我对您的表结构和示例数据一无所知,因此可能需要您进行一些调试,但本质上是 (SELECT NOW() AS TIME_OF_REPORT, CURDATE() AS DATE_OF_REPORT) T
应始终返回一行。 YOUR_SUBQUERY 可以返回 0 到 N 行。在 YOUR_SUBQUERY 中有 0 行的情况下,总体报告仍应返回 1 行,带有时间(实际上是两行,尽管我不确定为什么使用 NOW() 来提供时间和日期,以及 CURDATE( ) - curdate 似乎是多余的,因为它只提供 NOW() 已经给出的信息,但我已经离开了)和一个空扩展。所有总数应该为 0,尽管我不确定是否例如MySQL 的 IF 函数,当像 IF(something_that_is_null = 2, 1, 0)
一样运行时返回 null (在我看来,它不应该;测试应该为 false,应该返回 0 并求和为 0)。如果您确实发现第一次运行时所有计数都是 null 而不是 0,您可以 COALESCE(logic_doing_sum_here, 0) 将它们从 null 转换为 0。如果 null 扩展对您来说很麻烦,请使用 COALESCE 将其转换为一些有意义的值默认值
关于mysql - 在选择时插入 0 以表示空返回(在任务计划程序上运行的 SQL 脚本),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47562479/
我已经为使用 JGroups 编写了简单的测试。有两个像这样的简单应用程序 import org.jgroups.*; import org.jgroups.conf.ConfiguratorFact
我有一个通过 ajax 检索的 json 编码数据集。我尝试检索的一些数据点将返回 null 或空。 但是,我不希望将那些 null 或空值显示给最终用户,或传递给其他函数。 我现在正在做的是检查
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: Why does one often see “null != variable” instead of “
嗨在我们公司,他们遵循与空值进行比较的严格规则。当我编码 if(variable!=null) 在代码审查中,我收到了对此的评论,将其更改为 if(null!=variable)。上面的代码对性能有影
我正在尝试使用 native Cordova QR 扫描仪插件编译项目,但是我不断收到此错误。据我了解,这是代码编写方式的问题,它向构造函数发送了错误的值,或者根本就没有找到构造函数。那么我该如何解决
我在装有 Java 1.8 的 Windows 10 上使用 Apache Nutch 1.14。我已按照 https://wiki.apache.org/nutch/NutchTutorial 中提
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: what is “=null” and “ IS NULL” Is there any difference bet
Three-EyedRaven 内网渗透初期,我们都希望可以豪无遗漏的尽最大可能打开目标内网攻击面,故,设计该工具的初衷是解决某些工具内网探测速率慢、运行卡死、服务爆破误报率高以及socks流
我想在Scala中像在Java中那样做: public void recv(String from) { recv(from, null); } public void recv(String
我正在尝试从一组图像补丁中创建一个密码本。我已将图像(Caltech 101)分成20 X 20图像块。我想为每个补丁创建一个SIFT描述符。但是对于某些图像补丁,它不返回任何描述符/关键点。我尝试使
我在验证器类中自动连接的两个服务有问题。这些服务工作正常,因为在我的 Controller 中是自动连接的。我有一个 applicationContext.xml 文件和 MyApp-servlet.
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 已关闭10 年前。 问题必须表现出对要解决的问题的最低程度的了解。告诉我们您尝试过做什么,为什么不起作用,以
大家好,我正在对数据库进行正常的选择,但是 mysql_num_rowsis 为空,我不知道为什么,我有 7 行选择。 如果您发现问题,请告诉我。 真的谢谢。 代码如下: function get_b
我想以以下格式创建一个字符串:id[]=%@&stringdata[]=%@&id[]=%@&stringdata[]=%@&id[]=%@&stringdata[]=%@&等,在for循环中,我得到
我正在尝试使用以下代码将URL转换为字符串: NSURL *urlOfOpenedFile = _service.myURLRequest.URL; NSString *fileThatWasOpen
我正在尝试将NSNumber传递到正在工作的UInt32中。然后,我试图将UInt32填充到NSData对象中。但是,这在这里变得有些时髦... 当我尝试将NSData对象中的内容写成它返回的字符串(
我正在进行身份验证并收到空 cookie。我想存储这个 cookie,但服务器没有返回给我 cookie。但响应代码是 200 ok。 httpConn.setRequestProperty(
我认为 Button bTutorial1 = (Button) findViewById(R.layout.tutorial1); bTutorial1.setOnClickListener
我的 Controller 中有这样的东西: model.attribute("hiringManagerMap",hiringManagerMap); 我正在访问此 hiringManagerMap
我想知道如何以正确的方式清空列表。在 div 中有一个列表然后清空 div 或列表更好吗? 我知道这是一个蹩脚的问题,但请帮助我理解这个 empty() 函数:) 案例)如果我运行这个脚本会发生什么:
我是一名优秀的程序员,十分优秀!