Solved

Help with SQL Query

Posted on 2013-07-01
4
370 Views
Last Modified: 2013-07-01
I have three tables:

TableAccident (ID, Date, County)
TableVehicle (ID, UnitNumber, VehicleType,
TablePerson (ID, UnitNumber, PersonNumber, Age)

TableAccident has an ID column that is unique and has a one to many relationship with TableVehicle and TablePerson.  

For example, for a crash with 2 vehicles (one person in one vehicle and 3 in the other) the rows would look like the attached image.

I want to create a new table that has the following columns:
ID (unique)
Date
County
MotorcycleCrash ('Yes' if ANY of the vehicles involved are VehicleType='Motorcycle")
TeenCrash ('Yes' if [Age] is between 13 and 19 AND the PersonNumber=1 for any of the vehicles involved in crash -- otherwise 'No')
Screenshot-2.jpg
0
Comment
Question by:VAMS1
  • 2
  • 2
4 Comments
 
LVL 20

Accepted Solution

by:
dsacker earned 500 total points
Comment Utility
Sure you don't simply want either just a SELECT statement or a VIEW, since the data is all available in the other tables?

SELECT  t1.ID,
        t1.Date,
        t1.County,
        CONVERT(varchar(3), CASE (SELECT TOP 1 1 FROM TableVehicle t2
                                  WHERE  t2.ID = t1.ID
                                  AND    t2.VehicleType = 'Motorcycle')
            WHEN 1 THEN 'Yes'
            ELSE 'No'
        END)            AS MotorcycleCrash,
        CONVERT(varchar(3), CASE (SELECT TOP 1 1 FROM TablePerson t3
                                  WHERE  t3.ID = t1.ID
                                  AND    t3.Age BETWEEN 13 AND 19)
            WHEN 1 THEN 'Yes'
            ELSE 'No'
        END)            AS TeenCrash
FROM    TableAccident t1

Open in new window

If you only want the SELECT statement, delete or comment out the CREATE VIEW statement. (FYI: I post-edited and corrected the SQL.)
0
 

Author Comment

by:VAMS1
Comment Utility
dsacker -- This code is exactly what I'm looking for, though I am running into a problem when there are multiple Motorcycles involved in crash -- (e.g., TableVehicle has 1 car and 2 motorcyles).. when the query gets to this point, this error comes up:

Msg 512, Level 16, State 1, Line 2
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

It looks like it is trying to return 2 'Yes's, correct?  Any idea how to get around this?
0
 
LVL 20

Expert Comment

by:dsacker
Comment Utility
I corrected the code, possibly after you grabbed it. Can you grab again and test?

Also, if you want to create a view from this, above the SELECT statement, simply add:

CREATE VIEW ViewAccidentStats AS
0
 

Author Closing Comment

by:VAMS1
Comment Utility
Exactly what I needed!  Thanks!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now