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

  • Home
  • SEARCH
  • 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 525039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:37:49+00:00 2026-05-13T08:37:49+00:00

I have following table and data: create table Foo ( id int not null,

  • 0

I have following table and data:

create table Foo
(
    id int not null,
    hid int not null,
    value int not null
)

insert into Foo(id, hid, value) values(1,1,1) -- use this as 1 < 3
insert into Foo(id, hid, value) values(1,2,3)

insert into Foo(id, hid, value) values(2,3,3) -- use this as 3 < 5
insert into Foo(id, hid, value) values(2,4,5)

insert into Foo(id, hid, value) values(3,2,2) -- use this or next one as value are the same
insert into Foo(id, hid, value) values(3,3,2)

Currently the “id” and “hid” has many-to-many association, what I want to achieve is to make the “hid” as “one” instead of “many”, the rule is to use the minimum “value” in the table, see comment in above sql code.

Is this possible use some query to achieve this instead of a cursor?

Thanks!

  • 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-13T08:37:49+00:00Added an answer on May 13, 2026 at 8:37 am

    SQL 2005:

    WITH X AS ( SELECT id, min(value) as minval from Foo group by id )
    SELECT * FROM 
    (
        SELECT Foo.*, RANK() OVER ( PARTITION by Foo.id order by Foo.hid, Foo.value ) as Rank
        FROM Foo JOIN X on Foo.id = X.id and Foo.value = X.minval
    ) tmp
    WHERE Rank = 1
    
    
    id          hid         value       Rank                 
    ----------- ----------- ----------- -------------------- 
    1           1           1           1
    2           3           3           1
    3           2           2           1
    

    The first line (WITH clause) gets a set of ids with the min value (my arbitrary choice).
    The RANK is used to eliminate duplicates – there may be a better way.

    With MySql or SQL 2000 I guess you could do this with a complicated set of subqueries.

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

Sidebar

Ask A Question

Stats

  • Questions 301k
  • Answers 301k
  • 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 In Access, as in Classic VB, put the radio buttons… May 13, 2026 at 8:14 pm
  • Editorial Team
    Editorial Team added an answer When you return PartialView("Application_AddServer");, you have to pass the model:… May 13, 2026 at 8:14 pm
  • Editorial Team
    Editorial Team added an answer I found out that the content-type can be specified hence… May 13, 2026 at 8:14 pm

Related Questions

If I have the following table & data to allow us to use the
Let's assume that I have two tables... Foo and Bar. They contain the following
I'm trying to implement a price fallback system in SQL server. I'd like to
table a (t_a): id name last first email state country 0 sklass klass steve
I'm trying to take a POCO object and update it with Linq2SQL using an

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.