我正在尝试将两个单独的查询合并为一个。我正在将它们用于jaspersoft报告工作室。第一个查询的输出是有关指定日期范围之间的表中条目的详细信息。 第二个查询的输出包含第一个查询的一些输出量以及它自己的参数。他们分开工作很好。
所以我甚至不确定是否可以将这些组合到一个单一查询中。
查询1:
SELECT
`alarminfo`.`condition_id`,
`alarminfo`.`landscape_h`,
`landscape`.`domain_name`,
`model`.`model_name`,
`alarminfo`.`set_time`,
`alarminfo`.`clear_time` ,
`alarminfo`.`alarm_key`,
`alarmtitle`.`title`,
`alarminfo`.`ack_time`,
`alarminfo`.`set_troubleticket_id`,
`alarminfo`.`first_assigned_time`,
`alarmcondition`.`condition_name`,
TIMESTAMPDIFF(SECOND,alarminfo.set_time ,alarminfo.clear_time) seconds_to_clear,
TIMESTAMPDIFF(SECOND,alarminfo.set_time ,'2017-09-01 00:00:00.0') alerttime
FROM alarminfo
INNER JOIN landscape ON (alarminfo.landscape_h=landscape.landscape_h)
INNER JOIN alarmtitle ON (alarminfo.alarm_title_id=alarmtitle.alarm_title_id)
INNER JOIN model ON (alarminfo.model_key=model.model_key)
INNER JOIN alarmcondition on (alarminfo.condition_id=alarmcondition.condition_id)
where
((alarminfo.set_time>='2017-09-01 00:00:00.0' AND alarminfo.set_time<'2017-09-30 23:59:59.0') AND
(alarminfo.clear_time>='2017-09-01 00:00:00.0' AND alarminfo.clear_time<'2017-09-30 23:59:59.0') AND
(alarmcondition.condition_name IN ('Critical', 'Major', 'Minor')) AND
(TIMESTAMPDIFF(SECOND,alarminfo.set_time ,alarminfo.ack_time)>60*15)
)
order by `alarminfo`.`set_troubleticket_id`DESC , `alarminfo`.`ack_time` , `alarminfo`.`set_time` , `alarminfo`.`landscape_h`, `alarminfo`.`condition_id`
查询2:
select
count(*) 'Total alarms',
sum(case when ack_time is not null then 1 else 0 end) as 'total acknowledged',
sum(case when (TIMESTAMPDIFF(SECOND,alarminfo.set_time ,alarminfo.ack_time)<60*15) then 1 else 0 end) as 'Total acknowledged within 15min',
sum(case when (TIMESTAMPDIFF(SECOND,alarminfo.set_time ,alarminfo.ack_time)>60*15) then 1 else 0 end) as 'Total acknowledged after 15min'
FROM alarminfo
where
alarminfo.set_time>='2017-09-01 00:00:00.0' AND alarminfo.set_time<'2017-09-30 23:59:59.0'
这就是我试图将它们合并在一起的方式:
SELECT
`alarminfo`.`condition_id`,
`alarminfo`.`landscape_h`,
`landscape`.`domain_name`,
`model`.`model_name`,
`alarminfo`.`set_time`,
`alarminfo`.`clear_time` ,
`alarminfo`.`alarm_key`,
`alarmtitle`.`title`,
`alarminfo`.`ack_time`,
`alarminfo`.`set_troubleticket_id`,
`alarminfo`.`first_assigned_time`,
`alarmcondition`.`condition_name`,
TIMESTAMPDIFF(SECOND,alarminfo.set_time ,alarminfo.clear_time) seconds_to_clear,
TIMESTAMPDIFF(SECOND,alarminfo.set_time ,'2017-09-01 00:00:00.0') alerttime
FROM alarminfo
INNER JOIN landscape ON (alarminfo.landscape_h=landscape.landscape_h)
INNER JOIN alarmtitle ON (alarminfo.alarm_title_id=alarmtitle.alarm_title_id)
INNER JOIN model ON (alarminfo.model_key=model.model_key)
INNER JOIN alarmcondition on (alarminfo.condition_id=alarmcondition.condition_id)
where
((alarminfo.set_time>='2017-09-01 00:00:00.0' AND alarminfo.set_time<'2017-09-30 23:59:59.0') AND
(alarminfo.clear_time>='2017-09-01 00:00:00.0' AND alarminfo.clear_time<'2017-09-30 23:59:59.0') AND
(alarmcondition.condition_name IN ('Critical', 'Major', 'Minor')) AND
(TIMESTAMPDIFF(SECOND,alarminfo.set_time ,alarminfo.ack_time)>60*15)
)
count(*) 'Total alarms',
sum(case when ack_time is not null then 1 else 0 end) as 'total acknowledged',
sum(case when (TIMESTAMPDIFF(SECOND,alarminfo.set_time ,alarminfo.ack_time)<60*15) then 1 else 0 end) as 'Total acknowledged within 15min',
sum(case when (TIMESTAMPDIFF(SECOND,alarminfo.set_time ,alarminfo.ack_time)>60*15) then 1 else 0 end) as 'Total acknowledged after 15min'
FROM alarminfo
where
alarminfo.set_time>='2017-09-01 00:00:00.0' AND alarminfo.set_time<'2017-09-30 23:59:59.0'
)
order by `alarminfo`.`set_troubleticket_id`DESC , `alarminfo`.`ack_time` , `alarminfo`.`set_time` , `alarminfo`.`landscape_h`, `alarminfo`.`condition_id`
遗憾的是,这不起作用。有人知道如何合并它们吗?
答案 0 :(得分:0)
使用第一个查询的输出作为第二个查询的输入。因为,第一个查询中已经包含第二个查询的where子句所以我们可以省略它。
选择
count(*)&#39; Total alarms&#39;,
总和(ack_time不为空的情况,然后是1,否则为0结束)为&#39;总确认&#39;,
sum((TIMESTAMPDIFF(SECOND,R.set_time,R.ack_time)&lt; 60 * 15)然后1或0结束时的情况)&#39;在15分钟内确认总数&#39;,
sum((TIMESTAMPDIFF(SECOND,R.set_time,R.ack_time)> 60 * 15)然后1或0结束时的情况)&#39; 15分钟后确认总数&#39;
FROM(选择
alarminfo
。condition_id
,
alarminfo
。landscape_h
,
landscape
。domain_name
,
model
。model_name
,
alarminfo
。set_time
,
alarminfo
。clear_time
,
alarminfo
。alarm_key
,
alarmtitle
。title
,
alarminfo
。ack_time
,
alarminfo
。set_troubleticket_id
,
alarminfo
。first_assigned_time
,
alarmcondition
。condition_name
,
TIMESTAMPDIFF(第二,alarminfo.set_time,alarminfo.clear_time)seconds_to_clear,
TIMESTAMPDIFF(第二,alarminfo.set_time,&#39; 2017-09-01 00:00:00.0&#39;)alerttime
来自alarminfo
INNER JOIN landscape ON(alarminfo.landscape_h = landscape.landscape_h)
INNER JOIN警报标志ON(alarminfo.alarm_title_id = alarmtitle.alarm_title_id)
INNER JOIN模型ON(alarminfo.model_key = model.model_key)
INNER JOIN报警条件打开(alarminfo.condition_id = alarmcondition.condition_id)
其中
((alarminfo.set_time&gt; =&#39; 2017-09-01 00:00:00.0&#39; AND alarminfo.set_time&lt;&#39; 2017-09-30 23:59:59.0&#39;)和
(alarminfo.clear_time&gt; =&#39; 2017-09-01 00:00:00.0&#39; AND alarminfo.clear_time&lt;&#39; 2017-09-30 23:59:59.0&#39;)AND <登记/> (alarmcondition.condition_name IN(&#39; Critical&#39;,&#39; Major&#39;,&#39; Minor&#39;))和
(TIMESTAMPDIFF(SECOND,alarminfo.set_time,alarminfo.ack_time)&gt; 60 * 15)
)
按alarminfo
排序。set_troubleticket_id
DESC,alarminfo
。ack_time
,alarminfo
。set_time
,alarminfo
。landscape_h
,alarminfo
。condition_id
)作为R