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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:18:54+00:00 2026-05-16T01:18:54+00:00

Hi all I have a sql query SELECT FKid1, FKID2, a, b, c from

  • 0

Hi all I have a sql query

SELECT  FKid1, FKID2, a, b, c
from [source]
where FKID1 = 3

which returns the following data set so
(hope formatting holds)

   FKID1   FKID2   A    B    C
    3   40297    0      0   2
    3   40297    0      100 1
    3   40325    0      0   2
    3   40325    0      0   3
    3   40325    0      10  -1
    3   40348    0      10  3
    3   40391    0      10  -1
    3   40392    0      10  -1
    3   40501    0      10  -1
    3   40501    0      0   2

I’m trying to improve this query so that if there are 2 rows with duplicate FKID1 and FKID2 values, it will pick the column B value from a particular row as follows…

if there is a row with C = -1 use the B value in this row and ignore others.
if there are 2 rows with C <> -1 then pick the MAX(B) value.
For rows that are not duplicated, return as normal.

IE the results should look as follows…

  FKID1   FKID2   A    B    C
    3   40297    0      100 1
    3   40325    0      10  -1
    3   40348    0      10  3
    3   40391    0      10  -1
    3   40392    0      10  -1
    3   40501    0      10  -1

the correct values selected for the B column and no dupes.

We have a solution at the moment, but I think it’s overcomplicated and wondered if anyone had any ideas?

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-16T01:18:55+00:00Added an answer on May 16, 2026 at 1:18 am

    One way

    ;WITH cte As
    (
    SELECT  FKid1, FKID2, a, b, c,
    ROW_NUMBER() OVER (PARTITION BY FKID1, FKID2
                       ORDER BY CASE WHEN C = -1 THEN 0 ELSE 1 END ASC, B DESC) AS RN
    from [source]
    where FKID1 = 3
    )
    SELECT  FKid1, FKID2, a, b, c 
    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 have SQL SELECT query which returns a lot of rows, and I have
I have the following SQL query: SELECT Phrases.* FROM Phrases WHERE (((Phrases.phrase) Like *ing
i have the following SQL Query which runs on SQL Server CE 4 SELECT
I have the following sql query: SELECT field_number, COUNT(*) AS c FROM lead_detail WHERE
I have a following SQL QUERY: SELECT articles.name, articles.price, users.zipcode FROM articles INNER JOIN
I have the following SQL query: select expr1, operator, expr2, count(*) as c from
I have an SQL query like SELECT Col1, Col2 FROM Table1 UNION ALL SELECT
I have a SQL query which retrieve wall post list $qry=mysql_query(SELECT senderid,post,date FROM .post_table.
I have the following SQL query: SELECT DISTINCT business_key FROM Memory WHERE concept <>
I have the following SQL query: SELECT Comment, JD, Jurisdiction, RegStatus, Region, SPDR_NAME FROM

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.