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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:29:46+00:00 2026-06-18T10:29:46+00:00

I’m unable to figure out a sql query (using MS Sql Server). I’m trying

  • 0

I’m unable to figure out a sql query (using MS Sql Server). I’m trying to retrieve a single row from a dataset in which an item with one id can have more than one row. The part that is throwing me off is that the correct row should be based on a “hierarchy”. I have trying to throw a case statement at the problem.

Some sample data:

Id           Class       Date
100          Red         2012-12-12
100          Blue        2012-12-31
200          Red         2012-10-31
300          Green       2012-04-04
300          Blue        2011-09-01

I want to return a single row based on the value of Class.

Case When Red Then
    Date 
Case When Blue Then 
    Date
Case When Green Then 
    Date 
Else
    ''

My final dataset should look like this:

Id           Class       Date
100          Red         2012-12-12
200          Red         2012-10-31
300          Blue        2011-09-01

So, if one of the duplicate rows has a value of Red, use the date from that row first. Then blue, then green.

Been working on this one all day, playing around with subqueries, group bys, havings, case statements, derived tables. I’m quite rusty on my sql skills, as it’s been a while.

Any hints on the direction I should take?

  • 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-18T10:29:47+00:00Added an answer on June 18, 2026 at 10:29 am

    You can try this

    ;WITH cte AS
      (
        SELECT Id, Class, Date,
          row_number() OVER (PARTITION BY Id
                             ORDER BY CASE Class
                                      WHEN 'Red' THEN 1
                                      WHEN 'Blue' THEN 2
                                      WHEN 'Green' THEN 3
                                      ELSE 4 END) as rn
        FROM MyTable
       )
    
    SELECT Id, Class, Date
    FROM cte
    WHERE rn = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
I have a text area in my form which accepts all possible characters from
I'm trying to select an H1 element which is the second-child in its group
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
I am using jsonparser to parse data and images obtained from json response. When
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.