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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:27:39+00:00 2026-06-12T19:27:39+00:00

I have a database that looks like this: label_id, scan_type, scan_cost And some rows

  • 0

I have a database that looks like this:

label_id, scan_type, scan_cost

And some rows that look like this:

001, A40, 70
001, A40, 70
002, A40, 85
003, A40, 85
003, A40, 85

I need to produce a result set that looks like this:

001, A40, 70
001, A40, 0
002, A40, 85
003, A40, 85
003, A40, 0

That is, any multiple occurrence of the same label_id then the scan_cost column needs to be set to 0, but the first occurrence of each label_id the value needs to remain untouched.

The label_id are not sequential if that changes anything.

Is it possible to achieve this behaviour in SQL? Note the SQL dialect is T-SQL, Microsoft SQL Server 2008

  • 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-12T19:27:41+00:00Added an answer on June 12, 2026 at 7:27 pm

    This query returns what you are looking for:

    SELECT
        label_id
    ,   scan_type
    ,   CASE WHEN row_number() OVER (PARTITION BY label_id ORDER BY scan_type)=1
            THEN scan_cost
            ELSE 0
        END
    FROM test
    ORDER BY label_id, scan_type
    

    The idea behind this solution is to partition the data by label_id, and use the ROW_NUMBER function to decide which data to keep.

    I used scan_type in the ORDER BY sub-clause, which is not ideal. If your actual table has a column with data that is better suited to determine what row is first, e.g. a timestamp column, you should use that other column instead. The second column in the outer order by needs to be the same as the column in the inner order by.

    Here is a link to this query on sqlfiddle.

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

Sidebar

Related Questions

I have a mysql database that looks like this: |id | city | created
say i have a nvarchar field in my database that looks like this 1,
I have a MySQL database table called Participant that looks something like this: (idParticipant)
I have a database that look like this: [Invoice] -PK:ID -... [InvoiceDetail] -PK:ID -FK:Invoice_ID
I have many collections of documents in a mongo database that look like this:
I have a database full of text fields that look like this: (paragraph of
I have some code to update a database table that looks like try {
I have a database table that looks like this: CREATE TABLE IF NOT EXISTS
I have a child object in the database that looks like this: CREATE TABLE
I have a database with a simple one-to-many relationship that looks like this: Tables

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.