I have tables for police stations and polling booths.
Table police station have column (1) gid, (2) Name_police station (3) pin code (4) geom
Table Polling Booth have column (1) gid (2) Booth_name (3) geom
How can I get the police stations which are within the range of 2km radius of polling the polling booths.
Related
I have a table named Pupils and a table named Bus.
Bus contains an ID,Destination (where it goes to) and Time (when it leaves).
Pupils contains Name,Location(Where it lives) and BusID (Which bus it needs to take).
I want to fill the 'BusID' column with the correct 'ID' from the 'Bus' table (depending on the pupil's location and time selected).
For example, the pupil lives in Tel Aviv and wants a bus at 10:00.
I need to fill his 'BusID' column AND ROW ( there are many pupils) with the same 'ID' in the bus table where Destination equals Tel Aviv and Time equals 10:00.
Is it in anyway possible? I was thinking about using Insert into/Insert into select but it doesn't really fits my needs.
As I meanwhile understood: the pupils are somewhere else (presumably not in T.A.) and want to find the bus that brings them back to T.A. at 10:00 hrs.
This can then be achieved with
-- The id of the bus that travels at 10:00 to T.A.:
select id from bus where destination='Tel Aviv' and time='10:00:00';
-- Update the pupil's record with the bus-id:
update pupils set busid=... where location='Tel Aviv';
-- Together (and that's the answer):
update pupils set busid=(select id
from bus
where destination='Tel Aviv'
and time='10:00:00')
where location='Tel Aviv';
Note that this assumes there is exactly one result row in the first query, i.e. exactly one 10 o'clock bus to T.A.
I bought a geo-database a long time ago and I'm updating its precision to the lat/lng values. But I've found some weird stuff. There are some cities that have the same lat/lng coordinates. Thing that is geographically impossible.
id City State Lat Lng
1 A sA XX XX
2 B sA XX XX
3 C sA YY YY
4 D sA ZZ ZZ
So I tried Group By City, Lat, Lng but as I need the id to update the record the group by clause will ask me to add ´id´ column.
From the table ids 1 and 2 should be updated leaving 3 and 4 out. It shouldn't be 2 (or more) cities with the same Lat/Lng. The Table has 22K rows. I could send all to gmap API but I'm looking for use the time, bandwith and hits to the API as smart as possible but I'm running out of time considering I can make a request per second using the free API access.
I've tried
SELECT DISTINCT postcodes_id, Latitude, Longitude, Region1Name, Region2Name, Nation_D
FROM postcodes
where Latitude + Longitude IN
(
SELECT Latitude + Longitude
FROM
(
SELECT postcodes_id, Latitude, Longitude, count(distinct(Region2Name)) as cantidad
FROM postcodes
where Nation_D is not null
GROUP BY Latitude, Longitude
having count(distinct(Region2Name)) > 1
) A
)
AND Nation_D IS NOT NULL
ORDER BY Latitude, Longitude, Region1Name, Region2Name, Nation_D
But is not working as expected. I think its pretty obvious for a new pair of eyes.
I wrote a python script to use Google Map geocode to get the current Lat/Lng and update it if it's different. This script works ok.
Hope someone has an idea. Thanks!!
Running MySQL 5.5 and Python 2.7 on a CentOS 7.
Just some pointers for you, which may be helpful:
You should not use group by or distinct on lat/lon or any combination of them, since they are contiguous floating points numbers and not discrete integers or strings.
By the same token, you should not use WHERE clauses on lat/lon or their sum. If you mean to check for proximity of two locations, use st_distance() function instead.
Multiple city names can refer to the same location. For example, New York, NY and Manhattan, NY.
And a non-technical point: storing Google geocoding data in your database is against their licensing agreement.
I am currently creating a table to store taxi bookings.
Key
Id - booking id
CustId - id of customer who is being picked up
DriverId - id of driver who is picking up customers
PickupAddr - pickup location
PickupLat - lat coordinate of pickup location
PickupLon - lon coordinate of pickup location
DropoffAdr - drop off location
DropoffLat - lat coordinate of dropoff location
DropoffLon - lon coordinate of dropoff location
Mileage - distance between PickupAddr and DropoffAddr, including any
other stops made
Stops - number of waypoints/stops during journey
Price - what this customer has to pay for this part in the journey
The problem I am facing at the moment is that I don't know how to deal with waypoints.
e.g. if you've been picked up at A to go to B but decide to stop at A1 and A2 during the journey to pick some friends up (all pre-booked beforehand of course)
What would be the best way of restructuring my current table to accommodate this?
Should it come as a separate row or column? As this data is dynamic (i.e. one can't anticipate the number of pickups/dropoffs a customer makes mid journey), what should I be aiming to accomplish.
Since this is a one-to-many relationship, it should be a separate table:
id - Waypoint ID
booking_id - FK to booking table
sequence - ordering of this waypoint within the trip
addr - address of waypoint
lat - latitude of waypoint
long - longitude of waypoint
If you want, you could remove the PickupXXX and DropoffXXX columns from the bookings table, and simply use the waypoints table for this. Pickup could be the first waypoint, and dropoff would be the last one.
I have 2 tables: city and city_neighbor.
city contains a list of all cities while city_neighbor contains the neighbor(s) of a given city: insert into city_neighbor (city_id, neighbor_id) Values(1,2) - where neighbor_id is the id of another city from the city table. Nothing too fancy.
What I must build is a page where the user can select all cities in a given radius. Ex: select all cities neighboring Chicago in a radius of 5 cities for example. IMPORTANT: it's radius of 5 cities. I can't use radius of miles/km, I need to use this path style of radius (no lat/long related responses please).
Second scenario: The user wants to select all cities ordered ascending by the number of jumps (where a jump is one city which must be passed in order to get from A(Chicago) to B).
Any ideas?
P.S. My database contains ~8,000 cities.
FUNCTION: CountNeighbors
IMPORT: city, depth
EXPORT: neighbors
ALGORITHM:
neighbors = SET
++depth
direct_neighbors = SELECT NEIGHBORS OF city
neighbors += direct_neighbors
IF depth < 5
FOR EACH neighbor IN direct_neighbors
neighbors += CountNeighbors(neighbor, depth, result)
I am trying to design the backend and have the following use case.
I have flight information from point A to B and need to define a schema which supports different use cases.
I'm trying to find a good way to handle the case, when there are stopover via points.
For e.g. flight route for A -> B actually looks like this:
A -> C
C -> D
D -> B
so A -> B is one entity, but in turn, it is comprised of several legs.
My current design:
AirLeg table:
- id
- departure and arrival information
- viaPoints: BOOL
viaPoints table:
- id
- airLegId // FK into Airleg table
- similar departure and arrival information from airLeg table
// if viaPoints flag is True in AirLeg table, viaPoints table can be queried for, using airLegId table to retrieve intermediaries.
Is there a better way to deal with this ?
I thought I'll add the info I am storing about a one way trip or segment:
AirLeg-id
Departure Airport : FK into airports
Arrival Airport : FK into airports
Departure timestamp (in departure city's local time)
Arrival timestamp (in arrival city's local time)
flight duration of this airleg: static value
flightId : FK into airlines yielding airline name and flight number
Baggage Policy : text
Misc (TEXT: Cancellation policy)
EDIT:
I added a related question and I think the answer to this problem will have to cater to both the requirements.
If there are multiple segments in a trip, price is defined for the complete trip and not individual segments
Similarly, the price for a round trip is specified as a unit and not individual components from A->B and back, B->A.
I'd design it like this:
Journeys:
- ID
- Other info (billing, whatever)
Segments:
- ID
- JourneyID (FK)
- departure, arrival, etc
And an additional view
Journeys_View
- Journeys.*
- First departure
- Last arrival
I'm trying to piece together the two questions, and it's not totally clear what you want to do - but I think it boils down to the following.
You have an itinerary, which is the parent item. An "itinerary" has multiple legs (question: do you want to deal with multi-part itinerarys, e.g. "London->Paris->New York->London"?). An itinerary has a price. The price is NOT the sum of the price of the legs, because return trips are cheaper than two one ways.
Itinerary
---------
ID
Price
Leg
----
Departure Airport : FK into airports
Arrival Airport : FK into airports
Departure timestamp (in departure city's local time)
Arrival timestamp (in arrival city's local time)
flight duration of this airleg: static value
flightId : FK into airlines yielding airline name and flight number
Baggage Policy : text
Misc (TEXT: Cancellation policy)
You could store price in a separate table - but you only need to do this if price changes independently of itinerary (e.g. if the price on Monday is $100, and on Tuesday it's $200).
I would encourage you not to use "magic numbers" in your database schema - instead of having the return leg be "-1", you should leave it NULL - there is no return leg. This makes your SQL a lot easier to read, and far less error prone - you don't depend on developers remembering that "-1" means there's no return leg, -2 meaning there's a provisionally booked leg etc.