Solved

Help with SQL Query

Posted on 2013-07-01
4
379 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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.

615 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