Wondering if anyone out there might know a trick to a small sql script.
http://sqlfiddle.com/#!3/09638/3
I am looking to return only the rows that have a manual transmission and are Ford make. If no rows exist, then return all Ford make vehicles. I currently doing it using an IF EXISTs condition. I considered using a temporary table to store the first set of data, then looking at the rowcount() (rows inserted == 0) of the table to see if I needed to insert more data. There may be no other way to do it then my two options I described. Maybe the community has some thought on it.
Example DDL
CREATE TABLE Cars
(
Make varchar(50),
Model varchar(50),
ManualTransmission bit
);
INSERT INTO Cars
(Make, Model, ManualTransmission)
VALUES
('Ford', 'Taurus', 0),
('Ford', 'Contour', 0),
('Ford', 'Mustang', 0),
('Jeep', 'Liberty', 1),
('Jeep', 'Cherokee', 0);
One way
Or another
Both of these answers exploit the fact that
ManualTransmissionis aBITdatatype and1is the maximum possible value it can have. IfManualTransmissionis nullable then you would need to change them toOr
The
CASEversion would also be adaptable for more complex conditions.