Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Administration
 query involving multiple tables

Author  Topic 

suhana
Starting Member

2 Posts

Posted - 2012-01-02 : 12:12:51

I have several tables with the following fields and datatypes

query involving multiple tables

1. State definition table
StateID char(2), AgencyName Varchar(35)
AL, Alaska
GA, Georgia
CO, Colorado
FL, Florida

2. Cities definition table
StateID int, Branch Name varchar(35), StateIDchar(2)
1000, Ancorage, AK
1001, Lakes, AK
1002, Kodiac, AK
2000, Atlanta, GA
2001, Berlin, GA
3000, Aurora, CO
3001, Denver, CO
3002, Boulder, CO
4000, Orlando, FL
4001, Tampa, FL

3. Offices definition table
OfficeName Varchar(35), OfficeDesc Varchar(50)
1000 Office10, .....
1001 Office11,......
1002 Office12,......
2000 Office20,......
2001 Office21,......
3000 Office30,......
3001 Office31,......
3002 Office32,......
4000 Office40,......
4001 Office41,......

here, each city has just one office( office name is preceded by cityid), but a city can have many offices in it

4.Vehicles definition table
VehicleID int, OfficeName, VehicleDesc
100, 1000 Office1,.....
101, 1000 Office1,.....
102, 1000 Office1,.....
103, 1000 Office1,.....
104, 1001 office2,.....
105, 1001 office2,.....
106, 1002 office3,.....
107, 2000 office4,.....
108, 2000 office4,.....
109, 2000 office4,.....
110, 2001 office5,.....
111, 2001 office5,.....
112, 2001 office5,.....
113, 3000 office6,.....
114, 3000 office6,.....

There are several other tables which store only the vehicle id and other related atributes of the vehicle
for eg. voltage table
vehicle id int, batteryvolts int,eventdate datetime
100, 10286, 8/1/2007 9:20:25 AM
100, 10289, 8/1/2007 9:20:28 AM
100, 10289, 8/1/2007 9:21:18 AM
100, 10286, 8/1/2007 9:21:25 AM
101, 10286, 8/1/2007 9:20:25 AM
101, 10286, 8/1/2007 9:20:25 AM
101, 10286, 8/1/2007 9:20:25 AM
102, 10286, 8/1/2007 9:20:25 AM
102, 10286, 8/1/2007 9:20:25 AM
103, 10286, 8/1/2007 9:20:25 AM
103, 10286, 8/1/2007 9:20:25 AM
103, 10286, 8/1/2007 9:20:25 AM
109, 10286, 8/1/2007 9:20:25 AM
109, 10286, 8/1/2007 9:20:25 AM
109, 10286, 8/1/2007 9:20:25 AM

VehicleUsage table
vehicleid int, odometer int, ignition int, recorddate datetime


I have to generate a report where the user can query either by state, or city, or office or vehicles and you can imagine all possible combinations of parameters there by all state, one or multiple states, all offices and all vehicles, one office or all its vehicles, one or multiple offices and one or more vehicles of the selected offices, all vehicles only, one or multiple vehicles only, etc

what i want to do is produce a report which lists state, city, office, vehicleid, batteryvolts, odometer, ignition where battereyvolts, odometer, igntion
are fields in other tables and are calulated values summed based on the max or min date or other conditions in the where clause

Currently I am doing a left outer join where I start with state, left outer join with city on stateid, and left outer join with office on city id and so on.But since database is farily large with large number of records and is growing everyday, I am worried about the performance of the query as it could be very quick if the user queries for just one vehicle, but could take much longer if it involves calculating for several vehicles of several offices
and so on

If anyone can help me, I would highly appreciate.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-01-02 : 12:42:49
I don't see a question. Are you asking about performance of getting the data back or need help in writing the query? From your post, it seems you've already got a working query but you didn't post it. Is it too slow? Is it indexed?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-01-02 : 12:50:30
I would suggest starting off with analysing the execution plans of slow running queries or intensive queries and see if you can add some level of optimisation like adding proper indexes

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

suhana
Starting Member

2 Posts

Posted - 2012-01-02 : 13:27:11
I have developed a solution which is okay if user queries by just one vehicle.
I am worried about the situations where if the user queries by for eg 1000 Office10 and all vehicles in it, or 1000 Office10, 1001 Office11 and all vehicles in it or similar parameter combination

For eg. user supplies vehicle id 100

Currently I have the query something like this:

ALTER PROCEDURE [dbo].[reportOneVehicle]

@VehicleID int
AS
BEGIN

SET NOCOUNT ON;

select a.officename , b.vehicleid,
c.odometer ,c.Ignition ,e.BatteryVoltage/1000 as Voltage, f.TotalAccum
from
(


(select officename, officedesc from Offices) as a
left outer join


(select vehicleid, officename from vehicle where vehicleid = 100 ) as b
on a.officename = b.officename
left outer join


(select vehicleid, odometer/1609.344 as odometer, ignition/3600 as ignition, recorddate from vehicleusage ) as c
on b.vehicleid = c.vehicleid)

left outer join


(select vehicleid,(convert(varchar(6), (max(AccumSeconds))/3600)+ ':' +
RIGHT('0' + convert(varchar(2), ((MAX(AccumSeconds))%3600)/60),2) + ':' +
RIGHT('0' + convert(varchar(2), (max(AccumSeconds))%60),2)) as TotalAccum
from AccumulatedTotal
where Desc = 'Motion' group by VehicleID) as f
on b.vehicle = f.vehicleid

left outer join


(select p.vehicleid, p.BatteryVoltage, p.eventdate from voltage p where
p.eventdate = (select max(eventdate) as eventdate from voltage
where vehicleid = p.vehicleid)) as e
on b.vehicleid = e.vehicleid
order by b.Officename,b.vehicleid

END

This is okay for one vehicle, but how should I handle if user has parameters like multiple vehicles (100,101,102) or OfficeName,VehicleID('1000 Office10', '1001 office11') (100,101,102,103)

I am worried about performance, and so writing an efficient query which can tackle situations similar to this, where user can query from State level, or city level or office level or vehicle level and yet get all activities related to the vehicles that belong to an office, city, state.
Go to Top of Page

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2012-01-02 : 15:52:53
Firstly, on the query itself, you don't need all those subqueries and having them just makes the query harder to read.

Taking just a snippet of your query...

SELECT a.officename ,
b.vehicleid ,
c.odometer ,
c.Ignition ,
e.BatteryVoltage / 1000 AS Voltage ,
f.TotalAccum
FROM ( ( SELECT officename ,
officedesc
FROM Offices
) AS a
LEFT OUTER JOIN ( SELECT vehicleid ,
officename
FROM vehicle
WHERE vehicleid = 100
) AS b ON a.officename = b.officename
LEFT OUTER JOIN ( SELECT vehicleid ,
odometer / 1609.344 AS odometer ,
ignition / 3600 AS ignition ,
recorddate
FROM vehicleusage
) AS c ON b.vehicleid = c.vehicleid
)


This could be written as just this:

SELECT o.officename ,
v.vehicleid ,
v.odometer / 1609.344 AS odometer ,
v.ignition / 3600 AS ignition
FROM Offices o
LEFT OUTER JOIN Vehicle v ON o.officename = v.officename AND v.vehicleid = 100
LEFT OUTER JOIN vehicleusage vu ON v.vehicleid = vu.vehicleid


I'm sure you'll agree that's a lot easier to read.

Also, if you're going to alias tables, use meaningful aliases. Aliasing Vehicles as B makes no sense. B is unrelated to the table name and just means when I encounter that elsewhere in the query I have to go back and look for which table has been aliased B.

As for the various situations, typically you would create one procedure for each, so a procedure for search by vehicleID (if you're using SQL 2008 see table-type parameters for how you pass multiple), another procedure to search by office, etc. One procedure written so it can handle all possibilities is going to be slow or complex, and will usually end up being both.

--
Gail Shaw
SQL Server MVP
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-01-03 : 06:25:18
for passing multiple set of parameter valus use table valued parameter, XML parameter etc

see

http://vyaskn.tripod.com/passing_arrays_to_stored_procedures.htm

http://visakhm.blogspot.com/2010/04/using-xml-to-batch-load-master-child.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -