Solved

Help with SQL Query

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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

777 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