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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:43:23+00:00 2026-06-04T07:43:23+00:00

I have a table that looks like this: ID AMID DESC value —————————————————————– 100

  • 0

I have a table that looks like this:

  ID               AMID           DESC                  value 
  -----------------------------------------------------------------
  100              type A         AMID type A             10
  101              type B         AMID type B             18
  102              type C         AMID type C             34
  101               null            null                   4
  102               null            null                  19
  103              type D         AMID type D              6   
  103              type E            null                  7 

The table contains around 6 million rows.

Now I want to have the result like this

  ID               AMID           DESC                  value 
  -------------------------------------------------------------
  100              type A         AMID type A             10
  101              type B         AMID type B             18
  102              type C         AMID type C             34
  101              type B         AMID type B              4
  102              type C         AMID type C             19
  103              type D         AMID type D              6   
  103              type E            null                  7

It has show the same values when the AMID for ID in both rows are equal are one of it is null, if the AMID is different then leave it like that..

Thanks in advance for the help..

Cheers,

Harish

  • 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-04T07:43:25+00:00Added an answer on June 4, 2026 at 7:43 am

    First, let’s set up some proper DDL and sample data:

    USE tempdb;
    GO
    
    CREATE TABLE dbo.AMID
    (
      ID     INT, 
      AMID   VARCHAR(32), 
      [DESC] VARCHAR(32), 
      value  INT
    );
    
    INSERT dbo.AMID SELECT 100, 'type A', 'AMID type A', 10
          UNION ALL SELECT 101, 'type B', 'AMID type B', 18
          UNION ALL SELECT 102, 'type C', 'AMID type C', 34
          UNION ALL SELECT 101,  null   , null         , 4
          UNION ALL SELECT 102,  null   , null         , 19
          UNION ALL SELECT 103, 'type D', 'AMID type D', 6   
          UNION ALL SELECT 103, 'type E', null         , 7;
    

    Instead of using TOP without ORDER BY, which I don’t even know what it means, you can pick exactly one row to use for populating the NULL rows using partition + order. In your limited sample data you only have a max of two rows for any ID, but in case you can have more, this is important (otherwise, like TOP without ORDER BY, you’re going to pull some arbitrary row, making your update less than predictable). This orders by the value in the AMID column, but you can change this to pick the top row per ID using any criteria in the table.

    ;WITH src AS 
    (
      SELECT ID, AMID, [DESC]
      FROM dbo.AMID 
      WHERE AMID IS NULL AND [DESC] IS NULL
    ),
    nv AS 
    (
      SELECT ID, AMID, [DESC], rn = ROW_NUMBER() OVER
      (PARTITION BY ID ORDER BY AMID) -- change this ordering accordingly
      FROM dbo.AMID 
      WHERE AMID IS NOT NULL
    )
    UPDATE src
      SET AMID = nv.AMID, [DESC] = COALESCE(nv.[DESC], src.[DESC])
    FROM src INNER JOIN nv
    ON src.ID = nv.ID
    WHERE nv.rn = 1;
    
    SELECT ID, AMID, [DESC], Value FROM dbo.AMID;
    

    Results:

    ID   AMID    DESC         value
    ---  ------  -----------  -----
    100  type A  AMID type A  10
    101  type B  AMID type B  18
    102  type C  AMID type C  34
    101  type B  AMID type B  4
    102  type C  AMID type C  19
    103  type D  AMID type D  6
    103  type E  NULL         7     
    

    Don’t forget to clean up:

    DROP TABLE dbo.AMID;
    GO
    

    As an aside, DESC is a horrible name for a column, since it’s a T-SQL keyword, and therefore always has to be escaped with [double quotes].

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

Sidebar

Related Questions

I have a table that looks like this: id count 1 100 2 50
I have a table that looks like something like this: timestamp value person ===============================================
I have a table that looks like this: ---------------------- | DateTime | Value |
i have a table that looks like this: Field: msg_sent_datetime Type: datetime yet when
I have a table that looks like this <table cellpadding=1 cellspacing=0 width=100% border=0> <tr>
I have a table that looks like this: id datatype name value 0 nvarchar(255)
I have a table that looks something like this: ________________________ |id|value|date|approved| ----------------------- What I
I have a table that looks something like this: ID | STATUS | TYPE
I have a table that looks like this, Date Value 01/01/2010 03:59:00 324.44 01/02/2010
I have a table that looks like this: Id GroupId Value and it 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.