Solved

Help with SQL Query

Posted on 2013-07-01
4
372 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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

920 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