我有一个名为stops的表 表格放在这里有数据
CREATE TABLE `stops` (
`LineID` varchar(10) NOT NULL,
`StationID` int(11) NOT NULL,
`Rank` int(11) NOT NULL,
`DistanceToNext` float DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `stops` (`LineID`, `StationID`, `Rank`, `DistanceToNext`) VALUES
('1', 132, 1, 1.469),
('1', 90, 2, 1.114),
('1', 213, 3, 1.462),
('1', 145, 4, 1.084),
('1', 239, 5, 0.875),
('1', 8, 6, 0.727),
('1', 50, 7, 0.588),
('1', 112, 8, 0.922),
('1', 99, 9, 0.663),
('1', 47, 10, 1.048),
('1', 66, 11, 0.802),
('1', 296, 12, 0.639),
('1', 193, 13, 0.631),
('1', 151, 14, 0.716),
('1', 57, 15, 0.524),
('1', 119, 16, 1.034),
('1', 276, 17, 0.982),
('1', 18, 18, 1.055),
('1', 109, 19, 1.369),
('1', 253, 20, 1.097),
('1', 182, 21, 1.449),
('1', 234, 22, 1.094),
('1', 273, 23, 1.18),
('1', 21, 24, 1.146),
('1', 54, 25, 0),
('10', 35, 1, 1.192),
('10', 34, 2, 2.297),
('10', 218, 3, 0.519),
('10', 174, 4, 0.406),
('10', 173, 5, 0.418),
('10', 48, 6, 0.379),
('10', 88, 7, 0.501),
('10', 176, 8, 0.578),
('10', 125, 9, 0.876),
('10', 51, 10, 0.959),
('10', 13, 11, 0.414),
('10', 135, 12, 1.13),
('10', 281, 13, 0.99),
('10', 84, 14, 0.624),
('10', 297, 15, 0.509),
('10', 283, 16, 1.075),
('10', 152, 17, 0.356),
('10', 187, 18, 0.753),
('10', 63, 19, 0.386),
('10', 171, 20, 0.512),
('10', 44, 21, 0.363),
('10', 128, 22, 1.112),
('10', 107, 23, 0),
('11', 57, 1, 0.524),
('11', 119, 2, 0.561),
('11', 247, 3, 0.538),
('11', 9, 4, 0.847),
('11', 252, 5, 0.88),
('11', 114, 6, 0.669),
('11', 20, 7, 0.922),
('11', 243, 8, 0.499),
('11', 126, 9, 0.51),
('11', 206, 10, 0.591),
('11', 290, 11, 0.887),
('11', 231, 12, 1.137),
('11', 158, 13, 0),
('12', 225, 1, 0.872),
('12', 170, 2, 1.155),
('12', 168, 3, 0.622),
('12', 127, 4, 0.688),
('12', 134, 5, 0.593),
('12', 1, 6, 0.246),
('12', 204, 7, 0.443),
('12', 269, 8, 0.23),
('12', 184, 9, 0.6),
('12', 294, 10, 0.771),
('12', 272, 11, 0.623),
('12', 153, 12, 0.478),
('12', 66, 13, 0.658),
('12', 11, 14, 0.346),
('12', 286, 15, 0.447),
('12', 261, 16, 0.507),
('12', 283, 17, 0.428),
('12', 251, 18, 0.359),
('12', 185, 19, 0.722),
('12', 180, 20, 0.598),
('12', 95, 21, 0.57),
('12', 196, 22, 0.531),
('12', 306, 23, 0.82),
('12', 299, 24, 0.51),
('12', 67, 25, 1.135),
('12', 233, 26, 1.127),
('12', 69, 27, 0.699),
('12', 159, 28, 0),
('13', 10, 1, 0.828),
('13', 142, 2, 1.255),
('13', 103, 3, 1.831),
('13', 154, 4, 1.376),
('13', 223, 5, 0.828),
('13', 38, 6, 7.735),
('13', 266, 7, 1.054),
('13', 17, 8, 0.806),
('13', 265, 9, 1.943),
('13', 45, 10, 1.298),
('13', 157, 11, 0.722),
('13', 111, 12, 1.027),
('13', 230, 13, 0.561),
('13', 116, 14, 0.579),
('13', 133, 15, 0.442),
('13', 205, 16, 0.459),
('13', 147, 17, 0.444),
('13', 272, 18, 1.239),
('13', 177, 19, 0.713),
('13', 47, 20, 0.638),
('13', 121, 21, 0.481),
('13', 298, 22, 0.65),
('13', 268, 23, 0.639),
('13', 84, 24, 0.778),
('13', 180, 25, 0.506),
('13', 104, 26, 0.754),
('13', 200, 27, 0.46),
('13', 209, 28, 1.071),
('13', 232, 29, 1.054),
('13', 164, 30, 0.94),
('13', 165, 31, 0.676),
('13', 58, 32, 0),
('2', 217, 1, 0.983),
('2', 301, 2, 1.28),
('2', 50, 3, 0.478),
('2', 292, 4, 0.633),
('2', 71, 5, 0.616),
('2', 178, 6, 0.703),
('2', 305, 7, 0.656),
('2', 258, 8, 0.713),
('2', 205, 9, 0.485),
('2', 26, 10, 0.675),
('2', 204, 11, 0.71),
('2', 7, 12, 0.643),
('2', 16, 13, 0.975),
('2', 130, 14, 0.955),
('2', 287, 15, 0.401),
('2', 124, 16, 0.511),
('2', 64, 17, 0.989),
('2', 20, 18, 0.482),
('2', 72, 19, 0.516),
('2', 172, 20, 0.524),
('2', 199, 21, 0.557),
('2', 201, 22, 0.669),
('2', 3, 23, 0.666),
('2', 14, 24, 0.362),
('2', 182, 25, 0),
('3', 212, 1, 0.749),
('3', 5, 2, 0.508),
('3', 149, 3, 0.628),
('3', 220, 4, 0.542),
('3', 198, 5, 0.797),
('3', 308, 6, 0.573),
('3', 166, 7, 0.65),
('3', 305, 8, 0.879),
('3', 92, 9, 0.511),
('3', 272, 10, 0.358),
('3', 117, 11, 0.47),
('3', 189, 12, 0.462),
('3', 246, 13, 0.614),
('3', 36, 14, 0.618),
('3', 282, 15, 0.615),
('3', 250, 16, 0.496),
('3', 9, 17, 0.536),
('3', 291, 18, 0.313),
('3', 252, 19, 1.378),
('3', 194, 20, 0.488),
('3', 262, 21, 0.755),
('3', 199, 22, 1.329),
('3', 106, 23, 1.062),
('3', 219, 24, 0.862),
('3', 105, 25, 0),
('3Bis', 106, 1, 0.52),
('3Bis', 197, 2, 0.545),
('3Bis', 267, 3, 0.594),
('3Bis', 231, 4, 0),
('4', 224, 1, 0.391),
('4', 285, 2, 0.517),
('4', 168, 3, 0.411),
('4', 56, 4, 0.395),
('4', 16, 5, 0.685),
('4', 110, 6, 0.562),
('4', 108, 7, 0.472),
('4', 53, 8, 0.406),
('4', 288, 9, 0.394),
('4', 250, 10, 0.488),
('4', 91, 11, 0.347),
('4', 144, 12, 0.433),
('4', 57, 13, 0.382),
('4', 62, 14, 0.414),
('4', 275, 15, 0.558),
('4', 187, 16, 0.559),
('4', 270, 17, 0.445),
('4', 280, 18, 0.642),
('4', 278, 19, 0.619),
('4', 180, 20, 0.717),
('4', 300, 21, 0.381),
('4', 249, 22, 0.614),
('4', 81, 23, 0.373),
('4', 181, 24, 0.441),
('4', 2, 25, 0.679),
('4', 238, 26, 0),
('5', 27, 1, 3.051),
('5', 28, 2, 1.303),
('5', 89, 3, 1.139),
('5', 118, 4, 1.346),
('5', 228, 5, 0.623),
('5', 192, 6, 0.747),
('5', 138, 7, 1.078),
('5', 124, 8, 0.401),
('5', 287, 9, 1.459),
('5', 110, 10, 0.562),
('5', 108, 11, 0.636),
('5', 122, 12, 0.521),
('5', 252, 13, 0.647),
('5', 186, 14, 0.554),
('5', 254, 15, 0.503),
('5', 37, 16, 0.453),
('5', 18, 17, 0.818),
('5', 245, 18, 0.425),
('5', 107, 19, 0.513),
('5', 274, 20, 0.551),
('5', 43, 21, 0.556),
('5', 207, 22, 0),
('6', 50, 1, 0.474),
('6', 129, 2, 0.537),
('6', 29, 3, 0.592),
('6', 295, 4, 0.607),
('6', 195, 5, 0.49),
('6', 25, 6, 0.592),
('6', 83, 7, 0.604),
('6', 135, 8, 0.469),
('6', 42, 9, 0.967),
('6', 284, 10, 0.367),
('6', 196, 11, 1.11),
('6', 180, 12, 0.495),
('6', 87, 13, 0.514),
('6', 249, 14, 0.614),
('6', 81, 15, 0.483),
('6', 271, 16, 0.884),
('6', 113, 17, 0.642),
('6', 70, 18, 0.727),
('6', 207, 19, 0.718),
('6', 183, 20, 0.6),
('6', 61, 21, 0.808),
('6', 244, 22, 0.727),
('6', 22, 23, 1.055),
('6', 82, 24, 0.768),
('6', 80, 25, 0.659),
('6', 19, 26, 0.374),
('6', 202, 27, 0.693),
('6', 182, 28, 0),
('7', 131, 1, 0.982),
('7', 98, 2, 1.812),
('7', 12, 3, 0.962),
('7', 226, 4, 0.494),
('7', 68, 5, 0.782),
('7', 77, 6, 0.384),
('7', 256, 7, 0.926),
('7', 287, 8, 0.402),
('7', 148, 9, 0.471),
('7', 55, 10, 0.548),
('7', 108, 11, 0.935),
('7', 211, 12, 0.757),
('7', 41, 13, 0.367),
('7', 141, 14, 0.736),
('7', 59, 15, 0.308),
('7', 189, 16, 0.466),
('7', 242, 17, 0.514),
('7', 193, 18, 0.842),
('7', 216, 19, 0.575),
('7', 57, 20, 1.287),
('7', 215, 21, 0.552),
('7', 289, 22, 0.985),
('7', 128, 23, 0.405),
('7', 208, 24, 0.307),
('7', 46, 25, 0.437),
('7', 143, 26, 0.735),
('7', 207, 27, 0.57),
('7', 293, 28, 0.444),
('7', 161, 29, 1.412),
('7', 140, 30, 0.737),
('7', 302, 31, 1.018),
('7', 304, 32, 1),
('7', 303, 33, 3.635),
('7', 235, 34, 0.517),
('7', 222, 35, 0.543),
('7', 236, 36, 1.102),
('7', 203, 37, 0.881),
('7', 160, 38, 0),
('7bis', 148, 1, 0.546),
('7bis', 124, 2, 0.47),
('7bis', 30, 3, 0.854),
('7bis', 39, 4, 0.863),
('7bis', 32, 5, 0.533),
('7bis', 79, 6, 0.584),
('7bis', 206, 7, 0.713),
('7bis', 240, 8, 0),
('8', 15, 1, 0.464),
('8', 150, 2, 0.721),
('8', 33, 3, 0.522),
('8', 96, 4, 0.253),
('8', 65, 5, 0.703),
('8', 135, 6, 1.033),
('8', 85, 7, 0.552),
('8', 137, 8, 0.713),
('8', 121, 9, 1.113),
('8', 66, 10, 0.478),
('8', 153, 11, 0.742),
('8', 189, 12, 0.823),
('8', 255, 13, 0.462),
('8', 115, 14, 0.617),
('8', 31, 15, 0.621),
('8', 288, 16, 1.081),
('8', 252, 17, 0.558),
('8', 97, 18, 0.233),
('8', 279, 19, 0.425),
('8', 60, 20, 0.543),
('8', 18, 21, 0.774),
('8', 139, 22, 0.932),
('8', 94, 23, 0.391),
('8', 253, 24, 0.527),
('8', 179, 25, 0.864),
('8', 80, 26, 0.845),
('8', 175, 27, 0.367),
('8', 237, 28, 0.773),
('8', 221, 29, 1.094),
('8', 146, 30, 0.938),
('8', 49, 31, 1.014),
('8', 86, 32, 1.81),
('8', 163, 33, 1.199),
('8', 162, 34, 0.869),
('8', 74, 35, 0.833),
('8', 76, 36, 1.368),
('8', 75, 37, 0),
('9', 214, 1, 0.912),
('9', 24, 2, 0.699),
('9', 169, 3, 1.482),
('9', 229, 4, 0.601),
('9', 93, 5, 0.374),
('9', 174, 6, 0.406),
('9', 173, 7, 0.65),
('9', 123, 8, 0.41),
('9', 248, 9, 0.54),
('9', 136, 10, 0.788),
('9', 259, 11, 1.052),
('9', 295, 12, 0.772),
('9', 120, 13, 0.788),
('9', 4, 14, 0.975),
('9', 99, 15, 0.361),
('9', 277, 16, 0.622),
('9', 177, 17, 0.592),
('9', 264, 18, 0.91),
('9', 117, 19, 0.557),
('9', 59, 20, 0.629),
('9', 255, 21, 0.462),
('9', 115, 22, 0.617),
('9', 31, 23, 0.621),
('9', 288, 24, 1.081),
('9', 252, 25, 0.647),
('9', 186, 26, 0.706),
('9', 263, 27, 0.743),
('9', 307, 28, 0.691),
('9', 52, 29, 0.52),
('9', 260, 30, 0.856),
('9', 182, 31, 0.675),
('9', 40, 32, 0.588),
('9', 167, 33, 0.476),
('9', 227, 34, 1.478),
('9', 257, 35, 1.419),
('9', 78, 36, 0.79),
('9', 155, 37, 0);
这个表有一个关系表subwayline,但我不知道这个查询表需要与否,我也在这里添加该表
CREATE TABLE `subwaylines` (
`LineID` varchar(10) NOT NULL,
`Start1` time NOT NULL,
`Start2` time NOT NULL,
`EndW1` time NOT NULL,
`EndW2` time NOT NULL,
`EndWE1` time NOT NULL,
`EndWE2` time NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `subwaylines` (`LineID`, `Start1`, `Start2`, `EndW1`, `EndW2`, `EndWE1`, `EndWE2`) VALUES
('1', '05:30:00', '05:30:00', '00:40:00', '00:40:00', '01:40:00', '01:40:00'),
('10', '05:30:00', '05:35:00', '00:21:00', '00:47:00', '01:21:00', '01:47:00'),
('11', '05:30:00', '05:30:00', '00:56:00', '00:59:00', '01:56:00', '01:59:00'),
('12', '05:30:00', '05:30:00', '00:39:00', '00:38:00', '01:39:00', '01:38:00'),
('13', '05:30:00', '05:30:00', '00:37:00', '00:37:00', '01:37:00', '01:37:00'),
('14', '05:30:00', '05:30:00', '01:00:00', '01:01:00', '02:00:00', '02:01:00'),
('2', '05:30:00', '05:30:00', '00:40:00', '00:42:00', '01:40:00', '01:42:00'),
('3', '05:30:00', '05:30:00', '00:45:00', '00:44:00', '01:45:00', '01:44:00'),
('3bis', '05:32:00', '05:27:00', '01:11:00', '01:04:00', '02:11:00', '02:04:00'),
('4', '05:30:00', '05:30:00', '00:47:00', '00:47:00', '01:47:00', '01:47:00'),
('5', '05:30:00', '05:30:00', '00:41:00', '00:42:00', '01:41:00', '01:42:00'),
('6', '05:30:00', '05:30:00', '00:42:00', '00:41:00', '01:41:00', '01:41:00'),
('7', '05:29:00', '05:28:00', '00:27:00', '00:20:00', '01:27:00', '01:20:00'),
('7BIS', '05:44:00', '05:31:00', '01:07:00', '00:54:00', '02:07:00', '01:54:00'),
('8', '05:30:00', '05:30:00', '00:24:00', '00:24:00', '01:24:00', '01:24:00'),
('9', '05:30:00', '05:24:00', '00:26:00', '00:26:00', '01:26:00', '01:26:00');
计算每条线的两个停靠点之间的平均距离。
我已经完成了一个查询。但我不确定它是否正确。 请帮帮我
SELECT AVG(pd.pcount)
FROM(SELECT LineID,SUM(DistanceToNext) as pcount
FROM stops
GROUP BY StationID HAVING COUNT(StationID)=2) as pd
答案 0 :(得分:1)
如果您只想要每行两个站点之间的平均距离,我认为一个简单的聚合查询应该有效:
SELECT
LineID, AVG(DistanceToNext) AS avg_distance
FROM stops
GROUP BY
LineID;
第二个地铁表似乎是某种时间表,似乎与距离没有任何关系。