0

我有以下输出:

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)

即使在没有用的日子里,我也喜欢获得每个系统名的每日平均值,另一个转折是我想排除星期六。我尝试了一些东西,但我看不出想出正确的公式。以下是对该表的说明:

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')

谢谢!

4

1 回答 1

1

通常,最好的方法(恕我直言)是创建一个表格,其中包含您正在查看的任何年份范围(例如 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`)
);
于 2013-09-18T15:20:41.423 回答