我有两个包含一些列车信息的表。SQL:将表中的两个值与另一个表中的第三个值进行比较
- trainStation(station_code,市,州)
- 离港(id_departure,origin_station_code,destination_station_code)
是否有可能找出多少偏离是同一州内?
我有两个包含一些列车信息的表。SQL:将表中的两个值与另一个表中的第三个值进行比较
是否有可能找出多少偏离是同一州内?
当然...
Select count(*) NbrDepInCA
from departures d join trainStation s
on s.station_code = d.origin_station_code
Where s.State = 'CA'
,并通过@Katteract的建议,得到计数的所有状态(即至少有一个出发)
Select s.State, count(*) NbrDepInState
from departures d join trainStation s
on s.station_code = d.origin_station_code
Group by s.State
或使用“GROUP BY s.State”查找所有状态的计数。 – Kateract
在PostgreSQL
你可以使用的东西像这样的SQL:
SELECT COUNT(1) AS count FROM departures d
JOIN trainStation t1 ON t1.station_code = d.origin_station_code
JOIN trainStation t2 ON t2.station_code = d.destination_station_code
WHERE t1.state = t2.state
您正在使用哪些DBMS? –