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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:37:38+00:00 2026-06-11T05:37:38+00:00

I need help with a t-sql query. I have a table with this structure:

  • 0

I need help with a t-sql query.

I have a table with this structure:

id  |  OverallRank  |  FirstRank  |  SecondRank  |  Nrank..
1   |       10      |    20       |     30       |    5
2   |       15      |    24       |     12       |    80
3   |       10      |    40       |     37       |    12

I need a query that produces this kind of result:

When id: 1

id  |  OverallRank  |  BestRankLabel  |  BestRankValue  |  WorstRankLabel  | WorkRankValue
1   |     10        |    SecondRank   |        30       |     Nrank        |       5

When id: 2

id  |  OverallRank  |  BestRankLabel  |  BestRankValue  |  WorstRankLabel  | WorkRankValue
1   |     15        |    FirstRank    |        24       |     SecondRank   |       12

How can I do it?

Thanks in advance

  • 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-11T05:37:39+00:00Added an answer on June 11, 2026 at 5:37 am
    with cte(id, RankValue,RankName) as (
                SELECT id, RankValue,RankName
                FROM 
                            (SELECT id,  OverallRank,  FirstRank,  SecondRank, Nrank 
                            FROM ##input) p
                UNPIVOT
                            (RankValue FOR RankName IN 
                                        (OverallRank,  FirstRank,  SecondRank, Nrank)
                )AS unpvt)
    select t1.id, max(case when RankName = 'OverallRank'  then RankValue else null end) as OverallRank,
            max(case when t1.RankValue = t2.MaxRankValue then RankName else null end) as BestRankName,
            MAX(t2.MaxRankValue) as BestRankValue,
            max(case when t1.RankValue = t3.MinRankValue then RankName else null end) as WorstRankName,
            MAX(t3.MinRankValue) as WorstRankValue
            from cte as t1
            left join (select id, MAX(RankValue) as MaxRankValue from cte group by id) as t2 on t1.id = t2.id
            left join (select id, min(RankValue) as MinRankValue from cte group by id) as t3 on t1.id = t3.id
        group by t1.id
    

    Working good with your test data. You should only edit RankName IN (OverallRank, FirstRank, SecondRank, Nrank) by adding right columns’ names.

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

Sidebar

Related Questions

I need help with a SQL Query I have this table likes friend friend2
need help/guide for sql select query, I have 2 table stock and stock_history, in
i need some help with a sql query. I have a table called 'table1'
I need help to build SQL query. I have 2 tables table PROJECT and
I need some help to build SQL Query. I have table having data like:
I need help with an SQL-Query. I have one table with many entries and
I need help with an advanced SQL-query (MSSQL 2000). I have a table called
Need a little help with a SQL / ActiveRecord query. Let's say I have
I have an interesting SQL problem that I need help with. Here is the
I need some help with a T-SQL query. I want to count fields that

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.