SQL查询获取除星期六之外的平均每日使用量

时间:2013-09-18 15:05:34

标签: mysql sql average

我有以下输出:

root@localhost [hatsize]> SELECT COUNT(*) AS VMS,sysname,DAYNAME(Start),DATE(Start) from complete_ev WHERE type = 's' GROUP BY DAYOFYEAR(Start),sysname ORDER BY VMS DESC;
+-----+---------+----------------+-------------+
| VMS | sysname | DAYNAME(Start) | DATE(Start) |
+-----+---------+----------------+-------------+
|  85 | sv-esx5 | Monday         | 2013-09-16  |
|  63 | sv-esx6 | Wednesday      | 2013-09-18  |
|  60 | sv-esx5 | Monday         | 2013-09-09  |
|  55 | sv-esx6 | Monday         | 2013-09-16  |
|  52 | sv-esx6 | Monday         | 2013-09-09  |
|  51 | sv-esx5 | Wednesday      | 2013-09-18  |
|  50 | sv-esx7 | Wednesday      | 2013-09-11  |
|  44 | sv-esx4 | Monday         | 2013-09-09  |
|  43 | sv-esx4 | Tuesday        | 2013-09-10  |
|  34 | sv-esx4 | Tuesday        | 2013-09-17  |
|  34 | sv-esx7 | Monday         | 2013-09-09  |
|  34 | sv-esx4 | Monday         | 2013-09-16  |
|  34 | sv-esx7 | Tuesday        | 2013-09-17  |
|  34 | sv-esx6 | Wednesday      | 2013-09-11  |
|  34 | sv-esx7 | Monday         | 2013-09-16  |
|  30 | sv-esx5 | Wednesday      | 2013-09-11  |
|  30 | sv-esx5 | Thursday       | 2013-09-12  |
|  18 | sv-esx7 | Wednesday      | 2013-09-18  |
|  17 | sv-esx6 | Tuesday        | 2013-09-17  |
|  17 | sv-esx6 | Tuesday        | 2013-09-10  |
|  17 | sv-esx7 | Thursday       | 2013-09-12  |
|  13 | Pod130  | Wednesday      | 2013-09-11  |
|  10 | sv-esx4 | Friday         | 2013-03-22  |
|   9 | sv-esx4 | Sunday         | 2013-09-08  |
|   9 | sv-esx2 | Wednesday      | 2013-09-11  |
|   9 | sv-esx2 | Tuesday        | 2013-09-10  |
|   9 | Pod130  | Thursday       | 2013-09-12  |
|   9 | sv-esx4 | Wednesday      | 2013-09-11  |
|   9 | sv-esx5 | Friday         | 2013-09-13  |
|   9 | sv-esx4 | Friday         | 2013-09-13  |
|   9 | sv-esx2 | Monday         | 2013-09-09  |
|   9 | sv-esx5 | Tuesday        | 2013-09-10  |
|   2 | sv-esx2 | Friday         | 2012-07-27  |
+-----+---------+----------------+-------------+
33 rows in set (0.00 sec)

即使在没有用的日子里,我也希望得到每个sysname的每日平均值,而另一个转折是我想要排除星期六。我试了几件事,但我看不出合适的配方。这是表格的描述:

root@localhost [hatsize]> desc complete_ev;
+----------+--------------+------+-----+---------+-------+
| Field    | Type         | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| EventID  | int(16)      | YES  |     | NULL    |       |
| Start    | datetime     | YES  |     | NULL    |       |
| End      | datetime     | YES  |     | NULL    |       |
| Cname    | varchar(100) | YES  |     | NULL    |       |
| Template | varchar(100) | YES  |     | NULL    |       |
| pod      | varchar(100) | YES  |     | NULL    |       |
| sysname  | varchar(100) | YES  |     | NULL    |       |
| type     | char(10)     | YES  |     | NULL    |       |
+----------+--------------+------+-----+---------+-------+
8 rows in set (0.00 sec)

这实际上是一种观点:

show create table complete_ev\G
*************************** 1. row ***************************
       View: complete_ev
Create View: CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `complete_ev` AS select `e`.`EventID` AS `EventID`,`e`.`Start` AS `Start`,`e`.`End` AS `End`,`e`.`Name` AS `Cname`,`e`.`template` AS `Template`,`d`.`pod` AS `pod`,`d`.`name` AS `sysname`,`d`.`type` AS `type` from (`FutEv` `e` join `equip` `d` on((`e`.`EventID` = `d`.`EventID`))) where (`e`.`Status` = _latin1'i')

谢谢!

1 个答案:

答案 0 :(得分:1)

通常,执行此操作的最佳方式(imho)是创建一个表,其中包含您查看的任何年份的每个日期的记录(例如2000 - 2025)。然后从该表左边连接到主表,以获得所有日期,如果它们有结果。然后在WHERE子句中排除星期六。

这假设您创建了一个名为date_table的表,其中包含您需要的所有日期。它还假设您有一个名为systems的表,其中包含您将使用的系统列表。

SELECT sysname, AVG(VMS)

FROM

(

SELECT COUNT(*) AS VMS, sys.sysname, DAYNAME(dt.date), dt.date 

FROM   date_table as dt

       , (SELECT sysname FROM systems) as sys

       LEFT JOIN complete_ev as ce 
       ON dt.date = DATE(ce.Start) AND sys.sysname = ce.sysname
       AND ce.type = 's'

WHERE  DAYNAME(dt.date) <> 'Saturday'

GROUP BY dt.date, sys.sysname 

) as x

GROUP BY sysname;

创建date_table的代码。

CREATE TABLE `date_table` (
  `date` datetime DEFAULT NULL,
  `day_name` varchar(50) NOT NULL,
  PRIMARY KEY (`date`)
);