Solved

Help with SQL Query

Posted on 2013-07-01
4
376 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
ID: 39290468
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
ID: 39290525
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
ID: 39290533
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
ID: 39290555
Exactly what I needed!  Thanks!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

749 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