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

The Archive Base Latest Questions

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

I have a database table structured like this (irrelevant fields omitted for brevity): rankings

  • 0

I have a database table structured like this (irrelevant fields omitted for brevity):

rankings
------------------
(PK) indicator_id
(PK) alternative_id
(PK) analysis_id
rank

All fields are integers; the first three (labeled “(PK)”) are a composite primary key. A given “analysis” has multiple “alternatives”, each of which will have a “rank” for each of many “indicators”.

I’m looking for an efficient way to compare an arbitrary number of analyses whose ranks for any alternative/indicator combination differ. So, for example, if we have this data:

analysis_id | alternative_id | indicator_id | rank
----------------------------------------------------
          1 |              1 |            1 |    4
          1 |              1 |            2 |    6
          1 |              2 |            1 |    3
          1 |              2 |            2 |    9
          2 |              1 |            1 |    4
          2 |              1 |            2 |    7
          2 |              2 |            1 |    4
          2 |              2 |            2 |    9

…then the ideal method would identify the following differences:

analysis_id | alternative_id | indicator_id | rank
----------------------------------------------------
          1 |              1 |            2 |    6
          2 |              1 |            2 |    7
          1 |              2 |            1 |    3
          2 |              2 |            1 |    4

I came up with a query that does what I want for 2 analysis IDs, but I’m having trouble generalizing it to find differences between an arbitrary number of analysis IDs (i.e. the user might want to compare 2, or 5, or 9, or whatever, and find any rows where at least one analysis differs from any of the others). My query is:

declare @analysisId1 int, @analysisId2 int;
select @analysisId1 = 1, @analysisId2 = 2;

select 
    r1.indicator_id, 
    r1.alternative_id,
    r1.[rank] as Analysis1Rank,
    r2.[rank] as Analysis2Rank
from rankings r1
inner join rankings r2
    on r1.indicator_id = r2.indicator_id
        and r1.alternative_id = r2.alternative_id
        and r2.analysis_id = @analysisId2
where
    r1.analysis_id = @analysisId1
    and r1.[rank] != r2.[rank]

(It puts the analysis values into additional fields instead of rows. I think either way would work.)

How can I generalize this query to handle many analysis ids? (Or, alternatively, come up with a different, better query to do the job?) I’m using SQL Server 2005, in case it matters.

If necessary, I can always pull all the data out of the table and look for differences in code, but a SQL solution would be preferable since often I’ll only care about a few rows out of thousands and there’s no point in transferring them all if I can avoid it. (However, if you have a compelling reason not to do this in SQL, say so–I’d consider that a good answer too!)

  • 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-11T19:28:06+00:00Added an answer on May 11, 2026 at 7:28 pm

    This will return your desired data set – Now you just need a way to pass the required analysis ids to the query. Or potentially just filter this data inside your application.

        select r.* from rankings r
        inner join
        (
            select alternative_id, indicator_id
            from rankings
            group by alternative_id, indicator_id
            having count(distinct rank) > 1
        ) differ on r.alternative_id = differ.alternative_id
        and r.indicator_id = differ.indicator_id
        order by r.alternative_id, r.indicator_id, r.analysis_id, r.rank
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 291k
  • Answers 291k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I would embed the XML file directly into the assembly… May 13, 2026 at 6:05 pm
  • Editorial Team
    Editorial Team added an answer No, you don't. The following lines give you all the… May 13, 2026 at 6:05 pm
  • Editorial Team
    Editorial Team added an answer Find it once in O(log n) and then compare new… May 13, 2026 at 6:05 pm

Related Questions

Gday, I have a table that shows a series of scores and datetimes those
i have a LAPP (linux, apache, postgresql and php) environment, but the question is
I have a table structure like this (vertical design). I can have unlimited number
I have a set of records, stored as XML files, where the XML files

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.