Return 值,即使为 null、0 或不存在
Return values even if null, 0 or not present
我想创建一些报告,以便我可以快速获取有关我们帮助台的数据,我正在使用 运行 MySQL 5.5
上的 GLPI
我有下面的代码,它 returns 一些有用的信息,但还不够,所以可以将它放入一个漂亮的堆叠图中,这正是他所要求的。为了获得足够的数据来实现这一点,我需要紧迫性为零,天数也为零,然后我可以将数据转换为 table in Excel 并构建图表。
当前查询:
SELECT DATE_FORMAT(date,'%d/%m/%Y') AS Date,Urgency,COUNT(*) as Tickets
FROM glpi.glpi_tickets
WHERE month(date)=month(NOW())
GROUP BY urgency ORDER BY date,urgency ASC;
这个returns:
# Date, Urgency, Tickets
'07/06/2016', '3', '10'
'10/06/2016', '2', '1'
'14/06/2016', '1', '1'
'14/06/2016', '5', '1'
我希望它显示如下:
# Date, Urgency, Tickets
'07/06/2016', '1', '0'
'07/06/2016', '2', '0'
'07/06/2016', '3', '10'
'07/06/2016', '4', '0'
'07/06/2016', '5', '0'
'08/06/2016', '1', '0'
'08/06/2016', '2', '0'
'08/06/2016', '3', '0'
'08/06/2016', '4', '0'
'08/06/2016', '5', '0'
...
'14/06/2016', '1', '1'
'14/06/2016', '2', '0'
'14/06/2016', '3', '0'
'14/06/2016', '4', '0'
'14/06/2016', '5', '1'
以此类推
我有点掌握 SQL(自学)的窍门,非常感谢所有帮助。
编辑:添加了架构,我认为这就是您所要求的(希望它有效)http://sqlfiddle.com/#!9/715c7
您的查询每 urgency
生成一行,因为您只按该列分组。为了查看每个日期和紧急程度的不同结果,您必须修改 GROUP BY。
SELECT
DATE_FORMAT(date,'%d/%m/%Y') AS Date,
Urgency,
COUNT(*) as Tickets
FROM
glpi.glpi_tickets
WHERE
month(date)=month(NOW())
GROUP BY
DATE(date),
urgency
ORDER BY
date, urgency ASC;
我想这就是您要找的。虽然我没有架构,但我尝试编写查询。我想这就是你要找的。 (请检查 SQL,我已经编辑了。它应该像你想要的那样生成一个报告,如果没有日期的数据,只有丢失的记录。)
select DATE_FORMAT(dummy.date,'%d/%m/%Y') Date, dummy.Urgency, ifnull(main.Tickets, 0) Tickets from
(select * from
(select distinct date(date) date from glpi_tickets
WHERE month(date)=month(NOW()) ) dates
cross join
(select distinct Urgency from glpi_tickets) urgency
order by Date, Urgency) dummy
left join
(SELECT date(date) date, Urgency, COUNT(*) as Tickets
FROM glpi_tickets
WHERE month(date)=month(NOW())
GROUP BY date(date), Urgency
ORDER BY date(date), Urgency) main
on dummy.date = main.date
and dummy.Urgency = main.Urgency
order by dummy.date asc, Urgency asc
我想下面的 SQL 可以满足您的需求,我从 here 那里得到了一些帮助。您需要针对大数据优化查询。
select DATE_FORMAT(dummy.date,'%d/%m/%Y') Date, dummy.Urgency, ifnull(main.Tickets, 0) Tickets from
(select * from
(SELECT date_field date
FROM
(SELECT
MAKEDATE(YEAR(NOW()),1) +
INTERVAL (MONTH(NOW())-1) MONTH +
INTERVAL daynum DAY date_field
FROM
(SELECT t*10+u daynum
FROM
(SELECT 0 t UNION SELECT 1 UNION SELECT 2 UNION SELECT 3) A,
(SELECT 0 u UNION SELECT 1 UNION SELECT 2 UNION SELECT 3
UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7
UNION SELECT 8 UNION SELECT 9) B
ORDER BY daynum
) AA
) AAA
WHERE MONTH(date_field) = MONTH(NOW())
and date_field >= (select min(date(date)) from glpi_tickets WHERE month(date)=month(NOW()))
and date_field <= (select max(date(date)) from glpi_tickets WHERE month(date)=month(NOW())) ) dates
cross join
(select distinct Urgency from glpi_tickets) urgency
order by date, Urgency) dummy
left join
(SELECT date(date) date, Urgency, COUNT(*) as Tickets
FROM glpi_tickets
WHERE month(date)=month(NOW())
GROUP BY date(date), Urgency
ORDER BY date(date), Urgency) main
on dummy.date = main.date
and dummy.Urgency = main.Urgency
order by dummy.date asc, Urgency asc
这就是我最终所做的,它似乎按照我想要的方式提供了数据。
首先我创建了一个新的 table 来存储数据:
CREATE TABLE glpi_plugin_ns_ticketstats
(
id INT(11),
daterun date,
timerun time,
totaltickets INT(11),
verylow INT(11),
low INT(11),
med INT(11),
high INT(11),
veryhigh INT(11));
然后我构建了一个存储过程来收集和填充数据:
USE `glpi`;
DROP procedure IF EXISTS `Daily_Ticket_Stats`;
DELIMITER $$
USE `glpi`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `Daily_Ticket_Stats`()
BEGIN
declare todayd date;
## Declaring the variable for the daterun column ##
declare todayt time;
## Declaring the variable for the timerun column ##
declare totalt int(11);
## Declaring the variable for totaltickets column ##
declare vlow int (11);
## Declaring the variable for verylow column ##
declare low int(11);
## Declaring the variable for low column ##
declare med int(11);
## Declaring the variable for med column ##
declare high int (11);
## Declaring the variable for high column ##
declare vhigh int(11);
## Declaring the variable for veryhigh column ##
set todayd = CURDATE();
## Set date to today ##
set todayt = CURTIME();
## Set time to now ##
set totalt = (SELECT COUNT(*) as ttickets
FROM glpi.glpi_tickets
WHERE day(date)=day(NOW()));
## This has set the total for the total tickets variable ##
set vlow = (SELECT COUNT(*) as vltickets
FROM glpi.glpi_tickets
WHERE day(date)=day(NOW())
AND urgency = '1');
## This has set the total for the very low urgency tickets variable ##
set low = (SELECT COUNT(*) as ltickets
FROM glpi.glpi_tickets
WHERE day(date)=day(NOW())
AND urgency = '2');
## This has set the total for the low urgency tickets variable ##
set med = (SELECT COUNT(*) as mtickets
FROM glpi.glpi_tickets
WHERE day(date)=day(NOW())
AND urgency = '3');
## This has set the total for the medium urgency tickets variable ##
set high = (SELECT COUNT(*) as htickets
FROM glpi.glpi_tickets
WHERE day(date)=day(NOW())
AND urgency = '4');
## This has set the total for the high urgency tickets variable ##
set vhigh = (SELECT COUNT(*) as vhtickets
FROM glpi.glpi_tickets
WHERE day(date)=day(NOW())
AND urgency = '5');
IF EXISTS(
SELECT *
FROM glpi.glpi_plugin_ns_ticketstats
WHERE daterun = CURDATE())
THEN
BEGIN
UPDATE glpi.glpi_plugin_ns_ticketstats
SET
timerun = CURTIME(),
totaltickets = totalt,
verylow = vlow,
low = low,
med = med,
high = high,
veryhigh = vhigh
WHERE
daterun = CURDATE();
END;
ELSE
INSERT INTO glpi.glpi_plugin_ns_ticketstats VALUES (NULL,todayd,todayt,totalt,vlow,low,med,high,vhigh);
END IF;
END
#$$
DELIMITER ;
然后我将此过程设置为每小时 运行,这样如果我的经理希望在白天参考这些统计数据,这些数据就是最新的(我直接将其输入到 mysql 服务器终端):
CREATE EVENT TicketStatusUpdate
ON SCHEDULE EVERY 1 HOUR
STARTS CURRENT_TIMESTAMP + INTERVAL 29 MINUTE
DO
CALL Daily_Ticket_Stats();
29 分钟是因为我希望它 运行 尽可能接近整点。
这现在以一种允许我在 Excel 中创建堆叠图的格式输出可爱的数据(6 月 1 日至 16 日的欺骗性条目,所以我有月初的条目):
# id, daterun, timerun, totaltickets, verylow, low, med, high, veryhigh
'1', '2016-06-01', '23:00:00', '0', '0', '0', '0', '0', '0'
'2', '2016-06-02', '23:00:00', '0', '0', '0', '0', '0', '0'
'3', '2016-06-03', '23:00:00', '0', '0', '0', '0', '0', '0'
'4', '2016-06-04', '23:00:00', '0', '0', '0', '0', '0', '0'
'5', '2016-06-05', '23:00:00', '0', '0', '0', '0', '0', '0'
'6', '2016-06-06', '23:00:00', '0', '0', '0', '0', '0', '0'
'7', '2016-06-07', '23:00:00', '0', '0', '0', '0', '0', '0'
'8', '2016-06-08', '23:00:00', '0', '0', '0', '0', '0', '0'
'9', '2016-06-09', '23:00:00', '0', '0', '0', '0', '0', '0'
'10', '2016-06-10', '23:00:00', '0', '0', '0', '0', '0', '0'
'11', '2016-06-11', '23:00:00', '0', '0', '0', '0', '0', '0'
'12', '2016-06-12', '23:00:00', '0', '0', '0', '0', '0', '0'
'13', '2016-06-13', '23:00:00', '0', '0', '0', '0', '0', '0'
'14', '2016-06-14', '23:00:00', '0', '0', '0', '0', '0', '0'
'15', '2016-06-15', '23:00:00', '0', '0', '0', '0', '0', '0'
'16', '2016-06-16', '23:00:00', '0', '0', '0', '0', '0', '0'
'17', '2016-06-17', '12:31:22', '4', '1', '0', '0', '0', '3'
这让我可以做一个 select
所以我只将当前月份导入 Excel:
select * from glpi.glpi_plugin_ns_ticketstats
where month(daterun)=month(NOW())
我会把它留在这里,所以如果有人想使用它,他们可以,感谢大家的时间和帮助:)
iFr4g
我想创建一些报告,以便我可以快速获取有关我们帮助台的数据,我正在使用 运行 MySQL 5.5
上的 GLPI我有下面的代码,它 returns 一些有用的信息,但还不够,所以可以将它放入一个漂亮的堆叠图中,这正是他所要求的。为了获得足够的数据来实现这一点,我需要紧迫性为零,天数也为零,然后我可以将数据转换为 table in Excel 并构建图表。
当前查询:
SELECT DATE_FORMAT(date,'%d/%m/%Y') AS Date,Urgency,COUNT(*) as Tickets
FROM glpi.glpi_tickets
WHERE month(date)=month(NOW())
GROUP BY urgency ORDER BY date,urgency ASC;
这个returns:
# Date, Urgency, Tickets
'07/06/2016', '3', '10'
'10/06/2016', '2', '1'
'14/06/2016', '1', '1'
'14/06/2016', '5', '1'
我希望它显示如下:
# Date, Urgency, Tickets
'07/06/2016', '1', '0'
'07/06/2016', '2', '0'
'07/06/2016', '3', '10'
'07/06/2016', '4', '0'
'07/06/2016', '5', '0'
'08/06/2016', '1', '0'
'08/06/2016', '2', '0'
'08/06/2016', '3', '0'
'08/06/2016', '4', '0'
'08/06/2016', '5', '0'
...
'14/06/2016', '1', '1'
'14/06/2016', '2', '0'
'14/06/2016', '3', '0'
'14/06/2016', '4', '0'
'14/06/2016', '5', '1'
以此类推
我有点掌握 SQL(自学)的窍门,非常感谢所有帮助。
编辑:添加了架构,我认为这就是您所要求的(希望它有效)http://sqlfiddle.com/#!9/715c7
您的查询每 urgency
生成一行,因为您只按该列分组。为了查看每个日期和紧急程度的不同结果,您必须修改 GROUP BY。
SELECT
DATE_FORMAT(date,'%d/%m/%Y') AS Date,
Urgency,
COUNT(*) as Tickets
FROM
glpi.glpi_tickets
WHERE
month(date)=month(NOW())
GROUP BY
DATE(date),
urgency
ORDER BY
date, urgency ASC;
我想这就是您要找的。虽然我没有架构,但我尝试编写查询。我想这就是你要找的。 (请检查 SQL,我已经编辑了。它应该像你想要的那样生成一个报告,如果没有日期的数据,只有丢失的记录。)
select DATE_FORMAT(dummy.date,'%d/%m/%Y') Date, dummy.Urgency, ifnull(main.Tickets, 0) Tickets from
(select * from
(select distinct date(date) date from glpi_tickets
WHERE month(date)=month(NOW()) ) dates
cross join
(select distinct Urgency from glpi_tickets) urgency
order by Date, Urgency) dummy
left join
(SELECT date(date) date, Urgency, COUNT(*) as Tickets
FROM glpi_tickets
WHERE month(date)=month(NOW())
GROUP BY date(date), Urgency
ORDER BY date(date), Urgency) main
on dummy.date = main.date
and dummy.Urgency = main.Urgency
order by dummy.date asc, Urgency asc
我想下面的 SQL 可以满足您的需求,我从 here 那里得到了一些帮助。您需要针对大数据优化查询。
select DATE_FORMAT(dummy.date,'%d/%m/%Y') Date, dummy.Urgency, ifnull(main.Tickets, 0) Tickets from
(select * from
(SELECT date_field date
FROM
(SELECT
MAKEDATE(YEAR(NOW()),1) +
INTERVAL (MONTH(NOW())-1) MONTH +
INTERVAL daynum DAY date_field
FROM
(SELECT t*10+u daynum
FROM
(SELECT 0 t UNION SELECT 1 UNION SELECT 2 UNION SELECT 3) A,
(SELECT 0 u UNION SELECT 1 UNION SELECT 2 UNION SELECT 3
UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7
UNION SELECT 8 UNION SELECT 9) B
ORDER BY daynum
) AA
) AAA
WHERE MONTH(date_field) = MONTH(NOW())
and date_field >= (select min(date(date)) from glpi_tickets WHERE month(date)=month(NOW()))
and date_field <= (select max(date(date)) from glpi_tickets WHERE month(date)=month(NOW())) ) dates
cross join
(select distinct Urgency from glpi_tickets) urgency
order by date, Urgency) dummy
left join
(SELECT date(date) date, Urgency, COUNT(*) as Tickets
FROM glpi_tickets
WHERE month(date)=month(NOW())
GROUP BY date(date), Urgency
ORDER BY date(date), Urgency) main
on dummy.date = main.date
and dummy.Urgency = main.Urgency
order by dummy.date asc, Urgency asc
这就是我最终所做的,它似乎按照我想要的方式提供了数据。
首先我创建了一个新的 table 来存储数据:
CREATE TABLE glpi_plugin_ns_ticketstats
(
id INT(11),
daterun date,
timerun time,
totaltickets INT(11),
verylow INT(11),
low INT(11),
med INT(11),
high INT(11),
veryhigh INT(11));
然后我构建了一个存储过程来收集和填充数据:
USE `glpi`;
DROP procedure IF EXISTS `Daily_Ticket_Stats`;
DELIMITER $$
USE `glpi`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `Daily_Ticket_Stats`()
BEGIN
declare todayd date;
## Declaring the variable for the daterun column ##
declare todayt time;
## Declaring the variable for the timerun column ##
declare totalt int(11);
## Declaring the variable for totaltickets column ##
declare vlow int (11);
## Declaring the variable for verylow column ##
declare low int(11);
## Declaring the variable for low column ##
declare med int(11);
## Declaring the variable for med column ##
declare high int (11);
## Declaring the variable for high column ##
declare vhigh int(11);
## Declaring the variable for veryhigh column ##
set todayd = CURDATE();
## Set date to today ##
set todayt = CURTIME();
## Set time to now ##
set totalt = (SELECT COUNT(*) as ttickets
FROM glpi.glpi_tickets
WHERE day(date)=day(NOW()));
## This has set the total for the total tickets variable ##
set vlow = (SELECT COUNT(*) as vltickets
FROM glpi.glpi_tickets
WHERE day(date)=day(NOW())
AND urgency = '1');
## This has set the total for the very low urgency tickets variable ##
set low = (SELECT COUNT(*) as ltickets
FROM glpi.glpi_tickets
WHERE day(date)=day(NOW())
AND urgency = '2');
## This has set the total for the low urgency tickets variable ##
set med = (SELECT COUNT(*) as mtickets
FROM glpi.glpi_tickets
WHERE day(date)=day(NOW())
AND urgency = '3');
## This has set the total for the medium urgency tickets variable ##
set high = (SELECT COUNT(*) as htickets
FROM glpi.glpi_tickets
WHERE day(date)=day(NOW())
AND urgency = '4');
## This has set the total for the high urgency tickets variable ##
set vhigh = (SELECT COUNT(*) as vhtickets
FROM glpi.glpi_tickets
WHERE day(date)=day(NOW())
AND urgency = '5');
IF EXISTS(
SELECT *
FROM glpi.glpi_plugin_ns_ticketstats
WHERE daterun = CURDATE())
THEN
BEGIN
UPDATE glpi.glpi_plugin_ns_ticketstats
SET
timerun = CURTIME(),
totaltickets = totalt,
verylow = vlow,
low = low,
med = med,
high = high,
veryhigh = vhigh
WHERE
daterun = CURDATE();
END;
ELSE
INSERT INTO glpi.glpi_plugin_ns_ticketstats VALUES (NULL,todayd,todayt,totalt,vlow,low,med,high,vhigh);
END IF;
END
#$$
DELIMITER ;
然后我将此过程设置为每小时 运行,这样如果我的经理希望在白天参考这些统计数据,这些数据就是最新的(我直接将其输入到 mysql 服务器终端):
CREATE EVENT TicketStatusUpdate
ON SCHEDULE EVERY 1 HOUR
STARTS CURRENT_TIMESTAMP + INTERVAL 29 MINUTE
DO
CALL Daily_Ticket_Stats();
29 分钟是因为我希望它 运行 尽可能接近整点。
这现在以一种允许我在 Excel 中创建堆叠图的格式输出可爱的数据(6 月 1 日至 16 日的欺骗性条目,所以我有月初的条目):
# id, daterun, timerun, totaltickets, verylow, low, med, high, veryhigh
'1', '2016-06-01', '23:00:00', '0', '0', '0', '0', '0', '0'
'2', '2016-06-02', '23:00:00', '0', '0', '0', '0', '0', '0'
'3', '2016-06-03', '23:00:00', '0', '0', '0', '0', '0', '0'
'4', '2016-06-04', '23:00:00', '0', '0', '0', '0', '0', '0'
'5', '2016-06-05', '23:00:00', '0', '0', '0', '0', '0', '0'
'6', '2016-06-06', '23:00:00', '0', '0', '0', '0', '0', '0'
'7', '2016-06-07', '23:00:00', '0', '0', '0', '0', '0', '0'
'8', '2016-06-08', '23:00:00', '0', '0', '0', '0', '0', '0'
'9', '2016-06-09', '23:00:00', '0', '0', '0', '0', '0', '0'
'10', '2016-06-10', '23:00:00', '0', '0', '0', '0', '0', '0'
'11', '2016-06-11', '23:00:00', '0', '0', '0', '0', '0', '0'
'12', '2016-06-12', '23:00:00', '0', '0', '0', '0', '0', '0'
'13', '2016-06-13', '23:00:00', '0', '0', '0', '0', '0', '0'
'14', '2016-06-14', '23:00:00', '0', '0', '0', '0', '0', '0'
'15', '2016-06-15', '23:00:00', '0', '0', '0', '0', '0', '0'
'16', '2016-06-16', '23:00:00', '0', '0', '0', '0', '0', '0'
'17', '2016-06-17', '12:31:22', '4', '1', '0', '0', '0', '3'
这让我可以做一个 select
所以我只将当前月份导入 Excel:
select * from glpi.glpi_plugin_ns_ticketstats
where month(daterun)=month(NOW())
我会把它留在这里,所以如果有人想使用它,他们可以,感谢大家的时间和帮助:)
iFr4g