Question

I've done something like this in pvp using while loop but I don't think that is valid for vb. I have a total of 4 tables and 3 of them are based off of 1.

Main Table:
weather

Sub Tables:
applicationData
accidentData
trafficData

My goal is to use my filter system to check the weather data table. It then searches the other tables for traffic, accident, and application data based off the location, date, and time of the weather data fields. (They all share these columns). There are times where there are several accidents for each date/time/location.

For the sake of being easier to explain I'd like you to explain it with two different tables so here are my queries.

SELECT event, date, time, location 
FROM weather 
WHERE date= '" & datepicker.Text & "' 
AND time = '" & eventTime.SelectedItem.Value & "'

I then want to take the date, time, and location and search one of the sub tables with that information.

SELECT roadway_number, mile_marker 
FROM  accident 
WHERE date= 'query1Date' 
AND time = 'query1Time' 
AND location = 'query1Location'

I think want to display all this information in a datagrid and have it display to show all weather fields even if there is no accident data to go with it. Like this:

1pm, 12/1/10, Amity Weather Event: Snow

2pm, 12/1/10, Amity Weather Event: Light Rain

5pm, 12/1/10 Amity Weather Event: Heavy Snow Accident: Road: I165N, Mile_Marker 95.5 Road: I165N, Mile_Marker 71

7pm, 12/1/10 Amity Weather Event: Heavy Snow Accident: Road: I165S, Mile_Marker 85.5

I really appreciate the help!

Was it helpful?

Solution

Instead of querying the database in a loop (a real performance killer), join the tables in your query:

SELECT weather.event, weather.date, weather.time, location,
    accident.roadway_number, accident.mile_marker
FROM weather
LEFT OUTER JOIN accident on weather.date = accident.date and weather.time = accident.time
    and weather.location = accident.location
WHERE date= '" & datepicker.Text & "' AND time = '" & eventTime.SelectedItem.Value & "'

The OUTER join is the key to getting all weather records regardless of if there are related accidents.

You should also look into parametrizing your query instead of concatenating the input values.

OTHER TIPS

You should be able to use an left outer join for this:

SELECT w.event, w.date, w.time, w.location, a.roadway_number, a.mile_marker 
FROM weather w
LEFT OUTER JOIN accident a ON a.date = w.date 
                          AND a.time = w.time
                          AND a.location = w.location
WHERE w.date= '" & datepicker.Text & "' 
AND w.time = '" & eventTime.SelectedItem.Value & "'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top