Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9083277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:44:20+00:00 2026-06-16T20:44:20+00:00

Wondering if anyone out there might know a trick to a small sql script.

  • 0

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);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T20:44:21+00:00Added an answer on June 16, 2026 at 8:44 pm

    One way

    WITH CTE
         AS (SELECT *,
                    RANK() OVER (ORDER BY ManualTransmission DESC) AS Rnk
             FROM   Cars
             WHERE  Make = 'Ford')
    SELECT Make,
           Model,
           ManualTransmission
    FROM   CTE
    WHERE  Rnk = 1 
    

    Or another

    SELECT TOP 1 WITH TIES Make,
                           Model,
                           ManualTransmission
    FROM   Cars
    WHERE  Make = 'Ford'
    ORDER  BY ManualTransmission DESC 
    

    Both of these answers exploit the fact that ManualTransmission is a BIT datatype and 1 is the maximum possible value it can have. If ManualTransmission is nullable then you would need to change them to

    ORDER BY ISNULL(ManualTransmission,0) DESC
    

    Or

    ORDER BY CASE WHEN ManualTransmission = 1 THEN 0 ELSE 1 END
    

    The CASE version would also be adaptable for more complex conditions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am wondering if anyone out there knows how I might go about getting
I'm wondering if anyone out there as built a custom script or stored procedure
Any astronomers out there? I'm wondering if anyone has produced or stumbled upon a
Wondering if anyone out there has seen any jQuery plugins that will allow me
I am wondering if anyone out there on here has been able to succesfuly
I'm wondering if there's anyone else out there that has come across this problem
Wondering if anyone out there has ran into this before.... I'd like to use
Wondering if anyone out there can shed some light on why the following regular
I am just wondering if anyone out there knows if it is possible to
I'm wondering if there is some trick to keep a file in the repository

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.