数据库统计相关

Posted yacbo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据库统计相关相关的知识,希望对你有一定的参考价值。

--路灯id和它的所有设备统计
SELECT
	*
FROM
	(
		SELECT
			cfg_object_device.object_id AS lampId,
			COUNT (*) AS lampSubDevcCount
		FROM
			cfg_object_device
		GROUP BY
			cfg_object_device.object_id
	) AS t1


-----------------------------------------------------------------------------------------------
--路灯id及其告警设备数
SELECT
	t3.object_type AS lampId,
	COUNT (*) AS lampSubDevcAlarmNum
FROM
	(
		SELECT
			t1.object_type
		FROM
			cfg_object_device AS t1,
			(
				SELECT
					alarm_objectid AS b1,
					COUNT (*) AS b2
				FROM
					his_alarm
				WHERE
					his_alarm.status <= 2
				GROUP BY
					alarm_objectid
			) AS t2
		WHERE
			t1.object_id = t2.b1
	) AS t3
GROUP BY
	t3.object_type

-----------------------------------------------------------------------------------------------
SELECT
	alarm_objectid AS a1,
	COUNT (*) AS a2
FROM
	his_alarm
WHERE
	his_alarm.status <= 2
GROUP BY
	alarm_objectid

  

 

 1 SELECT
 2     tt1.Lamp_pole_oid,
 3     tt1.lampSubDevcCount,
 4     CASE
 5     WHEN tt2.lampSubDevcAlarmNum IS NULL THEN
 6         0
 7     ELSE
 8         tt2.lampSubDevcAlarmNum
 9     END
10 FROM
11     (
12         SELECT
13             Streetlamp_Relation.Lamp_pole_oid,
14             COUNT (*) AS lampSubDevcCount
15         FROM
16             Streetlamp_Relation
17         GROUP BY
18             Streetlamp_Relation.Lamp_pole_oid
19     ) AS tt1
20 LEFT JOIN (
21     SELECT
22         t3.Lamp_pole_oid AS lampId,
23         COUNT (*) AS lampSubDevcAlarmNum
24     FROM
25         (
26             SELECT
27                 t1.Lamp_pole_oid
28             FROM
29                 Streetlamp_Relation AS t1,
30                 (
31                     SELECT
32                         alarm_objectid AS b1,
33                         COUNT (*) AS b2
34                     FROM
35                         his_alarm
36                     WHERE
37                         his_alarm.status <= 2
38                     GROUP BY
39                         alarm_objectid
40                 ) AS t2
41             WHERE
42                 t1.device_obj_id = t2.b1
43         ) AS t3
44     GROUP BY
45         t3.Lamp_pole_oid
46 ) AS tt2 ON tt1.Lamp_pole_oid = tt2.lampId

 

 1 SELECT
 2     table1.Lamp_id,
 3     table1.All_count,
 4     table1.Sub_count,
 5   (Sub_count*1.0)/All_count AS The_value,
 6     状态 = CASE WHEN Sub_count*1.0/All_count = 0 THEN
 7             \'正常\'
 8         WHEN Sub_count*1.0/All_count < 0.4 THEN
 9             \'普通\'
10         WHEN Sub_count*1.0/All_count < 0.7 THEN
11             \'紧急\'
12     WHEN Sub_count*1.0/All_count <= 1 THEN
13             \'严重\'
14         END
15 FROM
16     (
17         SELECT
18             tt1.Lamp_pole_oid AS Lamp_id,
19             tt1.lampSubDevcCount AS All_count,
20             CASE
21         WHEN tt2.lampSubDevcAlarmNum IS NULL THEN
22             0
23         ELSE
24             tt2.lampSubDevcAlarmNum
25         END AS Sub_count
26         FROM
27             (
28                 SELECT
29                     Streetlamp_Relation.Lamp_pole_oid,
30                     COUNT (*) AS lampSubDevcCount
31                 FROM
32                     Streetlamp_Relation
33                 GROUP BY
34                     Streetlamp_Relation.Lamp_pole_oid
35             ) AS tt1
36         LEFT JOIN (
37             SELECT
38                 t3.Lamp_pole_oid AS lampId,
39                 COUNT (*) AS lampSubDevcAlarmNum
40             FROM
41                 (
42                     SELECT
43                         t1.Lamp_pole_oid
44                     FROM
45                         Streetlamp_Relation AS t1,
46                         (
47                             SELECT
48                                 alarm_objectid AS b1,
49                                 COUNT (*) AS b2
50                             FROM
51                                 his_alarm
52                             WHERE
53                                 his_alarm.status <= 2
54                             GROUP BY
55                                 alarm_objectid
56                         ) AS t2
57                     WHERE
58                         t1.device_obj_id = t2.b1
59                 ) AS t3
60             GROUP BY
61                 t3.Lamp_pole_oid
62         ) AS tt2 ON tt1.Lamp_pole_oid = tt2.lampId
63     ) AS table1

 

1, 2, 3

以上是关于数据库统计相关的主要内容,如果未能解决你的问题,请参考以下文章

AJAX相关JS代码片段和部分浏览器模型

Express实战 - 应用案例- realworld-API - 路由设计 - mongoose - 数据验证 - 密码加密 - 登录接口 - 身份认证 - token - 增删改查API(代码片段

Jekyll 偏移代码片段高亮的初始行

从零开始配置vim(27)——代码片段

从零开始配置vim(27)——代码片段

从零开始配置vim(27)——代码片段