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 245673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:06:30+00:00 2026-05-11T21:06:30+00:00

Say I have a result set that can contain one or more values, but

  • 0

Say I have a result set that can contain one or more values, but I always only want one value in the result, and specific values are worth more than others.

How would I solve this in sql so I can use it in a sub-query?

Example (T-SQL specific, that’s the accent I work with):

I have a table:

tChore(ChoreId int primary key, ChoreDescription nvarchar(15))

which is loaded with all types of chores. I also have another table:

tDayChores(
    DayId int primary key,
    ChoreId int foreign key references tChore(ChoreId)
)

Which is loaded with days and chores in a many-to-many relationship.

There is of course a third table containing the name of all week days.

Now, lets say I would like to get one, and only one, day to vacuum. I prefer to do it on wednesdays, but if that’s not scheduled then I’d like to do it on thursdays, and in turn if that’s not scheduled then it doesn’t matter, any day is fine.

One way to limit the result set to one row would be to do this:

select top(1)
    DayId
from
    tDayChores DC
    inner join tChore C
        on C.ChoreId = DC.ChoreId
where
    C.ChoreDescription = 'vacuum'

However, if vacuum is scheduled for monday and wednesday, then I would get monday as a result, and I’d like to rank wednesday highest.

  • 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-05-11T21:06:30+00:00Added an answer on May 11, 2026 at 9:06 pm

    Here’s a solution using a sub-query:

    select top(1)
        t.DayId
    from (
            select
                DayId,
                case
                    when DayId = 3 then 1 -- DayId: 3 = Wednesday
                    when DayId = 4 then 2 -- DayId: 4 = Thursday
                    else 3
                end DayRank
            from
                tDayChores DC
                inner join tChore C
                    on C.ChoreId = DC.ChoreId
            where
                C.ChoreDescription = 'vacuum'
        ) t
    order by t.DayRank
    

    The day rank makes sure that wednesday and thursday are sorted before any other day, and then you simply pick the top one.

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

Sidebar

Related Questions

I have a result set, say clientList. And I want to make sure that
How can I get the set difference of two result sets? Say I have
I have several databases that each have a table called, say, products . One
Say i have this sample XML. <result> <field k='field1'> <value h='1'><text>text_value1</text></value> </field> <field k='field2'>
I have a problem in hand and that is to fetch a result set
I know for more than one result, you don't have much choice for using
Let's say I have a subtable called result . SELECT school, grade FROM (SELECT
Say I have a Telerik MVC Grid, AJAX bound and I want to put
Say I have a SSI script that uses exec, or a PHP script that
I have a table x that's like the one bellow: id | name |

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.